Skip to content

[Bug]: Scope rename affecting parent scope computed property identifier when shadowing #16751

@albertogasparin

Description

@albertogasparin

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

See it on ASTExplorer: https://astexplorer.net/#/gist/3a1d53d8dec7b552e6ce309a2d933f7d/487e79162e32ae27cdd60c2b5ef8dd3670e10d95

Given

let x = 1

const foo = {
 get [x]() {
   return x
 },
}

And this transformation:

Function(path) {
  const bodyPath = path.get('body');
  // create a declaration that shadows parent variable
  const declaration = t.variableDeclaration('const', [
    t.variableDeclarator(t.identifier('x'), t.nullLiteral())
  ]);
  bodyPath.unshiftContainer('body', declaration);
  const declarationPath = bodyPath.get('body.0');
  // register the new declaration
  bodyPath.scope.registerDeclaration(declarationPath);
  // rename the new "local" declaration 
  bodyPath.scope.rename('x', 'y')
}

Configuration file name

No response

Configuration

No response

Current and expected behavior

The current output becomes invalid as the resulting identifier is used before being declared

let x = 1;
const foo = {
  get [y]() {
    const y = null;
    return y;
  }
};

While I would expect the computed property identifier to still be x.
The problem does not affect other types of functions.

Environment

  • Babel: v7.19.0 and 7.23.0 (tested)

Possible solution

No response

Additional context

No response

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