Skip to content

[Bug]: iterableIsArray assumption with shadowing variable generates invalid code #16009

@francis-zhu-cloudpense

Description

@francis-zhu-cloudpense

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

Fn = (e) => {
  return {
    extract: () => {
      const r = [];
      for (const s of e) {
        const e = s;
        r.push(e);
      }
      return r;
    },
  };
};

Configuration file name

babel.config.json

Configuration

{
  "presets": [
    "@babel/preset-env"
  ],
  "assumptions": {
    "iterableIsArray": true
  }
}

Current and expected behavior

current result:

"use strict";

Fn = function Fn(e) {
  return {
    extract: function extract() {
      var r = [];
      for (var _i2 = 0; _i2 < e.length; _i2++) {
        var s = _e[_i2];
        var _e = s;
        r.push(_e);
      }
      return r;
    }
  };
};

the _e reference in inner most block is not valid. should be

"use strict";

Fn = function Fn(e) {
  return {
    extract: function extract() {
      var r = [];
      for (var _i2 = 0; _i2 < e.length; _i2++) {
        var s = e[_i2];
        var _e = s;
        r.push(_e);
      }
      return r;
    }
  };
};

Environment

  System:
    OS: macOS 13.6
  Binaries:
    Node: 20.2.0 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.6.6 - /opt/homebrew/bin/npm
  npmPackages:
    @babel/cli: ^7.23.0 => 7.23.0
    @babel/core: ^7.23.0 => 7.23.0
    @babel/preset-env: ^7.22.20 => 7.22.20

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA 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