Skip to content

[Bug]: transform error when use two loops in one function with @babel/plugin-transform-block-scoping@7.20.9 #15308

@Erucy

Description

@Erucy

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const test = (data) => {
  for (let i = 0; i < data.length; i++) {
    const x = data[i];
    data.find((item) => item === x);
  }
  for (let i = 0; i < data.length; i++) {
    const x = data[i];
    data.find((item) => item === x);
  }
}

Configuration file name

.babelrc

Configuration

{
"presets": [
"@babel/preset-env"
]
}

Current and expected behavior

current output:

"use strict";

var test = function test(data) {
  var _loop = function _loop() {
    var x = data[i];
    data.find(function (item) {
      return item === x;
    });
  };
  for (var i = 0; i < data.length; i++) {
    _loop();
  }
  var _loop2 = function _loop2() {
    var x = data[i];
    data.find(function (item) {
      return item === x;
    });
  };
  for (var _i = 0; _i < data.length; _i++) {
    _loop2();
  }
};

the second var x = data[i]; should be var x = data[_i];

Environment

System:
OS: macOS 12.6.2
Binaries:
Node: 14.19.0 - /usr/local/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 6.14.16 - /usr/local/bin/npm
npmPackages:
@babel/cli: ^7.17.6 => 7.17.6
@babel/core: ^7.20.7 => 7.20.7
@babel/node: ^7.16.8 => 7.16.8
@babel/plugin-transform-block-scoping: ^7.20.9 => 7.20.9
@babel/preset-env: ^7.16.11 => 7.16.11

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: regressionoutdatedA 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