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
hoistFunctionEnvironment
function, we “extract”this
,arguments
,super
andnew.target
to the outer scope, to preserve the correct semantics in cases like this one:However, when there is a
var arguments;
declaration,arguments
shouldn’t refer to the outer function’sarguments
anymore. 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.parent
and so on: we should stop when we reach a non-arrow function scope, which defines the implicitarguments
binding.A few test cases:
If it is the first time that you contribute to Babel, follow these steps: (you need to have
make
andyarn
available on your machine)git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
yarn && make bootstrap
make watch
(ormake build
whenever you change a file)input.js
;output.js
will be automatically generated)yarn jest plugin-transform-arrow-functions
to run the testsoutput.js
files and run the tests againOVERWRITE=true yarn jest plugin-transform-arrow-functions
and they will be automatically updated.make test
to run all the testsgit push
and 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 watch
never ends. If you modify a file and save it, you’ll notice that themake watch
command 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.