babel: SyntaxError: unknown: Binding 'arguments' in strict mode (2:6)
Hi I follow the instructions in this doc to transform arrow functions.
I find an error SyntaxError: unknown: Binding 'arguments' in strict mode (2:5) is thrown every time when a variable is named “arguments”. For example,
var foo = () => {
var arguments = 1;
};
The instruction I use is require('@babel/core').transform(code, {plugins: ['@babel/plugin-transform-arrow-functions']}) where code is the input program like the above example.
So how can I fix it? Is there any options I miss to set?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (14 by maintainers)
@RichardHoOoOo If you are interested in fixing this issue you are welcome to help, otherwise it’s open for anyone who wants to work on it!
The problem is in the utility we have to convert arrow functions to ES5 function expressions. In the
hoistFunctionEnvironmentfunction, we “extract”this,arguments,superandnew.targetto the outer scope, to preserve the correct semantics in cases like this one:However, when there is a
var arguments;declaration,argumentsshouldn’t refer to the outer function’sargumentsanymore. When replacingarguments, we need to check if it was defined as a variable in the local scope usingargumentsChild.scope.hasOwnBinding("arguments").Note that we also need to check
argumentsChild.scope.parent,argumentsChild.scope.parent.parentand so on: we should stop when we reach a non-arrow function scope, which defines the implicitargumentsbinding.A few test cases:
If it is the first time that you contribute to Babel, follow these steps: (you need to have
makeandyarnavailable on your machine)git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babelyarn && make bootstrapmake watch(ormake buildwhenever you change a file)input.js;output.jswill be automatically generated)yarn jest plugin-transform-arrow-functionsto run the testsoutput.jsfiles and run the tests againOVERWRITE=true yarn jest plugin-transform-arrow-functionsand they will be automatically updated.make testto run all the testsgit pushand open a PR!I have started working on the issue!
It’s expected that it doesn’t stop: it’s waiting for you to change some files. I suggest leaving it in the background and opening a new terminal.
That’s expected (I should have been clearer):
make watchnever ends. If you modify a file and save it, you’ll notice that themake watchcommand that is running will produce new output showing that it re-compiled the updated file.@nicolo-ribaudo Very appreciate your help for finding the root cause! Let me take a look and see if I can fix it.
Thanks for your answer! It works!
However, it seems the semantic of the program got changed. For example, if the input is like this:
The output is
Then the output in console is different. Could you give me some more advise? Thanks!
You can pass
sourceType: "script"as an option to Babel. The default value is"module", and that code is not valid inside modules.