[Pkg-mozext-commits] [adblock-plus] 425/464: Issue 301 - jshydra: Handle for .. of ..
David Prévot
taffit at moszumanska.debian.org
Tue Jul 22 20:44:40 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 6c70e9f94e5f1953a6d00f17fc1fa6ec84cf3cf8
Author: Tom Schuster <tschuster at adblockplus.org>
Date: Mon Apr 14 18:39:28 2014 +0200
Issue 301 - jshydra: Handle for .. of ..
---
autotest/abprewrite_source.js | 10 ++++---
jshydra.js | 2 +-
scripts/abprewrite.js | 62 ++++++++++++++++++++++++-------------------
scripts/astDecompile.js | 11 ++++++++
4 files changed, 52 insertions(+), 33 deletions(-)
diff --git a/autotest/abprewrite_source.js b/autotest/abprewrite_source.js
index dec712d..14bb25d 100644
--- a/autotest/abprewrite_source.js
+++ b/autotest/abprewrite_source.js
@@ -20,20 +20,22 @@ let [a, b] = foo();
let {x: y} = foo();
let {k1: v1, k2: v2} = foo();
-for each (let v in fooList)
+
+for (let v of fooList)
alert(v);
-for each (let [a, b] in fooList)
+for (let [a, b] of fooList)
{
a += b;
b -= a;
}
-for each (let [a, b] in fooList);
+for (let [a, b] of fooList);
-for each (k in fooList)
+for (k of fooList)
alert(k);
+
let a = function() 1;
let b = {
get foo() 1
diff --git a/jshydra.js b/jshydra.js
index 046bb65..588d472 100644
--- a/jshydra.js
+++ b/jshydra.js
@@ -40,4 +40,4 @@ var _print = print;
for (var i = 1; i < scriptArgs.length; i++)
process_js(Reflect.parse(read(scriptArgs[i])), scriptArgs[i], scriptArg);
-})(arguments);
+})(this.arguments || this.scriptArgs);
diff --git a/scripts/abprewrite.js b/scripts/abprewrite.js
index 71ce4d1..8ca06ba 100644
--- a/scripts/abprewrite.js
+++ b/scripts/abprewrite.js
@@ -320,33 +320,8 @@ function modifyForInStatement(ast)
{
if (ast.each)
{
- // Convert "for each" loops:
- // for each (var foo in fooList)
- // {
- // ...
- // }
- //
- // Change into:
- // for (var _loopIndex44 = 0; _loopIndex44 < fooList.length; ++_loopIndex44)
- // {
- // var foo = fooList[_loopIndex44];
- // ...
- // }
- let loopIndex = Identifier("_loopIndex" + options.varIndex++);
-
- let block = ensureBlock(ast.body);
- if (ast.left.type == "VariableDeclaration")
- block.body.unshift(VariableDeclaration(ast.left.declarations[0].id, Member(ast.right, loopIndex, true)));
- else
- block.body.unshift(Assignment(ast.left, Member(ast.right, loopIndex, true)));
-
- return {
- type: "ForStatement",
- init: VariableDeclaration(loopIndex, 0),
- test: LogicalExpression(loopIndex, "<", Member(ast.right, "length", false)),
- update: IncExpression(loopIndex),
- body: block
- };
+ print("Use for (.. of ..) instead of for each (..)!\n");
+ throw new Error("Use for (.. of ..) instead of for each (..)!");
}
// Make sure that the loop body is always wrapped in a block
@@ -355,6 +330,37 @@ function modifyForInStatement(ast)
return ast;
}
+function modifyForOfStatement(ast)
+{
+ // Convert "for of" loops:
+ // for (var foo of fooList)
+ // {
+ // ...
+ // }
+ //
+ // Change into:
+ // for (var _loopIndex44 = 0; _loopIndex44 < fooList.length; ++_loopIndex44)
+ // {
+ // var foo = fooList[_loopIndex44];
+ // ...
+ // }
+ let loopIndex = Identifier("_loopIndex" + options.varIndex++);
+
+ let block = ensureBlock(ast.body);
+ if (ast.left.type == "VariableDeclaration")
+ block.body.unshift(VariableDeclaration(ast.left.declarations[0].id, Member(ast.right, loopIndex, true)));
+ else
+ block.body.unshift(Assignment(ast.left, Member(ast.right, loopIndex, true)));
+
+ return {
+ type: "ForStatement",
+ init: VariableDeclaration(loopIndex, 0),
+ test: LogicalExpression(loopIndex, "<", Member(ast.right, "length", false)),
+ update: IncExpression(loopIndex),
+ body: block
+ };
+}
+
function modifyLetStatement(ast)
{
if (ast.body.type == "ForStatement" && ast.body.init == null)
@@ -475,7 +481,7 @@ function modifyYieldExpression(ast)
process_js = function(ast, filename, args)
{
- for each (let arg in args.split(/\s+/))
+ for (let arg of args.split(/\s+/))
{
let match = /^(\w+)\s*=\s*(.*)/.exec(arg);
if (match && typeof options[match[1]] == "boolean")
diff --git a/scripts/astDecompile.js b/scripts/astDecompile.js
index 2a7b18d..dc68d21 100644
--- a/scripts/astDecompile.js
+++ b/scripts/astDecompile.js
@@ -134,6 +134,17 @@ function decompileForInStatement(ast) {
return str;
}
+function decompileForOfStatement(ast) {
+ let str = "for (";
+ if (ast.left.type == "VariableDeclaration")
+ str += decompileVariableDeclaration(ast.left, true);
+ else
+ str += decompileAST(ast.left);
+ str += " of " + decompileExpr(ast.right, ast) + ") ";
+ str += decompileAST(ast.body);
+ return str;
+}
+
function decompileLetStatement(ast) {
let str = "let (";
str += [d ? decompileAST(d) : ' ' for each (d in ast.head)].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