diff --git a/lib/6to5/transformers/computed-property-names.js b/lib/6to5/transformers/computed-property-names.js new file mode 100644 index 0000000000..df85c79869 --- /dev/null +++ b/lib/6to5/transformers/computed-property-names.js @@ -0,0 +1,32 @@ +var util = require("../util"); +var _ = require("lodash"); + +exports.ObjectExpression = function (node) { + var hasComputed = false; + var computed = []; + + node.properties = node.properties.filter(function (prop) { + if (prop.computed) { + hasComputed = true; + computed.unshift(prop); + return false; + } else { + return true; + } + }); + + if (!hasComputed) return; + + var container = util.template("function-return-obj", { + OBJECT: node + }); + + _.each(computed, function (prop) { + container.callee.body.body.unshift(util.template("obj-key-set", { + KEY: prop.key, + VALUE: prop.value + }, true)); + }); + + return container; +}; diff --git a/test/fixtures/computed-property-names/mixed/actual.js b/test/fixtures/computed-property-names/mixed/actual.js new file mode 100644 index 0000000000..17e110ad3d --- /dev/null +++ b/test/fixtures/computed-property-names/mixed/actual.js @@ -0,0 +1,6 @@ +var obj = { + ["x" + foo]: "heh", + ["y" + bar]: "noo", + foo: "foo", + bar: "bar" +}; diff --git a/test/fixtures/computed-property-names/mixed/expected.js b/test/fixtures/computed-property-names/mixed/expected.js new file mode 100644 index 0000000000..706bd93908 --- /dev/null +++ b/test/fixtures/computed-property-names/mixed/expected.js @@ -0,0 +1,8 @@ +var obj = function (obj) { + obj["x" + foo] = "heh"; + obj["y" + bar] = "noo"; + return obj; +}({ + foo: "foo", + bar: "bar" +}); diff --git a/test/fixtures/computed-property-names/multiple/actual.js b/test/fixtures/computed-property-names/multiple/actual.js new file mode 100644 index 0000000000..02d4195fa7 --- /dev/null +++ b/test/fixtures/computed-property-names/multiple/actual.js @@ -0,0 +1,4 @@ +var obj = { + ["x" + foo]: "heh", + ["y" + bar]: "noo" +}; diff --git a/test/fixtures/computed-property-names/multiple/expected.js b/test/fixtures/computed-property-names/multiple/expected.js new file mode 100644 index 0000000000..394266087c --- /dev/null +++ b/test/fixtures/computed-property-names/multiple/expected.js @@ -0,0 +1,5 @@ +var obj = function (obj) { + obj["x" + foo] = "heh"; + obj["y" + bar] = "noo"; + return obj; +}({}); diff --git a/test/fixtures/computed-property-names/single/actual.js b/test/fixtures/computed-property-names/single/actual.js new file mode 100644 index 0000000000..04ec89b5ce --- /dev/null +++ b/test/fixtures/computed-property-names/single/actual.js @@ -0,0 +1,3 @@ +var obj = { + ["x" + foo]: "heh" +}; diff --git a/test/fixtures/computed-property-names/single/expected.js b/test/fixtures/computed-property-names/single/expected.js new file mode 100644 index 0000000000..d71682a31a --- /dev/null +++ b/test/fixtures/computed-property-names/single/expected.js @@ -0,0 +1,4 @@ +var obj = function (obj) { + obj["x" + foo] = "heh"; + return obj; +}({});