add Scope#moveBindingTo method and change more for array loops to for...of

This commit is contained in:
Sebastian McKenzie 2015-05-11 17:25:09 +01:00
parent f373f8f003
commit 539784b640
2 changed files with 28 additions and 23 deletions

View File

@ -32,8 +32,8 @@ function isVar(node, parent) {
}
function standardizeLets(declars) {
for (var i = 0; i < declars.length; i++) {
delete declars[i]._let;
for (var declar of (declars: Array)) {
delete declar._let;
}
}
@ -109,19 +109,14 @@ function traverseReplace(node, parent, scope, remaps) {
}
var letReferenceBlockVisitor = {
enter(node, parent, scope, state) {
if (this.isFunction()) {
this.traverse(letReferenceFunctionVisitor, state);
return this.skip();
}
Function(node, parent, scope, state) {
this.traverse(letReferenceFunctionVisitor, state);
return this.skip();
}
};
var letReferenceFunctionVisitor = {
enter(node, parent, scope, state) {
// not a direct reference
if (!this.isReferencedIdentifier()) return;
ReferencedIdentifier(node, parent, scope, state) {
var ref = state.letReferences[node.name];
// not a part of our scope
@ -159,10 +154,8 @@ var hoistVarDeclarationsVisitor = {
};
var loopLabelVisitor = {
enter(node, parent, scope, state) {
if (this.isLabeledStatement()) {
state.innerLabels.push(node.label.name);
}
LabeledStatement(node, parent, scope, state) {
state.innerLabels.push(node.label.name);
}
};

View File

@ -430,8 +430,8 @@ export default class Scope {
this.registerBinding("hoisted", path);
} else if (t.isVariableDeclaration(node)) {
var declarations = path.get("declarations");
for (var i = 0; i < declarations.length; i++) {
this.registerBinding(node.kind, declarations[i]);
for (var declar of (declarations: Array)) {
this.registerBinding(node.kind, declar);
}
} else if (t.isClassDeclaration(node)) {
this.registerBinding("let", path);
@ -548,8 +548,8 @@ export default class Scope {
// ForStatement - left, init
if (path.isLoop()) {
for (let i = 0; i < t.FOR_INIT_KEYS.length; i++) {
var node = path.get(t.FOR_INIT_KEYS[i]);
for (let key of (t.FOR_INIT_KEYS: Array)) {
var node = path.get(key);
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
}
}
@ -572,8 +572,8 @@ export default class Scope {
if (path.isFunction()) {
var params = path.get("params");
for (let i = 0; i < params.length; i++) {
this.registerBinding("param", params[i]);
for (let param of (params: Array)) {
this.registerBinding("param", param);
}
this.traverse(path.get("body").node, blockVariableVisitor, this);
}
@ -697,8 +697,7 @@ export default class Scope {
getAllBindingsOfKind(): Object {
var ids = object();
for (let i = 0; i < arguments.length; i++) {
var kind = arguments[i];
for (let kind of (arguments: Array)) {
var scope = this;
do {
for (var name in scope.bindings) {
@ -789,6 +788,19 @@ export default class Scope {
return this.parent && this.parent.hasBinding(name);
}
/**
* Move a binding of `name` to another `scope`.
*/
moveBindingTo(name, scope) {
var info = this.getBinding(name);
if (info) {
info.scope.removeOwnBinding(name);
info.scope = scope;
scope.bindings[name] = info;
}
}
/**
* Description
*/