diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index aa1b398273..082dc0bedf 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -402,7 +402,7 @@ export default class Scope { if (path.isLabeledStatement()) { this.registerBinding("label", path); } else if (path.isFunctionDeclaration()) { - this.registerBinding("hoisted", path); + this.registerBinding("hoisted", path.get("id"), true); } else if (path.isVariableDeclaration()) { let declarations = path.get("declarations"); for (let declar of (declarations: Array)) { @@ -441,7 +441,7 @@ export default class Scope { } } - registerBinding(kind: string, path: NodePath) { + registerBinding(kind: string, path: NodePath, bindingPath = path) { if (!kind) throw new ReferenceError("no `kind`"); if (path.isVariableDeclaration()) { @@ -453,7 +453,7 @@ export default class Scope { } let parent = this.getProgramParent(); - let ids = path.getOuterBindingIdentifiers(true); + let ids = path.getBindingIdentifiers(true); for (let name in ids) { for (let id of (ids[name]: Array)) { @@ -472,7 +472,7 @@ export default class Scope { identifier: id, existing: local, scope: this, - path: path, + path: bindingPath, kind: kind }); } @@ -618,7 +618,7 @@ export default class Scope { // FunctionExpression - id if (path.isFunctionExpression() && path.has("id")) { - this.registerBinding("local", path); + this.registerBinding("local", path.get("id"), path); } // Class diff --git a/packages/babel-types/src/retrievers.js b/packages/babel-types/src/retrievers.js index b42865e7cf..2dc1ffdb11 100644 --- a/packages/babel-types/src/retrievers.js +++ b/packages/babel-types/src/retrievers.js @@ -9,6 +9,7 @@ import * as t from "./index"; export function getBindingIdentifiers( node: Object, duplicates?: boolean, + outerOnly?: boolean ): Object { let search = [].concat(node); let ids = Object.create(null); @@ -30,6 +31,10 @@ export function getBindingIdentifiers( if (t.isDeclaration(node.declaration)) { search.push(node.declaration); } + } else if (outerOnly) { + if (t.isFunction(id)) { + search.push(id.id); + } } else if (keys) { for (let i = 0; i < keys.length; i++) { let key = keys[i]; @@ -93,16 +98,5 @@ export function getOuterBindingIdentifiers( node: Object, duplicates?: boolean, ): Object { - if (t.isFunction(node)) { - let id = node.id; - if (id) { - return { - [id.name]: duplicates ? [id] : id - }; - } else { - return {}; - } - } else { - return getBindingIdentifiers(node, duplicates); - } + return getBindingIdentifiers(node, duplicates, true); }