From 995f5a29aa9c147d129ea3b55d4ab1b4be38ae51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 5 Aug 2024 14:35:54 +0200 Subject: [PATCH] Update the Babel plugin to remove empty static blocks 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. --- .../babel-plugin-pdfjs-preprocessor.mjs | 6 +++++- .../fixtures_babel/staticblock-expected.js | 8 ++++++++ .../builder/fixtures_babel/staticblock.js | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 external/builder/fixtures_babel/staticblock-expected.js create mode 100644 external/builder/fixtures_babel/staticblock.js diff --git a/external/builder/babel-plugin-pdfjs-preprocessor.mjs b/external/builder/babel-plugin-pdfjs-preprocessor.mjs index 8c5254e7374c8..8f8be7a724e75 100644 --- a/external/builder/babel-plugin-pdfjs-preprocessor.mjs +++ b/external/builder/babel-plugin-pdfjs-preprocessor.mjs @@ -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) { @@ -215,6 +215,10 @@ function babelPluginPDFJSPreprocessor(babel, ctx) { } subExpressionIndex++; } + + if (node.type === "StaticBlock" && node.body.length === 0) { + path.remove(); + } }, }, Function: { diff --git a/external/builder/fixtures_babel/staticblock-expected.js b/external/builder/fixtures_babel/staticblock-expected.js new file mode 100644 index 0000000000000..7edf5a8d902ba --- /dev/null +++ b/external/builder/fixtures_babel/staticblock-expected.js @@ -0,0 +1,8 @@ +class A { + static { + foo(); + } + static { + var a = 0; + } +} diff --git a/external/builder/fixtures_babel/staticblock.js b/external/builder/fixtures_babel/staticblock.js new file mode 100644 index 0000000000000..161a5d7058886 --- /dev/null +++ b/external/builder/fixtures_babel/staticblock.js @@ -0,0 +1,20 @@ +class A { + static {} + static { + { foo() } + } + static { + {;} + } + static { + if (PDFJSDev.test('TRUE')) { + var a = 0; + } + } + + static { + if (PDFJSDev.test('FALSE')) { + var a = 1; + } + } +}