From bf0e4ede00236cc46e819eca3987a8f0ecfbebfb Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 6 May 2015 16:03:15 +0100 Subject: [PATCH] pass correct function scope to nameMethod.property when naming class methods - fixes #1456 --- .../transformation/transformers/es6/classes.js | 8 ++++---- .../spec.function-name/class-method/actual.js | 5 +++++ .../spec.function-name/class-method/expected.js | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/core/fixtures/transformation/spec.function-name/class-method/actual.js create mode 100644 test/core/fixtures/transformation/spec.function-name/class-method/expected.js diff --git a/src/babel/transformation/transformers/es6/classes.js b/src/babel/transformation/transformers/es6/classes.js index 2a4fe261cc..670a18b5fb 100644 --- a/src/babel/transformation/transformers/es6/classes.js +++ b/src/babel/transformation/transformers/es6/classes.js @@ -314,7 +314,7 @@ class ClassTransformer { if (isConstructor) { this.pushConstructor(node, path); } else { - this.pushMethod(node); + this.pushMethod(node, path); } } else if (t.isClassProperty(node)) { this.pushProperty(node); @@ -412,7 +412,7 @@ class ClassTransformer { this.pushMethod(t.methodDefinition( t.identifier(PROPERTY_COLLISION_METHOD_NAME), t.functionExpression(null, [], t.blockStatement(body)) - ), true); + ), null, true); if (this.hasSuper) { this.bareSuper.insertAfter(call); @@ -473,13 +473,13 @@ class ClassTransformer { * Push a method to its respective mutatorMap. */ - pushMethod(node: { type: "MethodDefinition" }, allowedIllegal?) { + pushMethod(node: { type: "MethodDefinition" }, path?: TraversalPath, allowedIllegal?) { if (!allowedIllegal && t.isLiteral(t.toComputedKey(node), { value: PROPERTY_COLLISION_METHOD_NAME })) { throw this.file.errorWithNode(node, messages.get("illegalMethodName", PROPERTY_COLLISION_METHOD_NAME)); } if (node.kind === "method") { - nameMethod.property(node, this.file, this.scope); + nameMethod.property(node, this.file, path ? path.get("value").scope : this.scope); if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/test/core/fixtures/transformation/spec.function-name/class-method/actual.js b/test/core/fixtures/transformation/spec.function-name/class-method/actual.js new file mode 100644 index 0000000000..88b2734a3f --- /dev/null +++ b/test/core/fixtures/transformation/spec.function-name/class-method/actual.js @@ -0,0 +1,5 @@ +class Foo { + t(t) { + return t; + } +} diff --git a/test/core/fixtures/transformation/spec.function-name/class-method/expected.js b/test/core/fixtures/transformation/spec.function-name/class-method/expected.js new file mode 100644 index 0000000000..244149b7c5 --- /dev/null +++ b/test/core/fixtures/transformation/spec.function-name/class-method/expected.js @@ -0,0 +1,15 @@ +"use strict"; + +var Foo = (function () { + function Foo() { + babelHelpers.classCallCheck(this, Foo); + } + + babelHelpers.createClass(Foo, [{ + key: "t", + value: function t(_t) { + return _t; + } + }]); + return Foo; +})();