diff --git a/lib/6to5/transformation/file.js b/lib/6to5/transformation/file.js index 127e047521..87086b3ff3 100644 --- a/lib/6to5/transformation/file.js +++ b/lib/6to5/transformation/file.js @@ -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; }; diff --git a/lib/6to5/traversal/scope.js b/lib/6to5/traversal/scope.js index 49ec6b3959..147e96ed17 100644 --- a/lib/6to5/traversal/scope.js +++ b/lib/6to5/traversal/scope.js @@ -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) }; /**