1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 14:48:08 +02:00

Merge pull request #18060 from nicolo-ribaudo/babel-plugin-class

Update the Babel plugin to remove empty class constructors
This commit is contained in:
Jonas Jenwald 2024-05-14 13:14:54 +02:00 committed by GitHub
commit 36a496b409
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 88 additions and 8 deletions

View file

@ -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/

View file

@ -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/

View file

@ -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/

View file

@ -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();
}
},
},
},
};
}

View file

@ -0,0 +1,23 @@
class A {
constructor() {
console.log("Hi!");
}
}
class B {
constructor(x = console.log("Hi!")) {}
}
class C {
constructor({
x
}) {}
}
class D {}
class E extends A {
constructor() {}
}
class F {
constructor() {
var a = 0;
}
}
class G {}

View file

@ -0,0 +1,37 @@
class A {
constructor() {
console.log("Hi!");
}
}
class B {
constructor(x = console.log("Hi!")) {}
}
class C {
constructor({ x }) {}
}
class D {
constructor(x, y, z) {}
}
class E extends A {
constructor() {}
}
class F {
constructor() {
if (PDFJSDev.test('TRUE')) {
var a = 0;
}
}
}
class G {
constructor() {
if (PDFJSDev.test('FALSE')) {
var a = 0;
}
}
}

View file

@ -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');
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
var k = !PDFJSDev.test('TRUE');

View file

@ -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) {
@ -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");
}

View file

@ -2408,8 +2408,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();