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:
parent
83d878d34c
commit
46626ac64a
3 changed files with 80 additions and 0 deletions
|
@ -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();
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
23
external/builder/fixtures_babel/constructors-expected.js
vendored
Normal file
23
external/builder/fixtures_babel/constructors-expected.js
vendored
Normal 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 {}
|
37
external/builder/fixtures_babel/constructors.js
vendored
Normal file
37
external/builder/fixtures_babel/constructors.js
vendored
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue