Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: When a variable shadows an imported type, references are not computed #15789

Open
haskellcamargo opened this issue Jul 17, 2023 · 2 comments

Comments

@haskellcamargo
Copy link

haskellcamargo commented Jul 17, 2023

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

Interactive example here

Transformer

export default function (babel) {
  const { types: t } = babel;

  return {
    visitor: {
      ImportDeclaration: (path) => {
        for (const specifier of path.node.specifiers) {
          const name = specifier.local.name;
          const binding = path.scope.getBinding(name);

          console.log(binding.references);
        }
      }
    }
  };
}

Input file

import { ConfigAPI } from '@babel/core';

{
  const ConfigAPI: ConfigAPI = null;
  ConfigAPI;
}

Current and expected behavior

A binding returned for a name does not compute as a reference when the name is used as a type reference, and there's a shadow for the name. For example, take as input the following code:

import { ConfigAPI } from '@babel/core';
{
  const ConfigAPI: ConfigAPI = null;
  ConfigAPI;
}

When running const binding = path.scope.getBinding('ConfigAPI') on the top-most scope, the binding is correctly returned. However, binding.references evaluates to 0.
If you rename the variable ConfigAPI to something else, binding.references will correctly compute 1 and count the type reference as a proper reference.

Babel thinks that : ConfigAPI references the new variable being introduced. For the type checker, though, the type reference will correctly point out to the correct symbol.

Environment

  • Babel version: 7.19.0 (but reproducible in multiple other environments)
@babel-bot
Copy link
Collaborator

Hey @haskellcamargo! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@sahilbaishya
Copy link

Hey, can I work with this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants