Skip to content
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

arguments reference in an arrow function in ES5/ES6 #2430

Closed
JsonFreeman opened this issue Mar 19, 2015 · 4 comments · Fixed by #2802
Closed

arguments reference in an arrow function in ES5/ES6 #2430

JsonFreeman opened this issue Mar 19, 2015 · 4 comments · Fixed by #2802
Assignees
Labels
Bug A bug in TypeScript Discussion Issues which may not have code impact

Comments

@JsonFreeman
Copy link
Contributor

As of recently, we do not allow references to 'arguments' in an arrow function. So the following code will error:

function foo() {
     () => {
           arguments;
     }
}

Here is what the ES6 spec has to say in section 14.2.16:

An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, or this within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function.

From reading this, it seems that the reference to arguments should resolve to the arguments of foo, and should not be an error in ES6. This does not match our current semantics because we error, and our ES5 emit causes arguments to reference the arguments of the inner function.

Here are the questions:

  1. Should we error in ES5?
  2. Should we error in ES6?
  3. Should we change the ES5 emit to match ES6 semantics (emit an arguments capture similar to a this capture)?

Any solution will be some combination of yesses and noes to those 3 questions. Here are some options worth discussing:

  1. Today's behavior - always error in ES5 and ES6, and don't change the ES5 emit.
    • Cons: ES6 users will get an error on perfectly valid ES6 code. ES5 users get an error even though their code worked before.
  2. Error in ES5, but not in ES6. Keep the ES5 code binding to the arrow function.
    • Pros: Valid ES6 code works correctly, with no errors. ES5 behavior is preserved for users who want it, but they are warned that it will not work the same in ES6. So as long as they upgrade to ES6 after upgrading to our new compiler, they will have time and notice to update their code.
    • Cons: ES5 and ES6 have different semantics.
  3. Error in ES5, but change the emit to match the ES6 semantics.
    • Pros: ES5 and ES6 have the same semantics. ES5 users who exhibit a sudden change in behavior will get a nice error notifying them of the behavior change.
    • Cons: ES5 users will have to change code that was working fine before.
  4. No errors, keep the ES5 emit how it is.
    • Cons: ES5 and ES6 have different semantics with no user notification. The user will upgrade to -t es6 and suddenly get different behavior.
  5. No errors, change the ES5 emit to match ES6 semantics.
    • Pros: ES5 and ES6 have the same semantics.
    • Cons: When the user upgrades to TS 1.5, but keeps -t es5, they will get a sudden change in semantics with no notification.

Needless to say, every single one of these is a breaking change.

@JsonFreeman JsonFreeman added Bug A bug in TypeScript Discussion Issues which may not have code impact labels Mar 19, 2015
@danquirk
Copy link
Member

See #1609 for previous discussion.

@mhegazy mhegazy added this to the TypeScript 1.5 milestone Mar 25, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Apr 1, 2015

We discussed this in the last design meeting, and the conclusion was:

  • make this an error for ES5/ES3, at least for one release
  • Emit the correct argument binding, by capturing the arguments object of the parent and using it instead

@DanielRosenwasser
Copy link
Member

This is technically not closed yet since we are undecided on whether we should give more correct emit semantics. See changes in #2764.

@DanielRosenwasser
Copy link
Member

I think we've more or less decided not to include it.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Discussion Issues which may not have code impact
Projects
None yet
5 participants