Skip to content

Repl error – Duplicate declaration – when using the same let-variable in for-of loop #11920

@aleksusklim

Description

@aleksusklim

Current behavior

Using REPL (current/default v7.11.1) with all of checkboxes disabled:

(Explicitly disable ENV or enable es2015 to see the error via the link above)

Input Code

function f(y){
  for(let x of y){
    let x = y;
    return x;
  }
}

Expected behavior

That should successfully compile just as this code does:

function f(z){
  for(let x=0; x<y; x++){
    let x = y;
    return x;
  }
}

Which gives

"use strict";

function f(z) {
  for (var x = 0; x < y; x++) {
    var _x = y;
    return _x;
  }
}

Actual behavior

However, the error is:

/repl.js: Duplicate declaration "x"
  1 | function f(y){
  2 |   for(let x of y){
> 3 |     let x = y;
    |         ^
  4 |     return x;
  5 |   }
  6 | }

Additional context

If the ES6 transpiling (let to var) is disabled (ENV: firefox 74), that code correctly gives:

"use strict";

function f(y) {
  for (let x of y) {
    let x = y;
    return x;
  }
}

Environment

  • Babel version: REPL v7.11.1
  • Browser: Firefox 76 on Windows 7

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions