Merge pull request #3274 from babel/fix-function-decl

Method names should not be bound to body
This commit is contained in:
Amjad Masad
2016-01-18 10:57:19 -08:00
5 changed files with 32 additions and 2 deletions

View File

@@ -63,6 +63,11 @@ export function push(mutatorMap: Object, node: Object, kind: string, file, scope
// infer function name
if (scope && t.isStringLiteral(key) && (kind === "value" || kind === "initializer") && t.isFunctionExpression(value)) {
value = nameFunction({ id: key, node: value, scope });
// Class methods don't have their name bound in the funciton body.
if (t.isClassMethod(node)) {
value.id[t.NOT_LOCAL_BINDING] = true
}
}
if (value) {

View File

@@ -0,0 +1,5 @@
class A {
foo() {
const foo = 2;
}
}

View File

@@ -0,0 +1,15 @@
"use strict";
var A = function () {
function A() {
babelHelpers.classCallCheck(this, A);
}
babelHelpers.createClass(A, [{
key: "foo",
value: function foo() {
var foo = 2;
}
}]);
return A;
}();

View File

@@ -663,13 +663,17 @@ export default class Scope {
// FunctionExpression - id
if (path.isFunctionExpression() && path.has("id")) {
this.registerBinding("local", path.get("id"), path);
if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
this.registerBinding("local", path.get("id"), path);
}
}
// Class
if (path.isClassExpression() && path.has("id")) {
this.registerBinding("local", path);
if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
this.registerBinding("local", path);
}
}
// Function - params, rest

View File

@@ -24,3 +24,4 @@ export const INHERIT_KEYS = {
};
export const BLOCK_SCOPED_SYMBOL = Symbol.for("var used to be block scoped");
export const NOT_LOCAL_BINDING = Symbol.for("should not be considered a local binding");