diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a3309937cc548..53dc1bf3445bd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4861,6 +4861,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 behavior. + // 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.The_arguments_object_cannot_be_referenced_in_an_arrow_function_Consider_using_a_standard_function_expression); + } + if (symbol.flags & SymbolFlags.Import) { var symbolLinks = getSymbolLinks(symbol); if (!symbolLinks.referenced) { @@ -4915,7 +4925,9 @@ 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 + needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES6); } switch (container.kind) { diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index f333520733e3f..94a026a185860 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -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 }, + The_arguments_object_cannot_be_referenced_in_an_arrow_function_Consider_using_a_standard_function_expression: { code: 9002, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression." }, }; } \ No newline at end of file diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 49ee8eebcd9bf..a20f6adf6491e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1612,7 +1612,7 @@ "Property '{0}' does not exist on 'const' enum '{1}'.": { "category": "Error", "code": 4088, - "isEarly": true + "isEarly": true }, "The current host does not support the '{0}' option.": { "category": "Error", @@ -1902,11 +1902,15 @@ "'yield' expressions are not currently supported.": { "category": "Error", "code": 9000, - "isEarly": true + "isEarly": true }, "Generators are not currently supported.": { "category": "Error", "code": 9001, - "isEarly": true + "isEarly": true + }, + "The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.": { + "category": "Error", + "code": 9002 } } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 34978ed7fd617..c33f7fd5234b4 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1487,7 +1487,6 @@ module ts { // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compilerOnSave feature export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile?: SourceFile): EmitResult { - // var program = resolver.getProgram(); var compilerOptions = host.getCompilerOptions(); var languageVersion = compilerOptions.target || ScriptTarget.ES3; var sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap ? [] : undefined; @@ -3240,6 +3239,10 @@ module ts { emitSignatureAndBody(node); } + function shouldEmitAsArrowFunction(node: FunctionLikeDeclaration): boolean { + return node.kind === SyntaxKind.ArrowFunction && languageVersion >= ScriptTarget.ES6; + } + function emitFunctionDeclaration(node: FunctionLikeDeclaration) { if (nodeIsMissing(node.body)) { return emitPinnedOrTripleSlashComments(node); @@ -3249,7 +3252,13 @@ module ts { // Methods will emit the comments as part of emitting method declaration emitLeadingComments(node); } - write("function "); + + // For targeting below es6, emit functions-like declaration including arrow function using function keyword. + // When targeting ES6, emit arrow function natively in ES6 by omitting function keyword and using fat arrow instead + if (!shouldEmitAsArrowFunction(node)) { + write("function "); + } + if (node.kind === SyntaxKind.FunctionDeclaration || (node.kind === SyntaxKind.FunctionExpression && node.name)) { emit(node.name); } @@ -3280,6 +3289,15 @@ module ts { decreaseIndent(); } + function emitSignatureParametersForArrow(node: FunctionLikeDeclaration) { + // Check whether the parameter list needs parentheses and preserve no-parenthesis + if (node.parameters.length === 1 && node.pos === node.parameters[0].pos) { + emit(node.parameters[0]); + return; + } + emitSignatureParameters(node); + } + function emitSignatureAndBody(node: FunctionLikeDeclaration) { var saveTempCount = tempCount; var saveTempVariables = tempVariables; @@ -3287,7 +3305,16 @@ module ts { tempCount = 0; tempVariables = undefined; tempParameters = undefined; - emitSignatureParameters(node); + + // When targeting ES6, emit arrow function natively in ES6 + if (shouldEmitAsArrowFunction(node)) { + emitSignatureParametersForArrow(node); + write(" =>"); + } + else { + emitSignatureParameters(node); + } + write(" {"); scopeEmitStart(node); increaseIndent(); @@ -3299,6 +3326,7 @@ module ts { startIndex = emitDirectivePrologues((node.body).statements, /*startWithNewLine*/ true); } var outPos = writer.getTextPos(); + emitCaptureThisForNodeIfNecessary(node); emitDefaultValueAssignments(node); emitRestParameter(node); diff --git a/src/harness/harness.ts b/src/harness/harness.ts index df73e52a605a3..166ae3c7d63d7 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -107,7 +107,7 @@ module Utils { export function memoize(f: T): T { var cache: { [idx: string]: any } = {}; - return (() => { + return (function () { var key = Array.prototype.join.call(arguments); var cachedResult = cache[key]; if (cachedResult) { diff --git a/src/harness/loggedIO.ts b/src/harness/loggedIO.ts index c6d15c34a84dd..570352625815d 100644 --- a/src/harness/loggedIO.ts +++ b/src/harness/loggedIO.ts @@ -138,7 +138,7 @@ module Playback { function recordReplay(original: T, underlying: any) { function createWrapper(record: T, replay: T): T { - return (() => { + return (function () { if (replayLog !== undefined) { return replay.apply(undefined, arguments); } else if (recordLog !== undefined) { diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 52467fdffd610..9b16e93d8eb9f 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -186,7 +186,7 @@ class ProjectRunner extends RunnerBase { function createCompilerHost(): ts.CompilerHost { return { getSourceFile, - getDefaultLibFilename: options => options.target === ts.ScriptTarget.ES6 ? "lib.es6.d.ts" : "lib.d.ts", + getDefaultLibFilename: options => Harness.Compiler.defaultLibFileName, writeFile, getCurrentDirectory, getCanonicalFileName: Harness.Compiler.getCanonicalFileName, diff --git a/tests/baselines/reference/computedPropertyNames29.js b/tests/baselines/reference/computedPropertyNames29.js index dc45746c63ce1..94e83f3462916 100644 --- a/tests/baselines/reference/computedPropertyNames29.js +++ b/tests/baselines/reference/computedPropertyNames29.js @@ -15,10 +15,9 @@ var C = (function () { function C() { } C.prototype.bar = function () { - var _this = this; - (function () { + (() => { var obj = { - [_this.bar()]() { + [this.bar()]() { } // needs capture }; }); diff --git a/tests/baselines/reference/computedPropertyNames3.js b/tests/baselines/reference/computedPropertyNames3.js index fbd76bf48fc74..969bbf96d865a 100644 --- a/tests/baselines/reference/computedPropertyNames3.js +++ b/tests/baselines/reference/computedPropertyNames3.js @@ -16,7 +16,7 @@ var C = (function () { } C.prototype[0 + 1] = function () { }; - C[function () { + C[() => { }] = function () { }; Object.defineProperty(C.prototype, delete id, { diff --git a/tests/baselines/reference/computedPropertyNames30.js b/tests/baselines/reference/computedPropertyNames30.js index c10ba51316db8..608531ffb5bbc 100644 --- a/tests/baselines/reference/computedPropertyNames30.js +++ b/tests/baselines/reference/computedPropertyNames30.js @@ -31,7 +31,7 @@ var C = (function (_super) { __extends(C, _super); function C() { _super.call(this); - (function () { + (() => { var obj = { // Ideally, we would capture this. But the reference is // illegal, and not capturing this is consistent with diff --git a/tests/baselines/reference/computedPropertyNames31.js b/tests/baselines/reference/computedPropertyNames31.js index bf75ac2be74e7..9a31b80e34687 100644 --- a/tests/baselines/reference/computedPropertyNames31.js +++ b/tests/baselines/reference/computedPropertyNames31.js @@ -37,7 +37,7 @@ var C = (function (_super) { } C.prototype.foo = function () { var _this = this; - (function () { + (() => { var obj = { [_super.prototype.bar.call(_this)]() { } // needs capture diff --git a/tests/baselines/reference/computedPropertyNamesContextualType1.js b/tests/baselines/reference/computedPropertyNamesContextualType1.js index d7704c8c377a9..82429be9c404f 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType1.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType1.js @@ -14,5 +14,5 @@ var o = { ["" + 0](y) { return y.length; }, - ["" + 1]: function (y) { return y.length; } + ["" + 1]: y => { return y.length; } }; diff --git a/tests/baselines/reference/computedPropertyNamesContextualType2.js b/tests/baselines/reference/computedPropertyNamesContextualType2.js index 7b9f98402f54c..028e797be18ff 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType2.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType2.js @@ -14,5 +14,5 @@ var o = { [+"foo"](y) { return y.length; }, - [+"bar"]: function (y) { return y.length; } + [+"bar"]: y => { return y.length; } }; diff --git a/tests/baselines/reference/computedPropertyNamesContextualType3.js b/tests/baselines/reference/computedPropertyNamesContextualType3.js index f5b2c71bc186e..944bb3595d41b 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType3.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType3.js @@ -13,5 +13,5 @@ var o = { [+"foo"](y) { return y.length; }, - [+"bar"]: function (y) { return y.length; } + [+"bar"]: y => { return y.length; } }; diff --git a/tests/baselines/reference/computedPropertyNamesContextualType6.js b/tests/baselines/reference/computedPropertyNamesContextualType6.js index 5d0cc149b2c3e..a92958871d9d4 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType6.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType6.js @@ -16,7 +16,7 @@ foo({ //// [computedPropertyNamesContextualType6.js] foo({ p: "", - 0: function () { + 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7.js b/tests/baselines/reference/computedPropertyNamesContextualType7.js index 74030be6f2af1..00103bc76db7a 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType7.js @@ -16,7 +16,7 @@ foo({ //// [computedPropertyNamesContextualType7.js] foo({ p: "", - 0: function () { + 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, diff --git a/tests/baselines/reference/constDeclarations-scopes.js b/tests/baselines/reference/constDeclarations-scopes.js index b1be921849c4c..f8fbc47312b1b 100644 --- a/tests/baselines/reference/constDeclarations-scopes.js +++ b/tests/baselines/reference/constDeclarations-scopes.js @@ -223,7 +223,7 @@ function F() { const c = 0; n = c; } -var F2 = function () { +var F2 = () => { const c = 0; n = c; }; @@ -272,7 +272,7 @@ var o = { const c = 0; n = c; }, - f2: function () { + f2: () => { const c = 0; n = c; } diff --git a/tests/baselines/reference/constDeclarations-validContexts.js b/tests/baselines/reference/constDeclarations-validContexts.js index 8238f69ca15b7..5a54e915713ad 100644 --- a/tests/baselines/reference/constDeclarations-validContexts.js +++ b/tests/baselines/reference/constDeclarations-validContexts.js @@ -186,7 +186,7 @@ const c18 = 0; function F() { const c19 = 0; } -var F2 = function () { +var F2 = () => { const c20 = 0; }; var F3 = function () { @@ -226,7 +226,7 @@ var o = { f() { const c28 = 0; }, - f2: function () { + f2: () => { const c29 = 0; } }; diff --git a/tests/baselines/reference/emitArrowFunction.js b/tests/baselines/reference/emitArrowFunction.js new file mode 100644 index 0000000000000..1c00a7579b6f8 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunction.js @@ -0,0 +1,29 @@ +//// [emitArrowFunction.ts] +var f1 = () => { } +var f2 = (x: string, y: string) => { } +var f3 = (x: string, y: number, ...rest) => { } +var f4 = (x: string, y: number, z = 10) => { } +function foo(func: () => boolean) { } +foo(() => true); +foo(() => { return false; }); + +//// [emitArrowFunction.js] +var f1 = function () { +}; +var f2 = function (x, y) { +}; +var f3 = function (x, y) { + var rest = []; + for (var _i = 2; _i < arguments.length; _i++) { + rest[_i - 2] = arguments[_i]; + } +}; +var f4 = function (x, y, z) { + if (z === void 0) { z = 10; } +}; +function foo(func) { +} +foo(function () { return true; }); +foo(function () { + return false; +}); diff --git a/tests/baselines/reference/emitArrowFunction.types b/tests/baselines/reference/emitArrowFunction.types new file mode 100644 index 0000000000000..030ed0cde60d5 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunction.types @@ -0,0 +1,39 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunction.ts === +var f1 = () => { } +>f1 : () => void +>() => { } : () => void + +var f2 = (x: string, y: string) => { } +>f2 : (x: string, y: string) => void +>(x: string, y: string) => { } : (x: string, y: string) => void +>x : string +>y : string + +var f3 = (x: string, y: number, ...rest) => { } +>f3 : (x: string, y: number, ...rest: any[]) => void +>(x: string, y: number, ...rest) => { } : (x: string, y: number, ...rest: any[]) => void +>x : string +>y : number +>rest : any[] + +var f4 = (x: string, y: number, z = 10) => { } +>f4 : (x: string, y: number, z?: number) => void +>(x: string, y: number, z = 10) => { } : (x: string, y: number, z?: number) => void +>x : string +>y : number +>z : number + +function foo(func: () => boolean) { } +>foo : (func: () => boolean) => void +>func : () => boolean + +foo(() => true); +>foo(() => true) : void +>foo : (func: () => boolean) => void +>() => true : () => boolean + +foo(() => { return false; }); +>foo(() => { return false; }) : void +>foo : (func: () => boolean) => void +>() => { return false; } : () => boolean + diff --git a/tests/baselines/reference/emitArrowFunctionAsIs.js b/tests/baselines/reference/emitArrowFunctionAsIs.js new file mode 100644 index 0000000000000..11df7903ad8d3 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionAsIs.js @@ -0,0 +1,13 @@ +//// [emitArrowFunctionAsIs.ts] +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; + +//// [emitArrowFunctionAsIs.js] +var arrow1 = function (a) { +}; +var arrow2 = function (a) { +}; +var arrow3 = function (a, b) { +}; diff --git a/tests/baselines/reference/emitArrowFunctionAsIs.types b/tests/baselines/reference/emitArrowFunctionAsIs.types new file mode 100644 index 0000000000000..1ab5de9724a77 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionAsIs.types @@ -0,0 +1,17 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIs.ts === +var arrow1 = a => { }; +>arrow1 : (a: any) => void +>a => { } : (a: any) => void +>a : any + +var arrow2 = (a) => { }; +>arrow2 : (a: any) => void +>(a) => { } : (a: any) => void +>a : any + +var arrow3 = (a, b) => { }; +>arrow3 : (a: any, b: any) => void +>(a, b) => { } : (a: any, b: any) => void +>a : any +>b : any + diff --git a/tests/baselines/reference/emitArrowFunctionAsIsES6.js b/tests/baselines/reference/emitArrowFunctionAsIsES6.js new file mode 100644 index 0000000000000..9941434ae1097 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionAsIsES6.js @@ -0,0 +1,13 @@ +//// [emitArrowFunctionAsIsES6.ts] +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; + +//// [emitArrowFunctionAsIsES6.js] +var arrow1 = a => { +}; +var arrow2 = (a) => { +}; +var arrow3 = (a, b) => { +}; diff --git a/tests/baselines/reference/emitArrowFunctionAsIsES6.types b/tests/baselines/reference/emitArrowFunctionAsIsES6.types new file mode 100644 index 0000000000000..6b8c9e54bbe4c --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionAsIsES6.types @@ -0,0 +1,17 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIsES6.ts === +var arrow1 = a => { }; +>arrow1 : (a: any) => void +>a => { } : (a: any) => void +>a : any + +var arrow2 = (a) => { }; +>arrow2 : (a: any) => void +>(a) => { } : (a: any) => void +>a : any + +var arrow3 = (a, b) => { }; +>arrow3 : (a: any, b: any) => void +>(a, b) => { } : (a: any, b: any) => void +>a : any +>b : any + diff --git a/tests/baselines/reference/emitArrowFunctionES6.js b/tests/baselines/reference/emitArrowFunctionES6.js new file mode 100644 index 0000000000000..c518a52f62986 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionES6.js @@ -0,0 +1,25 @@ +//// [emitArrowFunctionES6.ts] +var f1 = () => { } +var f2 = (x: string, y: string) => { } +var f3 = (x: string, y: number, ...rest) => { } +var f4 = (x: string, y: number, z=10) => { } +function foo(func: () => boolean) { } +foo(() => true); +foo(() => { return false; }); + + +//// [emitArrowFunctionES6.js] +var f1 = () => { +}; +var f2 = (x, y) => { +}; +var f3 = (x, y, ...rest) => { +}; +var f4 = (x, y, z = 10) => { +}; +function foo(func) { +} +foo(() => { return true; }); +foo(() => { + return false; +}); diff --git a/tests/baselines/reference/emitArrowFunctionES6.types b/tests/baselines/reference/emitArrowFunctionES6.types new file mode 100644 index 0000000000000..1f0c1941bdbcd --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionES6.types @@ -0,0 +1,39 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionES6.ts === +var f1 = () => { } +>f1 : () => void +>() => { } : () => void + +var f2 = (x: string, y: string) => { } +>f2 : (x: string, y: string) => void +>(x: string, y: string) => { } : (x: string, y: string) => void +>x : string +>y : string + +var f3 = (x: string, y: number, ...rest) => { } +>f3 : (x: string, y: number, ...rest: any[]) => void +>(x: string, y: number, ...rest) => { } : (x: string, y: number, ...rest: any[]) => void +>x : string +>y : number +>rest : any[] + +var f4 = (x: string, y: number, z=10) => { } +>f4 : (x: string, y: number, z?: number) => void +>(x: string, y: number, z=10) => { } : (x: string, y: number, z?: number) => void +>x : string +>y : number +>z : number + +function foo(func: () => boolean) { } +>foo : (func: () => boolean) => void +>func : () => boolean + +foo(() => true); +>foo(() => true) : void +>foo : (func: () => boolean) => void +>() => true : () => boolean + +foo(() => { return false; }); +>foo(() => { return false; }) : void +>foo : (func: () => boolean) => void +>() => { return false; } : () => boolean + diff --git a/tests/baselines/reference/emitArrowFunctionThisCapturing.js b/tests/baselines/reference/emitArrowFunctionThisCapturing.js new file mode 100644 index 0000000000000..25d9f7f100b0c --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionThisCapturing.js @@ -0,0 +1,30 @@ +//// [emitArrowFunctionThisCapturing.ts] +var f1 = () => { + this.age = 10 +}; + +var f2 = (x: string) => { + this.name = x +} + +function foo(func: () => boolean) { } +foo(() => { + this.age = 100; + return true; +}); + + +//// [emitArrowFunctionThisCapturing.js] +var _this = this; +var f1 = function () { + _this.age = 10; +}; +var f2 = function (x) { + _this.name = x; +}; +function foo(func) { +} +foo(function () { + _this.age = 100; + return true; +}); diff --git a/tests/baselines/reference/emitArrowFunctionThisCapturing.types b/tests/baselines/reference/emitArrowFunctionThisCapturing.types new file mode 100644 index 0000000000000..433f38e5ecc3f --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionThisCapturing.types @@ -0,0 +1,44 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturing.ts === +var f1 = () => { +>f1 : () => void +>() => { this.age = 10} : () => void + + this.age = 10 +>this.age = 10 : number +>this.age : any +>this : any +>age : any + +}; + +var f2 = (x: string) => { +>f2 : (x: string) => void +>(x: string) => { this.name = x} : (x: string) => void +>x : string + + this.name = x +>this.name = x : string +>this.name : any +>this : any +>name : any +>x : string +} + +function foo(func: () => boolean) { } +>foo : (func: () => boolean) => void +>func : () => boolean + +foo(() => { +>foo(() => { this.age = 100; return true;}) : void +>foo : (func: () => boolean) => void +>() => { this.age = 100; return true;} : () => boolean + + this.age = 100; +>this.age = 100 : number +>this.age : any +>this : any +>age : any + + return true; +}); + diff --git a/tests/baselines/reference/emitArrowFunctionThisCapturingES6.js b/tests/baselines/reference/emitArrowFunctionThisCapturingES6.js new file mode 100644 index 0000000000000..bf235bc0eef43 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionThisCapturingES6.js @@ -0,0 +1,29 @@ +//// [emitArrowFunctionThisCapturingES6.ts] +var f1 = () => { + this.age = 10 +}; + +var f2 = (x: string) => { + this.name = x +} + +function foo(func: () => boolean){ } +foo(() => { + this.age = 100; + return true; +}); + + +//// [emitArrowFunctionThisCapturingES6.js] +var f1 = () => { + this.age = 10; +}; +var f2 = (x) => { + this.name = x; +}; +function foo(func) { +} +foo(() => { + this.age = 100; + return true; +}); diff --git a/tests/baselines/reference/emitArrowFunctionThisCapturingES6.types b/tests/baselines/reference/emitArrowFunctionThisCapturingES6.types new file mode 100644 index 0000000000000..989130ef280e7 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionThisCapturingES6.types @@ -0,0 +1,44 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturingES6.ts === +var f1 = () => { +>f1 : () => void +>() => { this.age = 10} : () => void + + this.age = 10 +>this.age = 10 : number +>this.age : any +>this : any +>age : any + +}; + +var f2 = (x: string) => { +>f2 : (x: string) => void +>(x: string) => { this.name = x} : (x: string) => void +>x : string + + this.name = x +>this.name = x : string +>this.name : any +>this : any +>name : any +>x : string +} + +function foo(func: () => boolean){ } +>foo : (func: () => boolean) => void +>func : () => boolean + +foo(() => { +>foo(() => { this.age = 100; return true;}) : void +>foo : (func: () => boolean) => void +>() => { this.age = 100; return true;} : () => boolean + + this.age = 100; +>this.age = 100 : number +>this.age : any +>this : any +>age : any + + return true; +}); + diff --git a/tests/baselines/reference/emitArrowFunctionWhenUsingArguments.errors.txt b/tests/baselines/reference/emitArrowFunctionWhenUsingArguments.errors.txt new file mode 100644 index 0000000000000..3a0ab0e597c9b --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionWhenUsingArguments.errors.txt @@ -0,0 +1,46 @@ +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(2,15): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(7,19): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(13,13): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(19,15): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + + +==== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts (4 errors) ==== + var a = () => { + var arg = arguments[0]; // error + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + } + + var b = function () { + var a = () => { + var arg = arguments[0]; // error + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + } + } + + function baz() { + () => { + var arg = arguments[0]; + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + } + } + + function foo(inputFunc: () => void) { } + foo(() => { + var arg = arguments[0]; // error + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + }); + + function bar() { + var arg = arguments[0]; // no error + } + + + () => { + function foo() { + var arg = arguments[0]; // no error + } + } \ No newline at end of file diff --git a/tests/baselines/reference/emitArrowFunctionWhenUsingArguments.js b/tests/baselines/reference/emitArrowFunctionWhenUsingArguments.js new file mode 100644 index 0000000000000..00a4c7aa0510e --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionWhenUsingArguments.js @@ -0,0 +1,60 @@ +//// [emitArrowFunctionWhenUsingArguments.ts] +var a = () => { + var arg = arguments[0]; // error +} + +var b = function () { + var a = () => { + var arg = arguments[0]; // error + } +} + +function baz() { + () => { + var arg = arguments[0]; + } +} + +function foo(inputFunc: () => void) { } +foo(() => { + var arg = arguments[0]; // error +}); + +function bar() { + var arg = arguments[0]; // no error +} + + +() => { + function foo() { + var arg = arguments[0]; // no error + } +} + +//// [emitArrowFunctionWhenUsingArguments.js] +var a = () => { + var arg = arguments[0]; // error +}; +var b = function () { + var a = () => { + var arg = arguments[0]; // error + }; +}; +function baz() { + (() => { + var arg = arguments[0]; + }); +} +function foo(inputFunc) { +} +foo(() => { + var arg = arguments[0]; // error +}); +function bar() { + var arg = arguments[0]; // no error +} +(() => { + function foo() { + var arg = arguments[0]; // no error + } +}); diff --git a/tests/baselines/reference/emitArrowFunctionWhenUsingArgumentsES6.errors.txt b/tests/baselines/reference/emitArrowFunctionWhenUsingArgumentsES6.errors.txt new file mode 100644 index 0000000000000..f879f11799b15 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionWhenUsingArgumentsES6.errors.txt @@ -0,0 +1,46 @@ +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts(2,15): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts(7,19): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts(13,13): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. +tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts(19,15): error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + + +==== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts (4 errors) ==== + var a = () => { + var arg = arguments[0]; // error + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + } + + var b = function () { + var a = () => { + var arg = arguments[0]; // error + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + } + } + + function baz() { + () => { + var arg = arguments[0]; + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + } + } + + function foo(inputFunc: () => void) { } + foo(() => { + var arg = arguments[0]; // error + ~~~~~~~~~ +!!! error TS9002: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression. + }); + + function bar() { + var arg = arguments[0]; // no error + } + + + () => { + function foo() { + var arg = arguments[0]; // no error + } + } \ No newline at end of file diff --git a/tests/baselines/reference/emitArrowFunctionWhenUsingArgumentsES6.js b/tests/baselines/reference/emitArrowFunctionWhenUsingArgumentsES6.js new file mode 100644 index 0000000000000..0deacd409df5b --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionWhenUsingArgumentsES6.js @@ -0,0 +1,60 @@ +//// [emitArrowFunctionWhenUsingArgumentsES6.ts] +var a = () => { + var arg = arguments[0]; // error +} + +var b = function () { + var a = () => { + var arg = arguments[0]; // error + } +} + +function baz() { + () => { + var arg = arguments[0]; + } +} + +function foo(inputFunc: () => void) { } +foo(() => { + var arg = arguments[0]; // error +}); + +function bar() { + var arg = arguments[0]; // no error +} + + +() => { + function foo() { + var arg = arguments[0]; // no error + } +} + +//// [emitArrowFunctionWhenUsingArgumentsES6.js] +var a = () => { + var arg = arguments[0]; // error +}; +var b = function () { + var a = () => { + var arg = arguments[0]; // error + }; +}; +function baz() { + (() => { + var arg = arguments[0]; + }); +} +function foo(inputFunc) { +} +foo(() => { + var arg = arguments[0]; // error +}); +function bar() { + var arg = arguments[0]; // no error +} +(() => { + function foo() { + var arg = arguments[0]; // no error + } +}); diff --git a/tests/baselines/reference/emitArrowFunctionsAsIs.js b/tests/baselines/reference/emitArrowFunctionsAsIs.js new file mode 100644 index 0000000000000..ee8347dda3e10 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionsAsIs.js @@ -0,0 +1,13 @@ +//// [emitArrowFunctionsAsIs.ts] +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; + +//// [emitArrowFunctionsAsIs.js] +var arrow1 = function (a) { +}; +var arrow2 = function (a) { +}; +var arrow3 = function (a, b) { +}; diff --git a/tests/baselines/reference/emitArrowFunctionsAsIs.types b/tests/baselines/reference/emitArrowFunctionsAsIs.types new file mode 100644 index 0000000000000..36dae7e9c5a4b --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionsAsIs.types @@ -0,0 +1,17 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIs.ts === +var arrow1 = a => { }; +>arrow1 : (a: any) => void +>a => { } : (a: any) => void +>a : any + +var arrow2 = (a) => { }; +>arrow2 : (a: any) => void +>(a) => { } : (a: any) => void +>a : any + +var arrow3 = (a, b) => { }; +>arrow3 : (a: any, b: any) => void +>(a, b) => { } : (a: any, b: any) => void +>a : any +>b : any + diff --git a/tests/baselines/reference/emitArrowFunctionsAsIsES6.js b/tests/baselines/reference/emitArrowFunctionsAsIsES6.js new file mode 100644 index 0000000000000..021e048ed8866 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionsAsIsES6.js @@ -0,0 +1,13 @@ +//// [emitArrowFunctionsAsIsES6.ts] +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; + +//// [emitArrowFunctionsAsIsES6.js] +var arrow1 = a => { +}; +var arrow2 = (a) => { +}; +var arrow3 = (a, b) => { +}; diff --git a/tests/baselines/reference/emitArrowFunctionsAsIsES6.types b/tests/baselines/reference/emitArrowFunctionsAsIsES6.types new file mode 100644 index 0000000000000..4073355d259f0 --- /dev/null +++ b/tests/baselines/reference/emitArrowFunctionsAsIsES6.types @@ -0,0 +1,17 @@ +=== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIsES6.ts === +var arrow1 = a => { }; +>arrow1 : (a: any) => void +>a => { } : (a: any) => void +>a : any + +var arrow2 = (a) => { }; +>arrow2 : (a: any) => void +>(a) => { } : (a: any) => void +>a : any + +var arrow3 = (a, b) => { }; +>arrow3 : (a: any, b: any) => void +>(a, b) => { } : (a: any, b: any) => void +>a : any +>b : any + diff --git a/tests/baselines/reference/emitDefaultParametersFunctionExpressionES6.js b/tests/baselines/reference/emitDefaultParametersFunctionExpressionES6.js index eabb0e1e34496..5a49a200c6b57 100644 --- a/tests/baselines/reference/emitDefaultParametersFunctionExpressionES6.js +++ b/tests/baselines/reference/emitDefaultParametersFunctionExpressionES6.js @@ -9,13 +9,13 @@ var y = (function (num = 10, boo = false, ...rest) { })() var z = (function (num: number, boo = false, ...rest) { })(10) //// [emitDefaultParametersFunctionExpressionES6.js] -var lambda1 = function (y = "hello") { +var lambda1 = (y = "hello") => { }; -var lambda2 = function (x, y = "hello") { +var lambda2 = (x, y = "hello") => { }; -var lambda3 = function (x, y = "hello", ...rest) { +var lambda3 = (x, y = "hello", ...rest) => { }; -var lambda4 = function (y = "hello", ...rest) { +var lambda4 = (y = "hello", ...rest) => { }; var x = function (str = "hello", ...rest) { }; diff --git a/tests/baselines/reference/emitRestParametersFunctionExpressionES6.js b/tests/baselines/reference/emitRestParametersFunctionExpressionES6.js index 6c79e4956ae08..c7851db5e780f 100644 --- a/tests/baselines/reference/emitRestParametersFunctionExpressionES6.js +++ b/tests/baselines/reference/emitRestParametersFunctionExpressionES6.js @@ -5,9 +5,9 @@ var funcExp2 = function (...rest) { } var funcExp3 = (function (...rest) { })() //// [emitRestParametersFunctionExpressionES6.js] -var funcExp = function (...rest) { +var funcExp = (...rest) => { }; -var funcExp1 = function (X, ...rest) { +var funcExp1 = (X, ...rest) => { }; var funcExp2 = function (...rest) { }; diff --git a/tests/baselines/reference/letDeclarations-scopes.js b/tests/baselines/reference/letDeclarations-scopes.js index dbe18311eff7f..fc7218079940d 100644 --- a/tests/baselines/reference/letDeclarations-scopes.js +++ b/tests/baselines/reference/letDeclarations-scopes.js @@ -239,7 +239,7 @@ function F() { let l = 0; n = l; } -var F2 = function () { +var F2 = () => { let l = 0; n = l; }; @@ -289,7 +289,7 @@ var o = { let l = 0; n = l; }, - f2: function () { + f2: () => { let l = 0; n = l; } diff --git a/tests/baselines/reference/letDeclarations-validContexts.js b/tests/baselines/reference/letDeclarations-validContexts.js index ee821794b12d1..f6d404135f695 100644 --- a/tests/baselines/reference/letDeclarations-validContexts.js +++ b/tests/baselines/reference/letDeclarations-validContexts.js @@ -206,7 +206,7 @@ let l18 = 0; function F() { let l19 = 0; } -var F2 = function () { +var F2 = () => { let l20 = 0; }; var F3 = function () { @@ -246,7 +246,7 @@ var o = { f() { let l28 = 0; }, - f2: function () { + f2: () => { let l29 = 0; } }; diff --git a/tests/baselines/reference/parenthesizedContexualTyping3.js b/tests/baselines/reference/parenthesizedContexualTyping3.js index 69784d14ce404..cc6dbd44f88f4 100644 --- a/tests/baselines/reference/parenthesizedContexualTyping3.js +++ b/tests/baselines/reference/parenthesizedContexualTyping3.js @@ -25,11 +25,11 @@ var h = tempFun `${ (x => x) } ${ (((x => x))) } ${ undefined }` function tempFun(tempStrs, g, x) { return g(x); } -var a = tempFun `${function (x) { return x; }} ${10}`; -var b = tempFun `${(function (x) { return x; })} ${10}`; -var c = tempFun `${((function (x) { return x; }))} ${10}`; -var d = tempFun `${function (x) { return x; }} ${function (x) { return x; }} ${10}`; -var e = tempFun `${function (x) { return x; }} ${(function (x) { return x; })} ${10}`; -var f = tempFun `${function (x) { return x; }} ${((function (x) { return x; }))} ${10}`; -var g = tempFun `${(function (x) { return x; })} ${(((function (x) { return x; })))} ${10}`; -var h = tempFun `${(function (x) { return x; })} ${(((function (x) { return x; })))} ${undefined}`; +var a = tempFun `${x => { return x; }} ${10}`; +var b = tempFun `${(x => { return x; })} ${10}`; +var c = tempFun `${((x => { return x; }))} ${10}`; +var d = tempFun `${x => { return x; }} ${x => { return x; }} ${10}`; +var e = tempFun `${x => { return x; }} ${(x => { return x; })} ${10}`; +var f = tempFun `${x => { return x; }} ${((x => { return x; }))} ${10}`; +var g = tempFun `${(x => { return x; })} ${(((x => { return x; })))} ${10}`; +var h = tempFun `${(x => { return x; })} ${(((x => { return x; })))} ${undefined}`; diff --git a/tests/baselines/reference/taggedTemplateContextualTyping1.js b/tests/baselines/reference/taggedTemplateContextualTyping1.js index c1ae5c384e453..173d0f56bb722 100644 --- a/tests/baselines/reference/taggedTemplateContextualTyping1.js +++ b/tests/baselines/reference/taggedTemplateContextualTyping1.js @@ -26,28 +26,28 @@ function tempTag1(...rest) { // Otherwise, the arrow functions' parameters will be typed as 'any', // and it is an error to invoke an any-typed value with type arguments, // so this test will error. -tempTag1 `${function (x) { +tempTag1 `${x => { x(undefined); return x; }}${10}`; -tempTag1 `${function (x) { +tempTag1 `${x => { x(undefined); return x; -}}${function (y) { +}}${y => { y(undefined); return y; }}${10}`; -tempTag1 `${function (x) { +tempTag1 `${x => { x(undefined); return x; -}}${function (y) { +}}${(y) => { y(undefined); return y; }}${undefined}`; -tempTag1 `${function (x) { +tempTag1 `${(x) => { x(undefined); return x; -}}${function (y) { +}}${y => { y(undefined); return y; }}${undefined}`; diff --git a/tests/baselines/reference/taggedTemplateContextualTyping2.js b/tests/baselines/reference/taggedTemplateContextualTyping2.js index a24de8224f387..1eed600da4ccc 100644 --- a/tests/baselines/reference/taggedTemplateContextualTyping2.js +++ b/tests/baselines/reference/taggedTemplateContextualTyping2.js @@ -25,18 +25,18 @@ function tempTag2(...rest) { // Otherwise, the arrow functions' parameters will be typed as 'any', // and it is an error to invoke an any-typed value with type arguments, // so this test will error. -tempTag2 `${function (x) { +tempTag2 `${x => { x(undefined); return x; }}${0}`; -tempTag2 `${function (x) { +tempTag2 `${x => { x(undefined); return x; -}}${function (y) { +}}${y => { y(null); return y; }}${"hello"}`; -tempTag2 `${function (x) { +tempTag2 `${x => { x(undefined); return x; }}${undefined}${"hello"}`; diff --git a/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js index b88eadfd80ba8..da39703931c3c 100644 --- a/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js +++ b/tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.js @@ -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; diff --git a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js index 48af9403a7949..eb7086eadec90 100644 --- a/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js +++ b/tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.js @@ -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); }}`; diff --git a/tests/baselines/reference/templateStringInArrowFunctionES6.js b/tests/baselines/reference/templateStringInArrowFunctionES6.js index 633b96e9da410..11dd7a82fe397 100644 --- a/tests/baselines/reference/templateStringInArrowFunctionES6.js +++ b/tests/baselines/reference/templateStringInArrowFunctionES6.js @@ -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`; }; diff --git a/tests/baselines/reference/templateStringWithEmbeddedArrowFunctionES6.js b/tests/baselines/reference/templateStringWithEmbeddedArrowFunctionES6.js index f6f068aaa371c..3dd7e4c11e407 100644 --- a/tests/baselines/reference/templateStringWithEmbeddedArrowFunctionES6.js +++ b/tests/baselines/reference/templateStringWithEmbeddedArrowFunctionES6.js @@ -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`; diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunction.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunction.ts new file mode 100644 index 0000000000000..cace55c05fb24 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunction.ts @@ -0,0 +1,8 @@ +// @target:es5 +var f1 = () => { } +var f2 = (x: string, y: string) => { } +var f3 = (x: string, y: number, ...rest) => { } +var f4 = (x: string, y: number, z = 10) => { } +function foo(func: () => boolean) { } +foo(() => true); +foo(() => { return false; }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIs.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIs.ts new file mode 100644 index 0000000000000..1366fff90a15a --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIs.ts @@ -0,0 +1,5 @@ +// @target:ES5 +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; \ No newline at end of file diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIsES6.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIsES6.ts new file mode 100644 index 0000000000000..01331ededf995 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionAsIsES6.ts @@ -0,0 +1,5 @@ +// @target:ES6 +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; \ No newline at end of file diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionES6.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionES6.ts new file mode 100644 index 0000000000000..d56d07c6c4e60 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionES6.ts @@ -0,0 +1,8 @@ +// @target:es6 +var f1 = () => { } +var f2 = (x: string, y: string) => { } +var f3 = (x: string, y: number, ...rest) => { } +var f4 = (x: string, y: number, z=10) => { } +function foo(func: () => boolean) { } +foo(() => true); +foo(() => { return false; }); diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturing.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturing.ts new file mode 100644 index 0000000000000..e098874fc554c --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturing.ts @@ -0,0 +1,14 @@ +// @target:es5 +var f1 = () => { + this.age = 10 +}; + +var f2 = (x: string) => { + this.name = x +} + +function foo(func: () => boolean) { } +foo(() => { + this.age = 100; + return true; +}); diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturingES6.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturingES6.ts new file mode 100644 index 0000000000000..64d816dc2c627 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionThisCapturingES6.ts @@ -0,0 +1,14 @@ +// @target:es6 +var f1 = () => { + this.age = 10 +}; + +var f2 = (x: string) => { + this.name = x +} + +function foo(func: () => boolean){ } +foo(() => { + this.age = 100; + return true; +}); diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts new file mode 100644 index 0000000000000..fa92d48a4c0d8 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts @@ -0,0 +1,32 @@ +// @target: es6 +var a = () => { + var arg = arguments[0]; // error +} + +var b = function () { + var a = () => { + var arg = arguments[0]; // error + } +} + +function baz() { + () => { + var arg = arguments[0]; + } +} + +function foo(inputFunc: () => void) { } +foo(() => { + var arg = arguments[0]; // error +}); + +function bar() { + var arg = arguments[0]; // no error +} + + +() => { + function foo() { + var arg = arguments[0]; // no error + } +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts new file mode 100644 index 0000000000000..fa92d48a4c0d8 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArgumentsES6.ts @@ -0,0 +1,32 @@ +// @target: es6 +var a = () => { + var arg = arguments[0]; // error +} + +var b = function () { + var a = () => { + var arg = arguments[0]; // error + } +} + +function baz() { + () => { + var arg = arguments[0]; + } +} + +function foo(inputFunc: () => void) { } +foo(() => { + var arg = arguments[0]; // error +}); + +function bar() { + var arg = arguments[0]; // no error +} + + +() => { + function foo() { + var arg = arguments[0]; // no error + } +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIs.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIs.ts new file mode 100644 index 0000000000000..1366fff90a15a --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIs.ts @@ -0,0 +1,5 @@ +// @target:ES5 +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; \ No newline at end of file diff --git a/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIsES6.ts b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIsES6.ts new file mode 100644 index 0000000000000..01331ededf995 --- /dev/null +++ b/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionsAsIsES6.ts @@ -0,0 +1,5 @@ +// @target:ES6 +var arrow1 = a => { }; +var arrow2 = (a) => { }; + +var arrow3 = (a, b) => { }; \ No newline at end of file