Skip to content

Variable scoping error causes wrong variable to be inlined #17543

@stevenwaterman

Description

@stevenwaterman

💻

  • Would you like to work on a fix?

How are you using Babel?

Repl

Input code

https://babeljs.io/repl#?config_lz=N4IgZglgNgpgdgQwLYxALhAJxgBygOgCsBnADxABoQdtiYAXY9AbWZHgDdLR6FMBzBkwwA-AAz4xARgCkFAARwA9vXkQY8qVIXLV6gPpIlAI01TKIIwBMArrGEgAxkqRG4JC8Zv9IpGMPpMGxgAXwBdCjZsBEd6biwbOHoIFHQQBBt6FwRkxxBwyJBiXkEAWgAmeKsYZ0wcpUxiADUYRoglODTysXKAZlKtCxwIHBgoCDgYAAVMJRwlYgQoNIALGIBrfLCIoqUbTEcYABUAT1G06ztUEKA&code_lz=JYWwDg9gTgLgBAbwK4GcCmAZAhgTwkmAUQDNi0BjeLFOAOwF85ioIQ4AiKNLS9gbgBQA4klqVgEWnADSwABTAANHQCUiAXDhcYSKFPnUcYuHLUBeAHzrNmkWJgSptU4noa49ZbRV84Aej84ABUAC2AafRoYELQ4SVjmVjho2NkmUXFJLwh4RLYUuFBIWDgUGCwYNBA0WhgBeiA&lineWrap=true&version=7.28.4

Current and expected behavior

In the linked REPL, the n on line 6 should resolve to the n from the Ki function signature on line 3. However at the very bottom of the transpiled output, the n is turned into _react.useLayoutEffect which is incorrect. The function-scoped n should shadow the module-scoped n

Environment

See repl link

Targets >0.01%, not ie 11, not ie_mob 11
Modules commonjs

The rest left as default (v7.28.4)

Possible solution

The function n() {} on line 5 is necessary for this bug to happen. So it seems like that line is somehow confusing babel about the scope of the final n

This also only happens when modules is set to commonjs and targets is >0.01%

Additional context

We experienced this in the wild when testing our pre-minified library in codesandbox and narrowed it down to the code you see in the linked repl

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions