LEFT | RIGHT |
1 // This script rewrites AST to be compatible with JavaScript 1.5 and decompiles | 1 // This script rewrites AST to be compatible with JavaScript 1.5 and decompiles |
2 // the modified tree then | 2 // the modified tree then |
3 | 3 |
4 include("../scripts/astDecompile.js"); | 4 include("../scripts/astDecompile.js"); |
5 include("../utils/beautify.js"); | 5 include("../utils/beautify.js"); |
6 | 6 |
7 let headerPrinted = false; | 7 let headerPrinted = false; |
8 | 8 |
9 // See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for | 9 // See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for |
10 // AST structure. | 10 // AST structure. |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 } | 463 } |
464 | 464 |
465 // Now convert the arrow expression into a normal | 465 // Now convert the arrow expression into a normal |
466 // function expression. | 466 // function expression. |
467 ast.type = "FunctionExpression"; | 467 ast.type = "FunctionExpression"; |
468 ast.expression = false; | 468 ast.expression = false; |
469 | 469 |
470 // We want to avoid the .bind call if |this| isn't actually used. | 470 // We want to avoid the .bind call if |this| isn't actually used. |
471 // This is not necessarily always correct, but should be good enough. | 471 // This is not necessarily always correct, but should be good enough. |
472 var body = decompileAST(ast.body); | 472 var body = decompileAST(ast.body); |
473 if (["this", "Function", "eval", "=>"].some(search => body.contains(search)))
{ | 473 if (/((\bthis\b)|=>)/.test(body)) { |
474 return { | 474 return { |
475 type: "CallExpression", | 475 type: "CallExpression", |
476 callee: Member(ast, "bind"), | 476 callee: Member(ast, "bind"), |
477 arguments: [ | 477 arguments: [ |
478 { | 478 { |
479 type: "ThisExpression" | 479 type: "ThisExpression" |
480 } | 480 } |
481 ] | 481 ] |
482 }; | 482 }; |
483 } | 483 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 let code = 'require.scopes["' + options.filename + '"] = (function() {\n' + | 568 let code = 'require.scopes["' + options.filename + '"] = (function() {\n' + |
569 'var exports = {};\n' + | 569 'var exports = {};\n' + |
570 decompileAST(ast) + | 570 decompileAST(ast) + |
571 'return exports;\n' + | 571 'return exports;\n' + |
572 '})();\n'; | 572 '})();\n'; |
573 _print(js_beautify(code, options)); | 573 _print(js_beautify(code, options)); |
574 } | 574 } |
575 else | 575 else |
576 _print(js_beautify(decompileAST(ast), options)); | 576 _print(js_beautify(decompileAST(ast), options)); |
577 } | 577 } |
LEFT | RIGHT |