From 99fb31638e79bd8b54039e6c07ae5cbabec103f2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 29 Sep 2014 03:43:21 +1000 Subject: [PATCH] add computed property names support --- .../transformers/computed-property-names.js | 32 +++++++++++++++++++ .../computed-property-names/mixed/actual.js | 6 ++++ .../computed-property-names/mixed/expected.js | 8 +++++ .../multiple/actual.js | 4 +++ .../multiple/expected.js | 5 +++ .../computed-property-names/single/actual.js | 3 ++ .../single/expected.js | 4 +++ 7 files changed, 62 insertions(+) create mode 100644 lib/6to5/transformers/computed-property-names.js create mode 100644 test/fixtures/computed-property-names/mixed/actual.js create mode 100644 test/fixtures/computed-property-names/mixed/expected.js create mode 100644 test/fixtures/computed-property-names/multiple/actual.js create mode 100644 test/fixtures/computed-property-names/multiple/expected.js create mode 100644 test/fixtures/computed-property-names/single/actual.js create mode 100644 test/fixtures/computed-property-names/single/expected.js 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; +}({});