Emit Arrow function natively in ES6

This commit is contained in:
Yui T
2015-01-06 18:18:37 -08:00
parent 436baafc72
commit b0ea40164c
12 changed files with 45 additions and 31 deletions

View File

@@ -3219,7 +3219,15 @@ module ts {
// Methods will emit the comments as part of emitting method declaration
emitLeadingComments(node);
}
write("function ");
if (node.kind !== SyntaxKind.ArrowFunction) {
write("function ");
}
else if (node.kind === SyntaxKind.ArrowFunction && compilerOptions.target < ScriptTarget.ES6) {
// When targeting ES6, emit arrow function natively in ES6 by omitting function keyword and using fat arrow instead
write("function ");
}
if (node.kind === SyntaxKind.FunctionDeclaration || (node.kind === SyntaxKind.FunctionExpression && node.name)) {
emit(node.name);
}
@@ -3258,6 +3266,12 @@ module ts {
tempVariables = undefined;
tempParameters = undefined;
emitSignatureParameters(node);
// When targeting ES6, emit arrow function natively in ES6
if (node.kind === SyntaxKind.ArrowFunction && compilerOptions.target >= ScriptTarget.ES6) {
write(" => ");
}
write(" {");
scopeEmitStart(node);
increaseIndent();

View File

@@ -165,7 +165,7 @@ class ProjectRunner extends RunnerBase {
sourceRoot: testCase.resolveSourceRoot && testCase.sourceRoot ? ts.sys.resolvePath(testCase.sourceRoot) : testCase.sourceRoot,
module: moduleKind,
noResolve: testCase.noResolve,
target: ts.ScriptTarget.ES6
target: ts.ScriptTarget.Latest
};
}

View File

@@ -220,7 +220,7 @@ function F() {
const c = 0;
n = c;
}
var F2 = function () {
var F2 = () => {
const c = 0;
n = c;
};
@@ -269,7 +269,7 @@ var o = {
const c = 0;
n = c;
},
f2: function () {
f2: () => {
const c = 0;
n = c;
}

View File

@@ -183,7 +183,7 @@ const c18 = 0;
function F() {
const c19 = 0;
}
var F2 = function () {
var F2 = () => {
const c20 = 0;
};
var F3 = function () {
@@ -223,7 +223,7 @@ var o = {
f() {
const c28 = 0;
},
f2: function () {
f2: () => {
const c29 = 0;
}
};

View File

@@ -236,7 +236,7 @@ function F() {
let l = 0;
n = l;
}
var F2 = function () {
var F2 = () => {
let l = 0;
n = l;
};
@@ -286,7 +286,7 @@ var o = {
let l = 0;
n = l;
},
f2: function () {
f2: () => {
let l = 0;
n = l;
}

View File

@@ -203,7 +203,7 @@ let l18 = 0;
function F() {
let l19 = 0;
}
var F2 = function () {
var F2 = () => {
let l20 = 0;
};
var F3 = function () {
@@ -243,7 +243,7 @@ var o = {
f() {
let l28 = 0;
},
f2: function () {
f2: () => {
let l29 = 0;
}
};

View File

@@ -1,6 +1,6 @@
var _this = this;
// Add a lambda to ensure global 'this' capture is triggered
(function () { return _this.window; });
(() => { return _this.window; });
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }

View File

@@ -1,6 +1,6 @@
var _this = this;
// Add a lambda to ensure global 'this' capture is triggered
(function () { return _this.window; });
(() => { return _this.window; });
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }

View File

@@ -111,40 +111,40 @@ someGenerics1b `${3}`;
// Generic tag with argument of function type whose parameter is of type parameter type
function someGenerics2a(strs, n) {
}
someGenerics2a `${function (n) { return n; }}`;
someGenerics2a `${(n) => { return n; }}`;
function someGenerics2b(strs, n) {
}
someGenerics2b `${function (n, x) { return n; }}`;
someGenerics2b `${(n, x) => { return n; }}`;
// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter
function someGenerics3(strs, producer) {
}
someGenerics3 `${function () { return ''; }}`;
someGenerics3 `${function () { return undefined; }}`;
someGenerics3 `${function () { return 3; }}`;
someGenerics3 `${() => { return ''; }}`;
someGenerics3 `${() => { return undefined; }}`;
someGenerics3 `${() => { return 3; }}`;
// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type
function someGenerics4(strs, n, f) {
}
someGenerics4 `${4}${function () { return null; }}`;
someGenerics4 `${''}${function () { return 3; }}`;
someGenerics4 `${4}${() => { return null; }}`;
someGenerics4 `${''}${() => { return 3; }}`;
someGenerics4 `${null}${null}`;
// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type
function someGenerics5(strs, n, f) {
}
someGenerics5 `${4} ${function () { return null; }}`;
someGenerics5 `${''}${function () { return 3; }}`;
someGenerics5 `${4} ${() => { return null; }}`;
someGenerics5 `${''}${() => { return 3; }}`;
someGenerics5 `${null}${null}`;
// Generic tag with multiple arguments of function types that each have parameters of the same generic type
function someGenerics6(strs, a, b, c) {
}
someGenerics6 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`;
someGenerics6 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`;
someGenerics6 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`;
someGenerics6 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
someGenerics6 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
someGenerics6 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
// Generic tag with multiple arguments of function types that each have parameters of different generic type
function someGenerics7(strs, a, b, c) {
}
someGenerics7 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`;
someGenerics7 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`;
someGenerics7 `${function (n) { return n; }}${function (n) { return n; }}${function (n) { return n; }}`;
someGenerics7 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
someGenerics7 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
someGenerics7 `${(n) => { return n; }}${(n) => { return n; }}${(n) => { return n; }}`;
// Generic tag with argument of generic function type
function someGenerics8(strs, n) {
return n;

View File

@@ -118,5 +118,5 @@ fn4 `${null}${true}`;
function fn5() {
return undefined;
}
fn5 `${function (n) { return n.toFixed(); }}`; // will error; 'n' should have type 'string'.
fn5 `${function (n) { return n.substr(0); }}`;
fn5 `${(n) => { return n.toFixed(); }}`; // will error; 'n' should have type 'string'.
fn5 `${(n) => { return n.substr(0); }}`;

View File

@@ -2,4 +2,4 @@
var x = x => `abc${ x }def`;
//// [templateStringInArrowFunctionES6.js]
var x = function (x) { return `abc${x}def`; };
var x = (x) => { return `abc${x}def`; };

View File

@@ -2,4 +2,4 @@
var x = `abc${ x => x }def`;
//// [templateStringWithEmbeddedArrowFunctionES6.js]
var x = `abc${function (x) { return x; }}def`;
var x = `abc${(x) => { return x; }}def`;