mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Fix transform of unary expression in Babel plugin
All of our static evaluation & dead-code elimination transforms need to happen in post-order, transforming inner nodes first. This is so that in complex nested cases all transforms see the simplified version of their inner nodes. For example: async getNimbusExperimentData() { if (!PDFJSDev.test("GECKOVIEW")) { return null; } // other code } -> [evaluation of PDFJSDev.*] async getNimbusExperimentData() { if (!false) { return null; } // other code } -> [!false -> true] async getNimbusExperimentData() { if (true) { return null; } // other code } -> [if (true) -> replace with the if branch] async getNimbusExperimentData() { return null; // other code } -> [early return -> remove dead code] async getNimbusExperimentData() { return null; // other code } This was done correctly in all cases except for our `UnaryExpression` transform, which was happening in pre-order.
This commit is contained in:
parent
802f702695
commit
a352f28785
4 changed files with 21 additions and 12 deletions
|
@ -92,17 +92,22 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
|
|||
}
|
||||
},
|
||||
},
|
||||
UnaryExpression(path) {
|
||||
const { node } = path;
|
||||
if (node.operator === "typeof" && isPDFJSPreprocessor(node.argument)) {
|
||||
// typeof PDFJSDev => 'object'
|
||||
path.replaceWith(t.stringLiteral("object"));
|
||||
return;
|
||||
}
|
||||
if (node.operator === "!" && t.isBooleanLiteral(node.argument)) {
|
||||
// !true => false, !false => true
|
||||
path.replaceWith(t.booleanLiteral(!node.argument.value));
|
||||
}
|
||||
UnaryExpression: {
|
||||
exit(path) {
|
||||
const { node } = path;
|
||||
if (
|
||||
node.operator === "typeof" &&
|
||||
isPDFJSPreprocessor(node.argument)
|
||||
) {
|
||||
// typeof PDFJSDev => 'object'
|
||||
path.replaceWith(t.stringLiteral("object"));
|
||||
return;
|
||||
}
|
||||
if (node.operator === "!" && t.isBooleanLiteral(node.argument)) {
|
||||
// !true => false, !false => true
|
||||
path.replaceWith(t.booleanLiteral(!node.argument.value));
|
||||
}
|
||||
},
|
||||
},
|
||||
LogicalExpression: {
|
||||
exit(path) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue