diff --git a/lib/6to5/transformers/constants.js b/lib/6to5/transformers/constants.js index 0d1801b56c..6df3a4dbfe 100644 --- a/lib/6to5/transformers/constants.js +++ b/lib/6to5/transformers/constants.js @@ -18,11 +18,33 @@ exports.ForStatement = function (node) { _.each(node.body, function (child) { if (child && child.type === "VariableDeclaration" && child.kind === "const") { _.each(child.declarations, function (declar) { - var name = declar.id.name; - check(declar, name); + var search = [declar.id]; + var names = []; + + while (search.length) { + var id = search.shift(); + + if (id.type === "Identifier") { + names.push(id.name); + } else if (id.type === "ArrayPattern") { + _.each(id.elements, function (elem) { + search.push(elem); + }); + } else if (id.type === "ObjectPattern") { + _.each(id.properties, function (prop) { + search.push(prop.value); + }); + } else { + throw new Error("unknown node " + id.type); + } + } + + _.each(names, function (name) { + check(declar, name); + constants.push(name); + }); declar._ignoreConstant = true; - constants.push(name); }); child.kind = "let"; } diff --git a/test/fixtures/syntax/constants/destructuring-assignment/actual.js b/test/fixtures/syntax/constants/destructuring-assignment/actual.js new file mode 100644 index 0000000000..b08c16fd90 --- /dev/null +++ b/test/fixtures/syntax/constants/destructuring-assignment/actual.js @@ -0,0 +1,2 @@ +const [a, b] = [1, 2]; +a = 3; diff --git a/test/fixtures/syntax/constants/destructuring-assignment/options.json b/test/fixtures/syntax/constants/destructuring-assignment/options.json new file mode 100644 index 0000000000..feeb18e277 --- /dev/null +++ b/test/fixtures/syntax/constants/destructuring-assignment/options.json @@ -0,0 +1,3 @@ +{ + "throws": "a is read-only" +} diff --git a/test/fixtures/syntax/constants/destructuring/actual.js b/test/fixtures/syntax/constants/destructuring/actual.js new file mode 100644 index 0000000000..85d5aa61a9 --- /dev/null +++ b/test/fixtures/syntax/constants/destructuring/actual.js @@ -0,0 +1,4 @@ +const [a, b] = [1, 2]; +const [c, d] = [3, 4]; +const { e, f } = { e: 5, f: 6 }; +const { a: g, b: h } = { a: 7, b: 8 }; diff --git a/test/fixtures/syntax/constants/destructuring/expected.js b/test/fixtures/syntax/constants/destructuring/expected.js new file mode 100644 index 0000000000..3e5d42e210 --- /dev/null +++ b/test/fixtures/syntax/constants/destructuring/expected.js @@ -0,0 +1,23 @@ +(function () { + var _ref = [1, 2]; + var a = _ref[0]; + var b = _ref[1]; + + var _ref2 = [3, 4]; + var c = _ref2[0]; + var d = _ref2[1]; + + var _ref3 = { + e: 5, + f: 6 + }; + var e = _ref3.e; + var f = _ref3.f; + + var _ref4 = { + a: 7, + b: 8 + }; + var g = _ref4.a; + var h = _ref4.b; +})();