1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 06:38:07 +02:00

Update the Babel plugin to remove empty class constructors

This only happens when it's safe to do so. The exceptions are:
- when the class extends another subclass: removing the constructor would remove the error about the missing super() call
- when there are default parameters, that could have side effects
- when there are destructured prameters, that could have side effects
This commit is contained in:
Nicolò Ribaudo 2024-05-09 15:11:59 +02:00
parent 83d878d34c
commit 46626ac64a
No known key found for this signature in database
GPG key ID: AAFDA9101C58F338
3 changed files with 80 additions and 0 deletions

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;
}
}
}