-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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]: Function declarations not hoisted to upper scope in sloppy mode code #13549
Comments
Hey @overlookmotel! 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." |
Does |
💻
How are you using Babel?
Programmatic API (
babel.transform
,babel.parse
)Input code
Configuration file name
babel.config.json
Configuration
n/a
Current and expected behavior
In strict mode, function declarations are scoped to the statement block in which they're defined (i.e. behave like
let
). In sloppy mode they are scoped to the next enclosing function / top-level (i.e. behave likevar
).This code is valid in sloppy mode and will execute without an error:
Babel appears not to take strict/sloppy mode into account, and applies the strict mode behavior regardless.
This outputs:
i.e.
path.scope.getBinding('f')
did not identify thatfunction f
is in scope off()
.NB As far as I can see from testing in NodeJS, the function declaration's scope is hoisted up to enclosing function's scope/top-level scope if its surrounding environment is sloppy mode, regardless of whether the function itself is strict. i.e.
f
is still defined in top level scope in this case:Environment
Possible solution
No response
Additional context
I'm happy to work on a fix but would appreciate it if someone can point me in right direction. Last time I looked at the scope logic in Babel, I had trouble getting my head around it.
The text was updated successfully, but these errors were encountered: