diff --git a/lib/6to5/transformation/transformers/es6-computed-property-names.js b/lib/6to5/transformation/transformers/es6-computed-property-names.js index 3a559177df..bc3c9091f2 100644 --- a/lib/6to5/transformation/transformers/es6-computed-property-names.js +++ b/lib/6to5/transformation/transformers/es6-computed-property-names.js @@ -4,26 +4,62 @@ exports.ObjectExpression = function (node, parent, file, scope) { var hasComputed = false; var prop; var key; - var i; - for (i in node.properties) { + for (var i in node.properties) { hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" }); if (hasComputed) break; } if (!hasComputed) return; + var initProps = []; var objId = scope.generateUidBasedOnNode(parent, file); + // + var body = []; var container = t.functionExpression(null, [], t.blockStatement(body)); container._aliasFunction = true; + // + + var callback = spec; + if (file.isLoose("computedPropertyNames")) callback = loose; + + var result = callback(node, body, objId, initProps, file); + if (result) return result; + + // + + body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(objId, t.objectExpression(initProps)) + ])); + + body.push(t.returnStatement(objId)); + + return t.callExpression(container, []); +}; + +var loose = function (node, body, objId) { + for (var i in node.properties) { + var prop = node.properties[i]; + + body.push(t.expressionStatement( + t.assignmentExpression( + "=", + t.memberExpression(objId, prop.key, prop.computed), + prop.value + ) + )); + } +}; + +var spec = function (node, body, objId, initProps, file) { var props = node.properties; // normalise key - for (i in props) { + for (var i in props) { prop = props[i]; if (prop.kind !== "init") continue; @@ -36,7 +72,6 @@ exports.ObjectExpression = function (node, parent, file, scope) { // add all non-computed properties and `__proto__` properties to the initializer - var initProps = []; var broken = false; for (i in props) { @@ -86,14 +121,4 @@ exports.ObjectExpression = function (node, parent, file, scope) { return first; } } - - // - - body.unshift(t.variableDeclaration("var", [ - t.variableDeclarator(objId, t.objectExpression(initProps)) - ])); - - body.push(t.returnStatement(objId)); - - return t.callExpression(container, []); }; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/argument/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/argument/actual.js new file mode 100644 index 0000000000..fc50c41c49 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/argument/actual.js @@ -0,0 +1,3 @@ +foo({ + [bar]: "foobar" +}); diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/argument/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/argument/expected.js new file mode 100644 index 0000000000..8a544ec30e --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/argument/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +foo((function () { + var _foo = {}; + + _foo[bar] = "foobar"; + return _foo; +})()); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/assignment/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/assignment/actual.js new file mode 100644 index 0000000000..dd74ed9f04 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/assignment/actual.js @@ -0,0 +1,3 @@ +foo = { + [bar]: "foobar" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/assignment/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/assignment/expected.js new file mode 100644 index 0000000000..2b1f23d706 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/assignment/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +foo = (function () { + var _foo = {}; + + _foo[bar] = "foobar"; + return _foo; +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/actual.js new file mode 100644 index 0000000000..fce8293ed4 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/actual.js @@ -0,0 +1,3 @@ +var foo = { + [Symbol.iterator]: "foobar" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/expected.js new file mode 100644 index 0000000000..852e2b4e6c --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +var foo = (function () { + var _foo = {}; + + _foo[Symbol.iterator] = "foobar"; + return _foo; +})(); diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/method/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/method/actual.js new file mode 100644 index 0000000000..068edebd55 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/method/actual.js @@ -0,0 +1,5 @@ +var obj = { + [foobar]() { + return "foobar"; + } +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/method/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/method/expected.js new file mode 100644 index 0000000000..da3d3d08e2 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/method/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +var obj = (function () { + var _obj = {}; + + _obj[foobar] = function () { + return "foobar"; + }; + + return _obj; +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/mixed/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/mixed/actual.js new file mode 100644 index 0000000000..17e110ad3d --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/mixed/actual.js @@ -0,0 +1,6 @@ +var obj = { + ["x" + foo]: "heh", + ["y" + bar]: "noo", + foo: "foo", + bar: "bar" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/mixed/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/mixed/expected.js new file mode 100644 index 0000000000..7728ebd72c --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/mixed/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +var obj = (function () { + var _obj = {}; + + _obj["x" + foo] = "heh"; + _obj["y" + bar] = "noo"; + _obj.foo = "foo"; + _obj.bar = "bar"; + return _obj; +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/multiple/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/multiple/actual.js new file mode 100644 index 0000000000..02d4195fa7 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/multiple/actual.js @@ -0,0 +1,4 @@ +var obj = { + ["x" + foo]: "heh", + ["y" + bar]: "noo" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/multiple/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/multiple/expected.js new file mode 100644 index 0000000000..fb1c4ceb31 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/multiple/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var obj = (function () { + var _obj = {}; + + _obj["x" + foo] = "heh"; + _obj["y" + bar] = "noo"; + return _obj; +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/options.json b/test/fixtures/transformation/es6-computed-property-names-loose/options.json new file mode 100644 index 0000000000..422293bd82 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/options.json @@ -0,0 +1,3 @@ +{ + "loose": ["computedPropertyNames"] +} diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/single/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/single/actual.js new file mode 100644 index 0000000000..04ec89b5ce --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/single/actual.js @@ -0,0 +1,3 @@ +var obj = { + ["x" + foo]: "heh" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/single/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/single/expected.js new file mode 100644 index 0000000000..627faff494 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/single/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +var obj = (function () { + var _obj = {}; + + _obj["x" + foo] = "heh"; + return _obj; +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/this/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/this/actual.js new file mode 100644 index 0000000000..3e3a34ef12 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/this/actual.js @@ -0,0 +1,3 @@ +var obj = { + ["x" + this.foo]: "heh" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/this/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/this/expected.js new file mode 100644 index 0000000000..23b5ee17e4 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/this/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _this = this; +var obj = (function () { + var _obj = {}; + + _obj["x" + _this.foo] = "heh"; + return _obj; +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/two/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/two/actual.js new file mode 100644 index 0000000000..c63046dd19 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/two/actual.js @@ -0,0 +1,4 @@ +var obj = { + first: "first", + ["second"]: "second", +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/two/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/two/expected.js new file mode 100644 index 0000000000..42ef0c1e64 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/two/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var obj = (function () { + var _obj = {}; + + _obj.first = "first"; + _obj.second = "second"; + return _obj; +})(); diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/variable/actual.js b/test/fixtures/transformation/es6-computed-property-names-loose/variable/actual.js new file mode 100644 index 0000000000..13d8e7f24a --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/variable/actual.js @@ -0,0 +1,3 @@ +var foo = { + [bar]: "foobar" +}; diff --git a/test/fixtures/transformation/es6-computed-property-names-loose/variable/expected.js b/test/fixtures/transformation/es6-computed-property-names-loose/variable/expected.js new file mode 100644 index 0000000000..6f691f0fd6 --- /dev/null +++ b/test/fixtures/transformation/es6-computed-property-names-loose/variable/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +var foo = (function () { + var _foo = {}; + + _foo[bar] = "foobar"; + return _foo; +})();