Skip to content

Emit arrow function es6 #1627

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

Merged
merged 30 commits into from
Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
436baaf
Add default target in compiler option of project runner
Jan 7, 2015
b0ea401
Emit Arrow function natively in ES6
Jan 7, 2015
7d0fc62
Emit this binding natively in es6
Jan 7, 2015
cb48a5e
Add testcases
Jan 7, 2015
ba239c5
Address code review
Jan 16, 2015
581beb5
Merge branch 'master' into emitArrowFunctionES6
Jan 21, 2015
de9547c
Update type checking for lexical binding due to merge with master
Jan 24, 2015
593a099
Update emitter due to merge with master
Jan 24, 2015
3115288
Update baseline and fix white space
Jan 24, 2015
f219a2d
Address code review; preserve users non-parenthesis
Jan 27, 2015
15b05e6
Merge branch 'master' into emitArrowFunctionES6
Jan 27, 2015
2e2559b
Update tests baseline from merging with master
Jan 27, 2015
ca3c1ed
Address the issue that arrow function doesn't have arguments objects
Jan 29, 2015
d5b953d
Add testcases
Jan 29, 2015
70140ef
Fix spacing due to tab
Jan 29, 2015
8d731d4
Address code review
Jan 29, 2015
2b200d4
Address code review
Jan 29, 2015
9b04180
Change tab to space
Jan 29, 2015
e4b206c
Merge branch 'master' into emitArrowFunctionES6
Jan 29, 2015
fd20695
Remove flag and compare position
Jan 30, 2015
fb2c502
Clean up the checking of position
Jan 30, 2015
cf5aadb
Address code review
Jan 30, 2015
5d0376f
Address codereview
Jan 30, 2015
6a0eaf5
Update an error
Jan 31, 2015
4162671
Address code review
Jan 31, 2015
ff038fb
Merge branch 'master' into emitArrowFunctionES6
Jan 31, 2015
a595a78
Remove tabs in json
Jan 31, 2015
53dffda
Merge branch 'master' into emitArrowFunctionES6
Feb 2, 2015
0d53542
Merge branch 'master' into emitArrowFunctionES6
Feb 2, 2015
122d587
Merge branch 'master' into emitArrowFunctionES6
Feb 2, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4836,6 +4836,16 @@ module ts {
function checkIdentifier(node: Identifier): Type {
var symbol = getResolvedSymbol(node);

// As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects.
// Although in down-level emit of arrow function, we emit it using function expression which means that
// arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects
// will be bound to non-arrow function that contain this arrow function. This results in inconsistent behaviour.
// To avoid that we will give an error to users if they use arguments objects in arrow function so that they
// can explicitly bound arguments objects
if (symbol === argumentsSymbol && getContainingFunction(node).kind === SyntaxKind.ArrowFunction) {
error(node, Diagnostics.An_argument_object_has_different_behaviour_across_Javascript_versions_Use_function_expression_or_rest_parameters_instead);
}

if (symbol.flags & SymbolFlags.Import) {
var symbolLinks = getSymbolLinks(symbol);
if (!symbolLinks.referenced) {
Expand Down Expand Up @@ -4890,7 +4900,14 @@ module ts {
// Now skip arrow functions to get the "real" owner of 'this'.
if (container.kind === SyntaxKind.ArrowFunction) {
container = getThisContainer(container, /* includeArrowFunctions */ false);
needToCaptureLexicalThis = true;

// When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code
if (compilerOptions.target >= ScriptTarget.ES6) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce this to one branch, it's already set to false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needToCaptureLexicalThis = compilerOptions.target >= ScriptTarget.ES6

needToCaptureLexicalThis = false;
}
else {
needToCaptureLexicalThis = true;
}
}

switch (container.kind) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,5 +452,6 @@ module ts {
You_cannot_rename_this_element: { code: 8000, category: DiagnosticCategory.Error, key: "You cannot rename this element." },
yield_expressions_are_not_currently_supported: { code: 9000, category: DiagnosticCategory.Error, key: "'yield' expressions are not currently supported.", isEarly: true },
Generators_are_not_currently_supported: { code: 9001, category: DiagnosticCategory.Error, key: "Generators are not currently supported.", isEarly: true },
An_argument_object_has_different_behaviour_across_Javascript_versions_Use_function_expression_or_rest_parameters_instead: { code: 9002, category: DiagnosticCategory.Error, key: "An argument object has different behaviour across Javascript versions. Use function expression or rest parameters instead" },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to

The 'arguments' object is not allowed in an arrow function. Use a function expression or rest parameters instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

};
}
Loading