remove binding kind from pushing bindings to the function scope, better block scoped collision logic, and track label ids as references

This commit is contained in:
Sebastian McKenzie
2015-02-09 21:21:25 +11:00
parent 478f9e028f
commit 8a4296a948
2 changed files with 11 additions and 10 deletions

View File

@@ -460,7 +460,7 @@ File.prototype.generateUid = function (name, scope) {
File.prototype.generateUidIdentifier = function (name, scope) {
scope = scope || this.scope;
var id = t.identifier(this.generateUid(name, scope));
scope.addBindingToFunctionScope("var", id);
scope.addBindingToFunctionScope(id);
return id;
};

View File

@@ -134,7 +134,10 @@ Scope.prototype.generateTempBasedOnNode = function (node) {
return id;
};
Scope.prototype.checkBlockScopedCollisions = function (key, id) {
Scope.prototype.checkBlockScopedCollisions = function (kind, key, id) {
if (kind === "param") return;
if (kind === "hoisted" && this.bindingKinds["let"][key]) return;
if (this.bindingKinds["let"][key] || this.bindingKinds["const"][key]) {
throw this.file.errorWithNode(id, "Duplicate declaration " + key, TypeError);
}
@@ -195,9 +198,7 @@ Scope.prototype.register = function (node, reference, kind) {
for (var key in ids) {
var id = ids[key];
if (kind !== "hoisted" && kind !== "param") {
this.checkBlockScopedCollisions(key, id);
}
this.checkBlockScopedCollisions(kind, key, id);
this.registerType(key, id, node);
this.bindings[key] = id;
@@ -245,6 +246,8 @@ var programReferenceVisitor = {
enter: function (node, parent, scope, state) {
if (t.isReferencedIdentifier(node, parent) && !scope.hasReference(node.name)) {
state.register(node, true);
} else if (t.isLabelStatemented(node)) {
state.register(node.label, true);
}
}
};
@@ -393,20 +396,18 @@ Scope.prototype.push = function (opts) {
* Walk up the scope tree until we hit a `Function` and then
* push our `node` to it's references.
*
* @param {String} kind
* @param {Object} node
* @param {String} [kind]
*/
Scope.prototype.addBindingToFunctionScope = function (kind, node) {
Scope.prototype.addBindingToFunctionScope = function (node, kind) {
var scope = this.getFunctionParent();
var ids = t.getBindingIdentifiers(node);
extend(scope.bindings, ids);
extend(scope.references, ids);
// this ignores the duplicate declaration logic specified in `getInfo`
// but it doesn't really matter
extend(scope.bindingKinds[kind], ids);
if (kind) extend(scope.bindingKinds[kind], ids)
};
/**