Skip to content

Commit

Permalink
Update the Babel plugin to remove empty static blocks
Browse files Browse the repository at this point in the history
This commit updates the Babel plugin to:
- apply the same flattening logic that we already
  have for blocks, to flatten blocks nested inside
  class static blocks
- remove class static blocks when, after flattening
  all the blocks they contain, they are empty.

Before this commit, the transform output was the
same as the input.
  • Loading branch information
nicolo-ribaudo committed Aug 5, 2024
1 parent c60c0d1 commit 7513fb7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion external/builder/babel-plugin-pdfjs-preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
path.replaceWith(t.importExpression(source));
}
},
BlockStatement: {
"BlockStatement|StaticBlock": {
// Visit node in post-order so that recursive flattening
// of blocks works correctly.
exit(path) {
Expand Down Expand Up @@ -215,6 +215,10 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
}
subExpressionIndex++;
}

if (node.type === "StaticBlock" && node.body.length === 0) {
path.remove();
}
},
},
Function: {
Expand Down
5 changes: 5 additions & 0 deletions external/builder/fixtures_babel/staticblock-expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class A.
static {
foo();
}
}
9 changes: 9 additions & 0 deletions external/builder/fixtures_babel/staticblock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class A {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note library

Unused class A.
static {}
static {
{ foo() }
}
static {
{;}
}
}

0 comments on commit 7513fb7

Please sign in to comment.