From 6af0ffd97e08f08d30d2325a8eb1ca75ed8ce655 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 14 Oct 2014 08:50:34 +1100 Subject: [PATCH] nicer automatic reference variables for comptued property names and property method assignment #54 --- .../transformers/computed-property-names.js | 16 +---------- .../property-method-assignment.js | 2 +- lib/6to5/util.js | 27 ++++++++++++++++--- .../classes/accessing-super-class/expected.js | 18 +++++++------ .../getter-and-setter/expected.js | 6 ++--- .../getter/expected.js | 6 ++--- .../setter/expected.js | 6 ++--- 7 files changed, 45 insertions(+), 36 deletions(-) diff --git a/lib/6to5/transformers/computed-property-names.js b/lib/6to5/transformers/computed-property-names.js index dc356c62b8..fa9c03deeb 100644 --- a/lib/6to5/transformers/computed-property-names.js +++ b/lib/6to5/transformers/computed-property-names.js @@ -19,21 +19,7 @@ exports.ObjectExpression = function (node, parent, file) { if (!hasComputed) return; - var objIdNode; - - if (parent.type === "AssignmentExpression") { - objIdNode = parent.left; - } else if (parent.type === "VariableDeclarator") { - objIdNode = parent.id; - } - - var objId = "ref"; - - if (objIdNode && objIdNode.type === "Identifier") { - objId = objIdNode.name; - } - - objId = b.identifier(file.generateUid(objId)); + var objId = util.getUid(parent, file); var container = util.template("function-return-obj", { KEY: objId, diff --git a/lib/6to5/transformers/property-method-assignment.js b/lib/6to5/transformers/property-method-assignment.js index e0b38973a0..cbddb4a618 100644 --- a/lib/6to5/transformers/property-method-assignment.js +++ b/lib/6to5/transformers/property-method-assignment.js @@ -20,7 +20,7 @@ exports.ObjectExpression = function (node, parent, file) { if (_.isEmpty(mutatorMap)) return; - var objId = b.identifier(file.generateUid("ref")); + var objId = util.getUid(parent, file); return util.template("object-define-properties-closure", { KEY: objId, diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 9826858d1b..f0fb649bb4 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -20,6 +20,24 @@ exports.ensureBlock = function (node) { node.body = b.blockStatement(block); }; +exports.getUid = function (parent, file) { + var node; + + if (parent.type === "AssignmentExpression") { + node = parent.left; + } else if (parent.type === "VariableDeclarator") { + node = parent.id; + } + + var id = "ref"; + + if (node && node.type === "Identifier") { + id = node.name; + } + + return b.identifier(file.generateUid(id)); +}; + exports.isAbsolute = function (loc) { if (!loc) return false; if (loc[0] === "/") return true; // unix @@ -245,6 +263,11 @@ exports.parse = function (opts, code, callback) { } }; +exports.parseNoProperties = function (loc, code) { + var ast = exports.parse({ filename: loc }, code).program; + return exports.removeProperties(ast); +}; + try { exports.templates = require("../../templates.json"); } catch (err) { @@ -266,8 +289,6 @@ try { var loc = templatesLoc + "/" + name; var code = fs.readFileSync(loc, "utf8"); - var ast = exports.parse({ filename: loc }, code).program; - ast = exports.removeProperties(ast); - exports.templates[key] = ast; + exports.templates[key] = exports.parseNoProperties(loc, code); }); } diff --git a/test/fixtures/syntax/classes/accessing-super-class/expected.js b/test/fixtures/syntax/classes/accessing-super-class/expected.js index 797739f07c..70e4ced969 100644 --- a/test/fixtures/syntax/classes/accessing-super-class/expected.js +++ b/test/fixtures/syntax/classes/accessing-super-class/expected.js @@ -1,16 +1,18 @@ +var _slice = Array.prototype.slice; + var Test = function(Foo) { var Test = function Test() { woops.super.test(); Foo.call(this); Foo.prototype.test.call(this); foob(Foo); - Foo.call.apply(Foo, [this].concat(Array.prototype.slice.call(arguments))); - Foo.call.apply(Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this].concat(Array.prototype.slice.call(arguments))); + Foo.call.apply(Foo, [this].concat(_slice.call(arguments))); + Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments))); Foo.prototype.test.call.apply( Foo.prototype, - [this, "test"].concat(Array.prototype.slice.call(arguments)) + [this, "test"].concat(_slice.call(arguments)) ); }; @@ -31,11 +33,11 @@ var Test = function(Foo) { value: function() { Foo.prototype.test.call(this); - Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(Array.prototype.slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments))); Foo.prototype.test.call.apply( Foo.prototype.test, - [this, "test"].concat(Array.prototype.slice.call(arguments)) + [this, "test"].concat(_slice.call(arguments)) ); } } @@ -47,8 +49,8 @@ var Test = function(Foo) { value: function() { Foo.foo.call(this); - Foo.foo.call.apply(Foo.foo, [this].concat(Array.prototype.slice.call(arguments))); - Foo.foo.call.apply(Foo.foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); + Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments))); + Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments))); } } }); 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 fc86c1cb17..546b22ba33 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 (_ref) { - Object.defineProperties(_ref, { +var obj = function (_obj) { + Object.defineProperties(_obj, { foo: { get: function () { return 5 + 5; @@ -9,5 +9,5 @@ var obj = function (_ref) { } } }); - return _ref; + return _obj; }({}); diff --git a/test/fixtures/syntax/property-methods-assignment/getter/expected.js b/test/fixtures/syntax/property-methods-assignment/getter/expected.js index ea416a652a..3b3cea8385 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 (_ref) { - Object.defineProperties(_ref, { +var obj = function (_obj) { + Object.defineProperties(_obj, { foo: { get: function () { return 5 + 5; } } }); - return _ref; + return _obj; }({}); diff --git a/test/fixtures/syntax/property-methods-assignment/setter/expected.js b/test/fixtures/syntax/property-methods-assignment/setter/expected.js index b1329408d4..8ff48f2b58 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 (_ref) { - Object.defineProperties(_ref, { +var obj = function (_obj) { + Object.defineProperties(_obj, { foo: { set: function (value) { this._foo = value; } } }); - return _ref; + return _obj; }({});