From 14ef0b4211de6d77504bd62ce470e88698e7ac81 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 9 Feb 2024 13:58:24 +0100 Subject: [PATCH] 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. --- external/builder/babel-plugin-pdfjs-preprocessor.mjs | 12 +++++++----- external/builder/fixtures_esprima/ifs-expected.js | 5 +++-- external/builder/fixtures_esprima/ifs.js | 9 +++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/external/builder/babel-plugin-pdfjs-preprocessor.mjs b/external/builder/babel-plugin-pdfjs-preprocessor.mjs index 65457f3f9bfcb..653c459fa6b38 100644 --- a/external/builder/babel-plugin-pdfjs-preprocessor.mjs +++ b/external/builder/babel-plugin-pdfjs-preprocessor.mjs @@ -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); + } } }, }, diff --git a/external/builder/fixtures_esprima/ifs-expected.js b/external/builder/fixtures_esprima/ifs-expected.js index 12599c6196753..db9f4af150fb0 100644 --- a/external/builder/fixtures_esprima/ifs-expected.js +++ b/external/builder/fixtures_esprima/ifs-expected.js @@ -7,11 +7,12 @@ if ('test') { { "1"; } -; { "2"; } -; if ('1') { "1"; } +function f1() { + "1"; +} diff --git a/external/builder/fixtures_esprima/ifs.js b/external/builder/fixtures_esprima/ifs.js index a2f3d217f98bc..187c7f93228b6 100644 --- a/external/builder/fixtures_esprima/ifs.js +++ b/external/builder/fixtures_esprima/ifs.js @@ -23,3 +23,12 @@ if (true && false) { if (true && false || '1') { "1"; } + +function f1() { + if (true) { + "1"; + } + if (false) { + "2"; + } +}