Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Babel plugin to remove empty class constructors #18060

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ docs/
node_modules/
external/bcmaps/
external/builder/fixtures/
external/builder/fixtures_esprima/
external/builder/fixtures_babel/
external/quickjs/
external/openjpeg/
test/tmp/
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ docs/
node_modules/
external/bcmaps/
external/builder/fixtures/
external/builder/fixtures_esprima/
external/builder/fixtures_babel/
external/quickjs/
test/tmp/
test/pdfs/
Expand Down
2 changes: 1 addition & 1 deletion .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ docs/
node_modules/
external/bcmaps/
external/builder/fixtures/
external/builder/fixtures_esprima/
external/builder/fixtures_babel/
external/quickjs/
test/tmp/
test/pdfs/
Expand Down
20 changes: 20 additions & 0 deletions external/builder/babel-plugin-pdfjs-preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,26 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
}
},
},
ClassMethod: {
exit(path) {
const {
node,
parentPath: { parent: classNode },
} = path;
if (
// Remove empty constructors. We only do this for
// base classes, as the default constructor of derived
// classes is not empty (and an empty constructor
// must throw at runtime when constructed).
node.kind === "constructor" &&
node.body.body.length === 0 &&
node.params.every(p => p.type === "Identifier") &&
!classNode.superClass
) {
path.remove();
}
},
},
},
};
}
Expand Down
23 changes: 23 additions & 0 deletions external/builder/fixtures_babel/constructors-expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class A {
constructor() {
console.log("Hi!");
}
}
class B {
Dismissed Show dismissed Hide dismissed
constructor(x = console.log("Hi!")) {}
}
class C {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
constructor({
x
}) {}
}
class D {}
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
class E extends A {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
constructor() {}
}
class F {
Dismissed Show dismissed Hide dismissed
constructor() {
var a = 0;
Dismissed Show dismissed Hide dismissed
}
}
class G {}
Dismissed Show dismissed Hide dismissed
37 changes: 37 additions & 0 deletions external/builder/fixtures_babel/constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class A {
constructor() {
console.log("Hi!");
}
}

class B {
Dismissed Show dismissed Hide dismissed
constructor(x = console.log("Hi!")) {}
}

class C {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
constructor({ x }) {}
}

class D {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
constructor(x, y, z) {}
}

class E extends A {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
constructor() {}
}
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved

class F {
Dismissed Show dismissed Hide dismissed
constructor() {
if (PDFJSDev.test('TRUE')) {
var a = 0;
Dismissed Show dismissed Hide dismissed
}
}
}

class G {
Dismissed Show dismissed Hide dismissed
constructor() {
if (PDFJSDev.test('FALSE')) {
var a = 0;
Dismissed Show dismissed Hide dismissed
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var d = PDFJSDev.test('FALSE');
var e = PDFJSDev.eval('TRUE');
var f = PDFJSDev.eval('TEXT');
var g = PDFJSDev.eval('OBJ');
var h = PDFJSDev.json('$ROOT/external/builder/fixtures_esprima/evals.json');
var h = PDFJSDev.json('$ROOT/external/builder/fixtures_babel/evals.json');
Dismissed Show dismissed Hide dismissed
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
var k = !PDFJSDev.test('TRUE');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

let errors = 0;

const baseDir = path.join(__dirname, "fixtures_esprima");
const baseDir = path.join(__dirname, "fixtures_babel");
const files = fs
.readdirSync(baseDir)
.filter(function (name) {
Expand Down Expand Up @@ -49,7 +49,7 @@ files.forEach(function (expectationFilename) {
errors++;

// Allow regenerating the expected output using
// OVERWRITE=true node ./external/builder/test-fixtures_esprima.mjs
// OVERWRITE=true node ./external/builder/test-fixtures_babel.mjs
if (process.env.OVERWRITE) {
fs.writeFileSync(expectationFilename, out + "\n");
}
Expand Down
4 changes: 2 additions & 2 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2414,8 +2414,8 @@ gulp.task("externaltest", function (done) {
});

console.log();
console.log("### Running test-fixtures_esprima.js");
safeSpawnSync("node", ["external/builder/test-fixtures_esprima.mjs"], {
console.log("### Running test-fixtures_babel.js");
safeSpawnSync("node", ["external/builder/test-fixtures_babel.mjs"], {
stdio: "inherit",
});
done();
Expand Down