question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: unknown: Binding 'arguments' in strict mode (2:6)

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (14 by maintainers)

github_iconTop GitHub Comments

5reactions
nicolo-ribaudocommented, Apr 7, 2020

@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 and new.target to the outer scope, to preserve the correct semantics in cases like this one:

function f() {
  var g = () => console.log(arguments);
  g();
}

f(1, 2, 3); // should log 1, 2, 3

However, when there is a var arguments; declaration, arguments shouldn’t refer to the outer function’s arguments anymore. When replacing arguments, we need to check if it was defined as a variable in the local scope using argumentsChild.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 implicit arguments binding.

A few test cases:

var foo = () => {
  var arguments = 1;
  return arguments;
};
var foo = () => {
  var arguments = 1;
  return () => arguments;
};
function fn() {
  var arguments = 1;
  var foo = () => {
    return arguments;
  };
}
var arguments = 1;
function fn() {
  var foo = () => {
    return arguments;
  };
}

If it is the first time that you contribute to Babel, follow these steps: (you need to have make and yarn available on your machine)

  1. Write a comment there to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add a test (only input.js; output.js will be automatically generated)
  8. Update the code!
  9. yarn jest plugin-transform-arrow-functions to run the tests
    • If some test outputs don’t match but the new results are correct, you can delete the bad output.js files and run the tests again
    • If you prefer, you can run OVERWRITE=true yarn jest plugin-transform-arrow-functions and they will be automatically updated.
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!
2reactions
Yokubjon-Jcommented, Jun 27, 2020

I have started working on the issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strict Mode Issue with Binding Arguments - Stack Overflow
I have an issue with running a test on my files. Would love if someone more experienced could help me out here. the...
Read more >
Strict mode - JavaScript - MDN Web Docs
No syncing between parameters and arguments indices. Strict mode code doesn't sync indices of the arguments object with each parameter binding.
Read more >
JavaScript Mistakes — Strict Mode | by John Au-Yeung
With strict mode on, the this value passed into call , apply , or bind aren't boxed into an object. Therefore, the original...
Read more >
JavaScript Strict Mode
Attempting to perform an update on a property when its property definition defines otherwise will throw a TypeError in Strict Mode.
Read more >
ECMA-262-5 in detail. Chapter 2. Strict Mode.
In strict mode these names — eval and arguments are treated as kind of “keywords” (while they are not) and not allowed in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found