Skip to content

Commit

Permalink
Merge pull request #17596 from nicolo-ribaudo/fix-babel-plugin
Browse files Browse the repository at this point in the history
Fix transform of unary expression in Babel plugin
  • Loading branch information
Snuffleupagus authored Jan 29, 2024
2 parents 802f702 + a352f28 commit 56dabe9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
27 changes: 16 additions & 11 deletions external/builder/babel-plugin-pdfjs-preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions external/builder/fixtures_esprima/evals-expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ var i = '0';
var j = {
i: 1
};
var k = false;
var l = true;
2 changes: 2 additions & 0 deletions external/builder/fixtures_esprima/evals.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ var g = PDFJSDev.eval('OBJ');
var h = PDFJSDev.json('$ROOT/external/builder/fixtures_esprima/evals.json');
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
var k = !PDFJSDev.test('TRUE');
var l = !PDFJSDev.test('FALSE');
2 changes: 1 addition & 1 deletion web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class ExternalServices extends BaseExternalServices {
}

async getNimbusExperimentData() {
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("GECKOVIEW")) {
if (!PDFJSDev.test("GECKOVIEW")) {
return null;
}
const nimbusData = await FirefoxCom.requestAsync(
Expand Down

0 comments on commit 56dabe9

Please sign in to comment.