Skip to content

Commit

Permalink
Merge pull request #17647 from Snuffleupagus/babel-rm-empty-nodes
Browse files Browse the repository at this point in the history
Remove empty, top-level, nodes in the Babel plugin
  • Loading branch information
Snuffleupagus authored Feb 12, 2024
2 parents 5cfaff5 + 14ef0b4 commit d742daf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
12 changes: 7 additions & 5 deletions external/builder/babel-plugin-pdfjs-preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
if (t.isBooleanLiteral(node.test)) {
// if (true) stmt1; => stmt1
// if (false) stmt1; else stmt2; => stmt2
path.replaceWith(
node.test.value === true
? node.consequent
: node.alternate || t.emptyStatement()
);
if (node.test.value === true) {
path.replaceWith(node.consequent);
} else if (node.alternate) {
path.replaceWith(node.alternate);
} else {
path.remove(node);
}
}
},
},
Expand Down
5 changes: 3 additions & 2 deletions external/builder/fixtures_esprima/ifs-expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ if ('test') {
{
"1";
}
;
{
"2";
}
;
if ('1') {
"1";
}
function f1() {
"1";
}
9 changes: 9 additions & 0 deletions external/builder/fixtures_esprima/ifs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ if (true && false) {
if (true && false || '1') {
"1";
}

function f1() {
if (true) {
"1";
}
if (false) {
"2";
}
}

0 comments on commit d742daf

Please sign in to comment.