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
Fix scope of function body when var redeclares param #11158
Conversation
On a separate note, there isn't a "function parameter scope" property on a Function, so parameters of the function and local variables of the function all get lumped together in the same scope, so we have to choose between the current behaviour, where params take precedence over function body |
Can you also add this test, which I think should return 2? function f(a = 2) {
var a;
return a;
} |
Oh right, this pr will return the wrong value for the above. |
Added and fixed, thanks for the test case! Updated the implementation and the pr description, whenever we see an empty var declaration that clashes with a parameter it gets deleted. |
Can I get a review on this (I can't seem to request reviewers)? |
...abel-plugin-transform-parameters/test/fixtures/parameters/var-same-as-param-closure/input.js
Outdated
Show resolved
Hide resolved
The following test failed when merging latest master. Can this be updated?
|
@openorclose Can you rebase on the upstream master? It should be fixed now. |
796b77f
to
26adb87
Compare
Rebased! |
Nice work @openorclose! |
Thanks for the reviews! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
The below is valid js, but babel currently throws a redeclaration error:
Link: https://babeljs.io/repl#?browsers=%3E0.5%25%2C%20not%20ie%20%3C%3D11%2C%20not%20safari%205.1%2C%20not%20samsung%204&build=&builtIns=false&spec=false&loose=false&code_lz=GYVwdgxgLglg9mABMAFAQ0QXkQRgJSIDeAUIogG5oBOiG2OA3MQL5A&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ces2015%2Cenv&prettier=false&targets=&version=7.8.4&externalPlugins=%40babel%2Fplugin-transform-react-jsx%407.8.3
babel-traverse registers a
var
or function declaration that redeclare a function parameter as a constant violation.So now during function parameter transformation, if we encounter a parameter whose bindings contain constant violations, we check if they are
var
orfunction
declarations. If we find emptyvar
declarations (e.g.var x;
), we delete them since such declarations do not override the parameter. If not, so we have var declarations with initalisers or function declarations, and we need to wrap the function body in an iife to allow these differently scoped declarations.However
callDelegate
always hoists up thesevar
declarations into the real function scope instead of letting them remain in the IIFE function scope, so let's add another parameter tocallDelegate
that allows us to prevent this hoisting.We only need to check for
var
and function declarations redefinition, because redeclaration of parameters viaconst
orlet
is a parse error.