-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
plugin-transform-block-scoping renames function declaration #10046
Comments
Hey @mischnic! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
Not sure if this is related: in the example above, the function inside the |
How I would expect it to behave: diff --git a/packages/babel-plugin-transform-block-scoping/src/index.js b/packages/babel-plugin-transform-block-scoping/src/index.js
index cc63c554..cdf9b863 100644
--- a/packages/babel-plugin-transform-block-scoping/src/index.js
+++ b/packages/babel-plugin-transform-block-scoping/src/index.js
@@ -701,11 +701,7 @@ class BlockScoping {
const addDeclarationsFromChild = (path, node) => {
node = node || path.node;
- if (
- t.isClassDeclaration(node) ||
- t.isFunctionDeclaration(node) ||
- isBlockScoped(node)
- ) {
+ if (t.isClassDeclaration(node) || isBlockScoped(node)) {
if (isBlockScoped(node)) {
convertBlockScopedToVar(path, node, block, this.scope);
}
diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js
index 375d5827..2d1e4149 100644
--- a/packages/babel-traverse/src/scope/index.js
+++ b/packages/babel-traverse/src/scope/index.js
@@ -131,6 +131,13 @@ const collectorVisitor = {
scope.getBlockParent().registerDeclaration(path);
},
+ FunctionDeclaration(path) {
+ let scope =
+ path.parentPath.scope.getFunctionParent() ||
+ path.parentPath.scope.getProgramParent();
+ scope.registerDeclaration(path);
+ },
+
ClassDeclaration(path) {
const id = path.node.id;
if (!id) return;
diff --git a/packages/babel-types/src/validators/isBlockScoped.js b/packages/babel-types/src/validators/isBlockScoped.js
index 51ae6e07..a02e53c9 100644
--- a/packages/babel-types/src/validators/isBlockScoped.js
+++ b/packages/babel-types/src/validators/isBlockScoped.js
@@ -6,5 +6,5 @@ import isLet from "./isLet";
* Check if the input `node` is block scoped.
*/
export default function isBlockScoped(node: Object): boolean {
- return isFunctionDeclaration(node) || isClassDeclaration(node) || isLet(node);
+ return isClassDeclaration(node) || isLet(node);
} |
I would love to make a PR out of this, but without any comment on whether |
Block scoped functions are hard to get right, since they behave differently in loose and strict mode. |
Yes it does 😐 :
(
Details
|
The second one should be something like This seems to be a deeper problem in If you want, you could open a WIP pr 😉 |
😄, done: #10050 |
I believe I have an issue similar to this. I recently upgraded from React-Native 0.53.3 to 0.59.9 and my project is not creating the and I get this error:
I can't seem to find assistance anywhere, this is my
Any kind of assistance that is not repeating what others have said is appreciated. I have been at this for a week now. |
Bug Report
Current Behavior
@babel/plugin-transform-block-scoping (and maybe some other plugin in preset-env) incorrectly renames a FunctionDeclaration in a nested block even though it should behave like a
var
declaration.Input Code
REPL
Note: turning
function run() {
intovar run = function() {
behaves correctly.Expected behavior/code
Same as Input
Actual output
Babel Configuration (.babelrc, package.json, cli command)
or
Environment
The text was updated successfully, but these errors were encountered: