refactor t.getOuterBindingIdentifiers to only return the id if it's a function declaration

This commit is contained in:
Sebastian McKenzie 2015-11-03 03:05:57 +00:00
parent e72d4508de
commit 64a9a6027e
2 changed files with 11 additions and 17 deletions

View File

@ -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<Object>)) {
@ -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

View File

@ -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);
}