Skip to content

Commit

Permalink
Update the Babel plugin to remove empty class constructors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolo-ribaudo committed May 9, 2024
1 parent 83d878d commit 5cdc965
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
18 changes: 18 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,24 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
}
},
},
ClassMethod(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
17 changes: 17 additions & 0 deletions external/builder/fixtures_babel/constructors-expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class A {
constructor() {
console.log("Hi!");
}
}
class B {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class B.
constructor(x = console.log("Hi!")) {}
}
class C {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class C.
constructor({
x
}) {}
}
class D {}

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class D.
class E extends A {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class E.
constructor() {}
}
21 changes: 21 additions & 0 deletions external/builder/fixtures_babel/constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class A {
constructor() {
console.log("Hi!");
}
}

class B {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class B.
constructor(x = console.log("Hi!")) {}
}

class C {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class C.
constructor({ x }) {}
}

class D {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class D.
constructor(x, y, z) {}
}

class E extends A {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class E.
constructor() {}
}

0 comments on commit 5cdc965

Please sign in to comment.