Remove empty, top-level, nodes in the Babel plugin

Looking at the *built* files you'll notice some lines containing nothing more than a semicolon. This is the result of (mostly top-level) `if`-statements, which include `PDFJSDev`-checks, that evalute to `false` during Babel parsing.

This has always annoyed me a bit, and looking at Babel plugin it seems that we can fix this simply by *removing* the relevant nodes.
This commit is contained in:
Jonas Jenwald 2024-02-09 13:58:24 +01:00
parent 964bfe522b
commit 14ef0b4211
3 changed files with 19 additions and 7 deletions

View File

@ -84,11 +84,13 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
if (t.isBooleanLiteral(node.test)) { if (t.isBooleanLiteral(node.test)) {
// if (true) stmt1; => stmt1 // if (true) stmt1; => stmt1
// if (false) stmt1; else stmt2; => stmt2 // if (false) stmt1; else stmt2; => stmt2
path.replaceWith( if (node.test.value === true) {
node.test.value === true path.replaceWith(node.consequent);
? node.consequent } else if (node.alternate) {
: node.alternate || t.emptyStatement() path.replaceWith(node.alternate);
); } else {
path.remove(node);
}
} }
}, },
}, },

View File

@ -7,11 +7,12 @@ if ('test') {
{ {
"1"; "1";
} }
;
{ {
"2"; "2";
} }
;
if ('1') { if ('1') {
"1"; "1";
} }
function f1() {
"1";
}

View File

@ -23,3 +23,12 @@ if (true && false) {
if (true && false || '1') { if (true && false || '1') {
"1"; "1";
} }
function f1() {
if (true) {
"1";
}
if (false) {
"2";
}
}