From 44f06c0b4c4a124ebf362c054efc179e376c951a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:24:23 +1100 Subject: [PATCH] instance and static class method names --- .../transformers/es6-classes.js | 20 ++++++++++++++++--- .../es6-property-method-assignment.js | 16 +++++++++------ lib/6to5/types/index.js | 4 ++++ .../accessing-super-class/expected.js | 4 ++-- .../calling-super-properties/expected.js | 2 +- .../accessing-super-class/expected.js | 4 ++-- .../calling-super-properties/expected.js | 2 +- .../es6-classes/instance-method/expected.js | 2 +- .../es6-classes/statement/expected.js | 10 +++++----- .../es6-classes/static/expected.js | 2 +- .../private/expected.js | 4 ++-- 11 files changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 73ff53c5f5..d87aaa401f 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,12 +1,24 @@ -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); +var propertyMethodAssignment = require("./es6-property-method-assignment"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.ClassDeclaration = function (node, parent, file, scope) { return new Class(node, file, scope, true).run(); }; exports.ClassExpression = function (node, parent, file, scope) { + if (!node.id) { + if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) { + // var o = { foo: class {} }; + node.id = parent.key + } + + if (t.isVariableDeclarator(parent)) { + node.id = parent.id; + } + } + return new Class(node, file, scope, false).run(); }; @@ -181,6 +193,8 @@ Class.prototype.pushMethod = function (node) { var kind = node.kind; if (kind === "") { + propertyMethodAssignment._namedMethod(node, this.file, this.scope); + if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index b826f6977d..5dbe72c55a 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -2,13 +2,9 @@ var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); -exports.Property = function (node, parent, file, scope) { - if (!node.method) return; - - node.method = false; - +exports._namedMethod = function (node, file, scope) { var key = t.toComputedKey(node, node.key); - if (!t.isLiteral(key)) return; // we can't set a function id with this + if (!t.isLiteral(key)) return node; // we can't set a function id with this var id = t.toIdentifier(key.value); key = t.identifier(id); @@ -46,6 +42,14 @@ exports.Property = function (node, parent, file, scope) { } }; +exports.Property = function (node, parent, file, scope) { + if (!node.method) return; + + node.method = false; + + exports._namedMethod(node, file, scope); +}; + exports.ObjectExpression = function (node) { var mutatorMap = {}; var hasAny = false; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 99308880ad..c00e9506af 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -201,6 +201,10 @@ t.isReferenced = function (node, parent) { if (_.contains(parent.params, node)) return false; } + if (t.isMethodDefinition(parent) && parent.key === node && !parent.computed) { + return false; + } + // we're a catch clause param if (t.isCatchClause(parent) && parent.param === node) return false; diff --git a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js index b75a433fa1..92710413da 100644 --- a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js @@ -32,14 +32,14 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.prototype.test = function () { + Test.prototype.test = function test() { var _Foo$prototype$test3, _Foo$prototype$test4; Foo.prototype.test.call(this); (_Foo$prototype$test3 = Foo.prototype.test).call.apply(_Foo$prototype$test3, [this].concat(_slice.call(arguments))); (_Foo$prototype$test4 = Foo.prototype.test).call.apply(_Foo$prototype$test4, [this, "test"].concat(_slice.call(arguments))); }; - Test.foo = function () { + Test.foo = function foo() { var _Foo$foo, _Foo$foo2; Foo.foo.call(this); (_Foo$foo = Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments))); diff --git a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js index a69f118608..065e6fc263 100644 --- a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js @@ -23,7 +23,7 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.test = function () { + Test.test = function test() { return Foo.wow.call(this); }; diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index a762c97203..e783b08f76 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -61,7 +61,7 @@ var Test = (function (Foo) { _prototypeProperties(Test, { foo: { - value: function () { + value: function foo() { var _get4; _get(Object.getPrototypeOf(Test), "foo", this).call(this); _get(Object.getPrototypeOf(Test), "foo", this).apply(this, arguments); @@ -73,7 +73,7 @@ var Test = (function (Foo) { } }, { test: { - value: function () { + value: function test() { var _get5; _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); _get(Object.getPrototypeOf(Test.prototype), "test", this).apply(this, arguments); diff --git a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js index fc04a0bab4..b82d42c705 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -52,7 +52,7 @@ var Test = (function (Foo) { _prototypeProperties(Test, { test: { - value: function () { + value: function test() { return _get(Object.getPrototypeOf(Test), "wow", this).call(this); }, writable: true, diff --git a/test/fixtures/transformation/es6-classes/instance-method/expected.js b/test/fixtures/transformation/es6-classes/instance-method/expected.js index 8887b8d645..f632a93cba 100644 --- a/test/fixtures/transformation/es6-classes/instance-method/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-method/expected.js @@ -10,7 +10,7 @@ var Test = (function () { _prototypeProperties(Test, null, { test: { - value: function () { + value: function test() { return 5 + 5; }, writable: true, diff --git a/test/fixtures/transformation/es6-classes/statement/expected.js b/test/fixtures/transformation/es6-classes/statement/expected.js index 1a10318ee2..ac555c8256 100644 --- a/test/fixtures/transformation/es6-classes/statement/expected.js +++ b/test/fixtures/transformation/es6-classes/statement/expected.js @@ -9,16 +9,16 @@ var BaseView = function BaseView() { this.autoRender = true; }; -var BaseView = function () { +var BaseView = function BaseView() { this.autoRender = true; }; var BaseView = (function () { - var _class2 = function () {}; + function BaseView() {} - _prototypeProperties(_class2, null, { + _prototypeProperties(BaseView, null, { foo: { - value: function () { + value: function foo() { this.autoRender = true; }, writable: true, @@ -27,5 +27,5 @@ var BaseView = (function () { } }); - return _class2; + return BaseView; })(); diff --git a/test/fixtures/transformation/es6-classes/static/expected.js b/test/fixtures/transformation/es6-classes/static/expected.js index 408d9a036a..b219f347c7 100644 --- a/test/fixtures/transformation/es6-classes/static/expected.js +++ b/test/fixtures/transformation/es6-classes/static/expected.js @@ -10,7 +10,7 @@ var A = (function () { _prototypeProperties(A, { a: { - value: function () {}, + value: function a() {}, writable: true, enumerable: true, configurable: true diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js index d94a941e04..1af25539ca 100644 --- a/test/fixtures/transformation/es7-abstract-references/private/expected.js +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -16,7 +16,7 @@ var H = (function () { var J = new WeakMap(), K = new WeakMap(); var I = new WeakMap(); - var _class = function () {}; + function H() {} - return _class; + return H; })();