[Pkg-mozext-commits] [adblock-plus] 434/464: Issue 427 - Desugar arrow functions to normal or bound functions
David Prévot
taffit at moszumanska.debian.org
Tue Jul 22 20:44:42 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository adblock-plus.
commit 671ce37115a918bb7b5f6b120ea5a8e55ada0b14
Author: Tom Schuster <tschuster at adblockplus.org>
Date: Mon Jun 2 13:57:43 2014 +0200
Issue 427 - Desugar arrow functions to normal or bound functions
---
autotest/abprewrite_source.js | 9 ++++-
autotest/test_abprewrite.js.expected | 19 +++++++++
autotest/test_abprewrite_module.js.expected | 19 +++++++++
scripts/abprewrite.js | 61 ++++++++++++++++++++---------
scripts/astDecompile.js | 6 +++
5 files changed, 94 insertions(+), 20 deletions(-)
diff --git a/autotest/abprewrite_source.js b/autotest/abprewrite_source.js
index e371291..7f59f7b 100644
--- a/autotest/abprewrite_source.js
+++ b/autotest/abprewrite_source.js
@@ -36,9 +36,9 @@ for (k of fooList)
alert(k);
-let a = function() 1;
+let a = function() { return 1; };
let b = {
- get foo() 1
+ get foo() { return 1; }
};
function foo()
@@ -80,3 +80,8 @@ var a = function()
yield i;
}
};
+
+let x = (y) => y + 1;
+x = y => y + 1;
+x = (a, b) => this[a] + b;
+x = (a, b) => { return () => 1; }
\ No newline at end of file
diff --git a/autotest/test_abprewrite.js.expected b/autotest/test_abprewrite.js.expected
index 49d5e12..718c027 100644
--- a/autotest/test_abprewrite.js.expected
+++ b/autotest/test_abprewrite.js.expected
@@ -131,3 +131,22 @@ var a = function()
}
return _generatorResult10;
};
+var x = function(y)
+{
+ return y + 1;
+};
+x = function(y)
+{
+ return y + 1;
+};
+x = function(a, b)
+{
+ return this[a] + b;
+}.bind(this);
+x = function(a, b)
+{
+ return function()
+ {
+ return 1;
+ };
+}.bind(this);
diff --git a/autotest/test_abprewrite_module.js.expected b/autotest/test_abprewrite_module.js.expected
index 5d23db2..9817960 100644
--- a/autotest/test_abprewrite_module.js.expected
+++ b/autotest/test_abprewrite_module.js.expected
@@ -134,5 +134,24 @@ require.scopes["abprewrite_source"] = (function()
}
return _generatorResult10;
};
+ var x = function(y)
+ {
+ return y + 1;
+ };
+ x = function(y)
+ {
+ return y + 1;
+ };
+ x = function(a, b)
+ {
+ return this[a] + b;
+ }.bind(this);
+ x = function(a, b)
+ {
+ return function()
+ {
+ return 1;
+ };
+ }.bind(this);
return exports;
})();
diff --git a/scripts/abprewrite.js b/scripts/abprewrite.js
index 3b66b1b..5b84720 100644
--- a/scripts/abprewrite.js
+++ b/scripts/abprewrite.js
@@ -396,24 +396,8 @@ function modifyFunctionExpression(ast)
{
if (ast.expression)
{
- // Convert expression closures:
- // function() foo;
- //
- // Change into:
- // function()
- // {
- // return foo;
- // }
- ast.expression = false;
- ast.body = {
- type: "BlockStatement",
- body: [
- {
- type: "ReturnStatement",
- argument: ast.body
- }
- ]
- };
+ print("Use arrow functions instead of function expressions!\n");
+ throw new Error("Use arrow functions instead of function expressions!");
}
if (ast.generator)
@@ -459,6 +443,47 @@ function modifyFunctionDeclaration(ast)
return modifyFunctionExpression(ast);
}
+function modifyArrowExpression(ast)
+{
+ if (ast.body.type != "BlockStatement") {
+ // Convert expressions to block statements.
+ // (a) => a + 1
+ //
+ // Change into:
+ // (a) => { return a + 1; }
+ ast.body = {
+ type: "BlockStatement",
+ body: [
+ {
+ type: "ReturnStatement",
+ argument: ast.body
+ }
+ ]
+ };
+ }
+
+ // Now convert the arrow expression into a normal
+ // function expression.
+ ast.type = "FunctionExpression";
+ ast.expression = false;
+
+ // We want to avoid the .bind call if |this| isn't actually used.
+ // This is not necessarily always correct, but should be good enough.
+ var body = decompileAST(ast.body);
+ if (/((\bthis\b)|=>)/.test(body)) {
+ return {
+ type: "CallExpression",
+ callee: Member(ast, "bind"),
+ arguments: [
+ {
+ type: "ThisExpression"
+ }
+ ]
+ };
+ }
+ return ast;
+}
+
function modifyYieldExpression(ast)
{
// Convert generators into functions returning arrays:
diff --git a/scripts/astDecompile.js b/scripts/astDecompile.js
index dc68d21..49143a6 100644
--- a/scripts/astDecompile.js
+++ b/scripts/astDecompile.js
@@ -249,6 +249,12 @@ function decompileFunctionExpression(ast) {
return decompileFunctionDeclaration(ast);
}
+function decompileArrowExpression(ast) {
+ let str = "(" + ast.params.map(decompileAST).join(", ") + ")";
+ str += " => " + decompileAST(ast.body);
+ return str;
+}
+
function decompileSequenceExpression(ast) {
return "(" + [decompileExpr(e, ast) for each (e in ast.expressions)].join(", ") + ")";
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git
More information about the Pkg-mozext-commits
mailing list