From 0f7da020e35af12a26b53e1f395dbbdc3ec4fbb4 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 12 Oct 2014 00:23:13 +1100 Subject: [PATCH] change obj references to a unique identifier --- lib/6to5/templates/function-return-obj-this.js | 4 ++-- lib/6to5/templates/function-return-obj.js | 4 ++-- lib/6to5/templates/obj-key-set.js | 2 +- lib/6to5/templates/object-define-properties-closure.js | 4 ++-- lib/6to5/transformers/computed-property-names.js | 7 ++++++- lib/6to5/transformers/for-of.js | 2 +- lib/6to5/transformers/property-method-assignment.js | 7 +++++-- lib/6to5/traverse/index.js | 4 +++- .../syntax/computed-property-names/method/expected.js | 6 +++--- .../syntax/computed-property-names/mixed/expected.js | 8 ++++---- .../syntax/computed-property-names/multiple/expected.js | 8 ++++---- .../syntax/computed-property-names/single/expected.js | 6 +++--- .../syntax/computed-property-names/this/expected.js | 6 +++--- .../getter-and-setter/expected.js | 6 +++--- .../syntax/property-methods-assignment/getter/expected.js | 6 +++--- .../syntax/property-methods-assignment/setter/expected.js | 6 +++--- 16 files changed, 48 insertions(+), 38 deletions(-) diff --git a/lib/6to5/templates/function-return-obj-this.js b/lib/6to5/templates/function-return-obj-this.js index 0a69bc5022..99b9dd6df4 100644 --- a/lib/6to5/templates/function-return-obj-this.js +++ b/lib/6to5/templates/function-return-obj-this.js @@ -1,3 +1,3 @@ -(function (obj) { - return obj; +(function (KEY) { + return KEY; }).call(this, OBJECT) diff --git a/lib/6to5/templates/function-return-obj.js b/lib/6to5/templates/function-return-obj.js index 2ccd4cd89c..4f54b679b9 100644 --- a/lib/6to5/templates/function-return-obj.js +++ b/lib/6to5/templates/function-return-obj.js @@ -1,3 +1,3 @@ -(function (obj) { - return obj; +(function (KEY) { + return KEY; })(OBJECT) diff --git a/lib/6to5/templates/obj-key-set.js b/lib/6to5/templates/obj-key-set.js index 3f37b446d3..6663140995 100644 --- a/lib/6to5/templates/obj-key-set.js +++ b/lib/6to5/templates/obj-key-set.js @@ -1 +1 @@ -obj[KEY] = VALUE; +OBJECT_KEY[KEY] = VALUE; diff --git a/lib/6to5/templates/object-define-properties-closure.js b/lib/6to5/templates/object-define-properties-closure.js index c09c638d3f..a7b90fbc8c 100644 --- a/lib/6to5/templates/object-define-properties-closure.js +++ b/lib/6to5/templates/object-define-properties-closure.js @@ -1,4 +1,4 @@ -(function (obj) { +(function (KEY) { CONTENT; - return obj; + return KEY; })(OBJECT); diff --git a/lib/6to5/transformers/computed-property-names.js b/lib/6to5/transformers/computed-property-names.js index 7d931f35b6..00dd77cbeb 100644 --- a/lib/6to5/transformers/computed-property-names.js +++ b/lib/6to5/transformers/computed-property-names.js @@ -1,8 +1,9 @@ var traverse = require("../traverse"); var util = require("../util"); +var b = require("ast-types").builders; var _ = require("lodash"); -exports.ObjectExpression = function (node) { +exports.ObjectExpression = function (node, parent, opts, generateUid) { var hasComputed = false; var hasThis = false; @@ -24,7 +25,10 @@ exports.ObjectExpression = function (node) { var templateName = "function-return-obj"; if (hasThis) templateName += "-this"; + var objId = b.identifier(generateUid("ref")); + var container = util.template(templateName, { + KEY: objId, OBJECT: node }); @@ -37,6 +41,7 @@ exports.ObjectExpression = function (node) { _.each(computed, function (prop) { containerBody.unshift(util.template("obj-key-set", { + OBJECT_KEY: objId, KEY: prop.key, VALUE: prop.value }, true)); diff --git a/lib/6to5/transformers/for-of.js b/lib/6to5/transformers/for-of.js index 30f4fd8d97..26f71b0700 100644 --- a/lib/6to5/transformers/for-of.js +++ b/lib/6to5/transformers/for-of.js @@ -22,7 +22,7 @@ exports.ForOfStatement = function (node, parent, opts, generateUid) { util.ensureBlock(node); var block = node2.body; - block.body = block.body.concat(node.body.body || []); + block.body = block.body.concat(node.body.body); var declar = block.body[0]; declar.kind = declar.kind; diff --git a/lib/6to5/transformers/property-method-assignment.js b/lib/6to5/transformers/property-method-assignment.js index 8ed7f130f2..a37e2f9f54 100644 --- a/lib/6to5/transformers/property-method-assignment.js +++ b/lib/6to5/transformers/property-method-assignment.js @@ -6,7 +6,7 @@ exports.Property = function (node) { if (node.method) node.method = false; }; -exports.ObjectExpression = function (node) { +exports.ObjectExpression = function (node, parent, opts, generateUid) { var mutatorMap = {}; node.properties = node.properties.filter(function (prop) { @@ -20,8 +20,11 @@ exports.ObjectExpression = function (node) { if (_.isEmpty(mutatorMap)) return; + var objId = b.identifier(generateUid("ref")) + return util.template("object-define-properties-closure", { + KEY: objId, OBJECT: node, - CONTENT: util.buildDefineProperties(mutatorMap, b.identifier("obj")).expression + CONTENT: util.buildDefineProperties(mutatorMap, objId).expression }); }; diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index de00dbd76b..6f125ea95e 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -11,7 +11,9 @@ var traverse = module.exports = function (parent, callbacks, blacklistTypes) { return; } - var keys = VISITOR_KEYS[parent.type] || []; + var keys = VISITOR_KEYS[parent.type]; + if (!keys) return; + blacklistTypes = blacklistTypes || []; if (_.isFunction(callbacks)) { diff --git a/test/fixtures/syntax/computed-property-names/method/expected.js b/test/fixtures/syntax/computed-property-names/method/expected.js index 983da7e15b..e229295c5b 100644 --- a/test/fixtures/syntax/computed-property-names/method/expected.js +++ b/test/fixtures/syntax/computed-property-names/method/expected.js @@ -1,6 +1,6 @@ -var obj = function (obj) { - obj[foobar] = function () { +var obj = function (_ref) { + _ref[foobar] = function () { return "foobar"; }; - return obj; + return _ref; }({}); diff --git a/test/fixtures/syntax/computed-property-names/mixed/expected.js b/test/fixtures/syntax/computed-property-names/mixed/expected.js index 706bd93908..5553d0d9ff 100644 --- a/test/fixtures/syntax/computed-property-names/mixed/expected.js +++ b/test/fixtures/syntax/computed-property-names/mixed/expected.js @@ -1,7 +1,7 @@ -var obj = function (obj) { - obj["x" + foo] = "heh"; - obj["y" + bar] = "noo"; - return obj; +var obj = function (_ref) { + _ref["x" + foo] = "heh"; + _ref["y" + bar] = "noo"; + return _ref; }({ foo: "foo", bar: "bar" diff --git a/test/fixtures/syntax/computed-property-names/multiple/expected.js b/test/fixtures/syntax/computed-property-names/multiple/expected.js index 394266087c..81c11f23f2 100644 --- a/test/fixtures/syntax/computed-property-names/multiple/expected.js +++ b/test/fixtures/syntax/computed-property-names/multiple/expected.js @@ -1,5 +1,5 @@ -var obj = function (obj) { - obj["x" + foo] = "heh"; - obj["y" + bar] = "noo"; - return obj; +var obj = function (_ref) { + _ref["x" + foo] = "heh"; + _ref["y" + bar] = "noo"; + return _ref; }({}); diff --git a/test/fixtures/syntax/computed-property-names/single/expected.js b/test/fixtures/syntax/computed-property-names/single/expected.js index d71682a31a..52a86de647 100644 --- a/test/fixtures/syntax/computed-property-names/single/expected.js +++ b/test/fixtures/syntax/computed-property-names/single/expected.js @@ -1,4 +1,4 @@ -var obj = function (obj) { - obj["x" + foo] = "heh"; - return obj; +var obj = function (_ref) { + _ref["x" + foo] = "heh"; + return _ref; }({}); diff --git a/test/fixtures/syntax/computed-property-names/this/expected.js b/test/fixtures/syntax/computed-property-names/this/expected.js index e1aee4c417..7d26e193a7 100644 --- a/test/fixtures/syntax/computed-property-names/this/expected.js +++ b/test/fixtures/syntax/computed-property-names/this/expected.js @@ -1,4 +1,4 @@ -var obj = function (obj) { - obj["x" + this.foo] = "heh"; - return obj; +var obj = function (_ref) { + _ref["x" + this.foo] = "heh"; + return _ref; }.call(this, {}); diff --git a/test/fixtures/syntax/property-methods-assignment/getter-and-setter/expected.js b/test/fixtures/syntax/property-methods-assignment/getter-and-setter/expected.js index d52c634e98..fc86c1cb17 100644 --- a/test/fixtures/syntax/property-methods-assignment/getter-and-setter/expected.js +++ b/test/fixtures/syntax/property-methods-assignment/getter-and-setter/expected.js @@ -1,5 +1,5 @@ -var obj = function (obj) { - Object.defineProperties(obj, { +var obj = function (_ref) { + Object.defineProperties(_ref, { foo: { get: function () { return 5 + 5; @@ -9,5 +9,5 @@ var obj = function (obj) { } } }); - return obj; + return _ref; }({}); diff --git a/test/fixtures/syntax/property-methods-assignment/getter/expected.js b/test/fixtures/syntax/property-methods-assignment/getter/expected.js index 7f6b975474..ea416a652a 100644 --- a/test/fixtures/syntax/property-methods-assignment/getter/expected.js +++ b/test/fixtures/syntax/property-methods-assignment/getter/expected.js @@ -1,10 +1,10 @@ -var obj = function (obj) { - Object.defineProperties(obj, { +var obj = function (_ref) { + Object.defineProperties(_ref, { foo: { get: function () { return 5 + 5; } } }); - return obj; + return _ref; }({}); diff --git a/test/fixtures/syntax/property-methods-assignment/setter/expected.js b/test/fixtures/syntax/property-methods-assignment/setter/expected.js index 2bed21387e..b1329408d4 100644 --- a/test/fixtures/syntax/property-methods-assignment/setter/expected.js +++ b/test/fixtures/syntax/property-methods-assignment/setter/expected.js @@ -1,10 +1,10 @@ -var obj = function (obj) { - Object.defineProperties(obj, { +var obj = function (_ref) { + Object.defineProperties(_ref, { foo: { set: function (value) { this._foo = value; } } }); - return obj; + return _ref; }({});