Skip to content

[Bug]: @babel/traverse incorrect constantViolations with destructuring #16512

@j4k0xb

Description

@j4k0xb

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;

const ast = parser.parse(`
function v() { }
const G2 = () => {
  var a = 1;
  var {
    currentPoint: a,
    centp: v,
  } = {};
}
`);
traverse(ast, {
  Program(path) {
    const binding = path.scope.getBinding("v");
    console.log(binding.constantViolations.length);
  },
});

https://astexplorer.net/#/gist/a160da8edc997fcb75cf56061f0e0b79/72a54c5194bffab642627a4a2ddca828a2209f4f

Configuration file name

No response

Configuration

No response

Current and expected behavior

It currently detects the the VariableDeclarator with centp: v as a constantViolation of the global function v.
But v in G2 should be it's own binding.
Interestingly when the a variable or currentPoint: a is deleted, it logs 0 constantViolations for v

Environment

System:
OS: Linux 6.6 Manjaro Linux
Binaries:
Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
Yarn: 4.1.1 - /usr/bin/yarn
npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
pnpm: 8.14.1 - /usr/bin/pnpm
npmPackages:
@babel/generator: ^7.24.5 => 7.24.5
@babel/parser: ^7.24.5 => 7.24.5
@babel/traverse: ^7.24.5 => 7.24.5
@babel/types: ^7.24.5 => 7.24.5

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 issuepkg: traverse (scope)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions