diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 7a5a36fa77..4e2cd9379a 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -39,6 +39,8 @@ transform.moduleFormatters = { }; _.each({ + specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"), + // playground methodBinding: require("./transformers/playground-method-binding"), memoizationOperator: require("./transformers/playground-memoization-operator"), diff --git a/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js new file mode 100644 index 0000000000..5af12108f1 --- /dev/null +++ b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js @@ -0,0 +1,10 @@ +var t = require("../../types"); + +exports.ForInStatement = +exports.ForOfStatement = function (node, parent, file) { + var left = node.left; + if (t.isVariableDeclaration(left)) { + var declar = left.declarations[0]; + if (declar.init) throw file.errorWithNode(declar, "No assignments allowed in for-in/of head"); + } +}; diff --git a/lib/6to5/util.js b/lib/6to5/util.js index c291d12abd..85368e6f58 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -146,10 +146,10 @@ exports.template = function (name, nodes, keepExpression) { var node = template.body[0]; if (!keepExpression && t.isExpressionStatement(node)) { - node = node.expression; + return node.expression; + } else { + return node; } - - return node; }; exports.codeFrame = function (lines, lineNumber, colNumber) { diff --git a/test/fixtures/transformation/spec/for-in-assignment/actual.js b/test/fixtures/transformation/spec/for-in-assignment/actual.js new file mode 100644 index 0000000000..167407a7da --- /dev/null +++ b/test/fixtures/transformation/spec/for-in-assignment/actual.js @@ -0,0 +1,3 @@ +for (var i = 0 in obj) { + +} diff --git a/test/fixtures/transformation/spec/for-in-assignment/options.json b/test/fixtures/transformation/spec/for-in-assignment/options.json new file mode 100644 index 0000000000..c7ae9b8aa4 --- /dev/null +++ b/test/fixtures/transformation/spec/for-in-assignment/options.json @@ -0,0 +1,3 @@ +{ + "throws": "No assignments allowed in for-in/of head" +} diff --git a/test/fixtures/transformation/spec/for-of-assignment/actual.js b/test/fixtures/transformation/spec/for-of-assignment/actual.js new file mode 100644 index 0000000000..56e4dfef82 --- /dev/null +++ b/test/fixtures/transformation/spec/for-of-assignment/actual.js @@ -0,0 +1,3 @@ +for (var i = 0 of obj) { + +} diff --git a/test/fixtures/transformation/spec/for-of-assignment/options.json b/test/fixtures/transformation/spec/for-of-assignment/options.json new file mode 100644 index 0000000000..c7ae9b8aa4 --- /dev/null +++ b/test/fixtures/transformation/spec/for-of-assignment/options.json @@ -0,0 +1,3 @@ +{ + "throws": "No assignments allowed in for-in/of head" +}