From 2b458ec2d4358e78ce25a481a14aa083cf18b592 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 14 Nov 2014 00:53:10 +1100 Subject: [PATCH] make it illegal to use destructuring outside of an ExpressionStatement --- lib/6to5/transformation/transformers/for-of.js | 10 +++++----- .../transformation/for-of/illegal-left/actual.js | 3 +++ .../transformation/for-of/illegal-left/options.json | 3 +++ 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/transformation/for-of/illegal-left/actual.js create mode 100644 test/fixtures/transformation/for-of/illegal-left/options.json diff --git a/lib/6to5/transformation/transformers/for-of.js b/lib/6to5/transformation/transformers/for-of.js index a1905fb458..4c8d52da85 100644 --- a/lib/6to5/transformation/transformers/for-of.js +++ b/lib/6to5/transformation/transformers/for-of.js @@ -5,17 +5,17 @@ exports.ForOfStatement = function (node, parent, file, scope) { var left = node.left; var declar; - var stepKey = t.identifier(file.generateUid("step", scope)); - var stepValueId = t.memberExpression(stepKey, t.identifier("value")); + var stepKey = t.identifier(file.generateUid("step", scope)); + var stepValue = t.memberExpression(stepKey, t.identifier("value")); if (t.isIdentifier(left)) { - declar = t.expressionStatement(t.assignmentExpression("=", left, stepValueId)); + declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue)); } else if (t.isVariableDeclaration(left)) { declar = t.variableDeclaration(left.kind, [ - t.variableDeclarator(left.declarations[0].id, stepValueId) + t.variableDeclarator(left.declarations[0].id, stepValue) ]); } else { - return; + throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement"); } var node2 = util.template("for-of", { diff --git a/test/fixtures/transformation/for-of/illegal-left/actual.js b/test/fixtures/transformation/for-of/illegal-left/actual.js new file mode 100644 index 0000000000..cf3e981111 --- /dev/null +++ b/test/fixtures/transformation/for-of/illegal-left/actual.js @@ -0,0 +1,3 @@ +for (foo.bar of test) { + +} diff --git a/test/fixtures/transformation/for-of/illegal-left/options.json b/test/fixtures/transformation/for-of/illegal-left/options.json new file mode 100644 index 0000000000..22d8ac64c9 --- /dev/null +++ b/test/fixtures/transformation/for-of/illegal-left/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unknown node type MemberExpression in ForOfStatement" +}