google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.17k stars 578 forks source link

Duplicate variable error #2091

Closed guybedford closed 8 years ago

guybedford commented 8 years ago

For the following code:

'use strict';
var test = {
  echo: function echo() {
    var echo = '';
  }
};

Traceur will throw when running the scope transformer over the above as echo is added as a const declaration by the function itself, and not allowed to be defined as a var. Is this a valid error or should the code above be supported?

The error thrown by Traceur is:

Cannot read property 'reportError' of null
    at reportDuplicateVar (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:17691:13)
    at Scope.addVar (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:17715:11)
    at Scope.addBinding (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:17707:16)
    at ScopeChainBuilder.$__super.declareVariable (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:18082:20)
    at ScopeChainBuilder.$__super.visitBindingIdentifier (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:18019:14)
    at BindingIdentifier.$__super.visit (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:6597:17)
    at ScopeChainBuilder.ParseTreeVisitor.visitAny (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4110:31)
    at ScopeChainBuilder.ParseTreeVisitor.visitVariableDeclaration (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4519:14)
    at VariableDeclaration.$__super.visit (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:8589:17)
    at ScopeChainBuilder.ParseTreeVisitor.visitAny (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4110:31)
    at ScopeChainBuilder.ParseTreeVisitor.visitList (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4118:18)
    at ScopeChainBuilder.ParseTreeVisitor.visitVariableDeclarationList (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4524:14)
    at ScopeChainBuilder.$__super.visitVariableDeclarationList (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:18016:101)
    at VariableDeclarationList.$__super.visit (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:8608:17)
    at ScopeChainBuilder.ParseTreeVisitor.visitAny (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4110:31)
    at ScopeChainBuilder.ParseTreeVisitor.visitVariableStatement (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4527:14)
    at VariableStatement.$__super.visit (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:8626:17)
    at ScopeChainBuilder.ParseTreeVisitor.visitAny (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4110:31)
    at ScopeChainBuilder.ParseTreeVisitor.visitList (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4118:18)
    at ScopeChainBuilder.ParseTreeVisitor.visitFunctionBody (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4296:14)
    at FunctionBody.$__super.visit (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:7296:17)
    at ScopeChainBuilder.ParseTreeVisitor.visitAny (/Users/guybedford/projects/traceur-compiler/bin/traceur.js:4110:31)
arv commented 8 years ago

This should not be an error.

It is a bit tricky because named function expressions do introduce a const binding for the name but it is allowed to be shadowed.