Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: scripts/abprewrite.js

Issue 5417063522762752: Issue 427 - Desugar arrow functions to normal or bound functions (Closed)
Left Patch Set: Created May 10, 2014, 12:46 p.m.
Right Patch Set: I just decided to ignore eval and Function for now, I can open a new bug if we really want to ban t… Created May 18, 2014, 10:55 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « autotest/test_abprewrite_module.js.expected ('k') | scripts/astDecompile.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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)) {
Wladimir Palant 2014/05/15 06:53:44 I think this would be better here: if (/\b(this
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
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 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld