From 10925c1e9ba8cd7d5babc76823c458fb0dbfc886 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Fri, 13 Mar 2015 01:30:07 -0400 Subject: [PATCH] Make sure arrow function grammar rules can deal with type annotations --- src/compiler/checker.ts | 8 +-- ...sallowLineTerminatorBeforeArrow.errors.txt | 58 ++++++++++++++-- .../disallowLineTerminatorBeforeArrow.js | 68 +++++++++++++++++++ .../disallowLineTerminatorBeforeArrow.ts | 32 +++++++++ 4 files changed, 155 insertions(+), 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index df1b70ab6ff..be1b6f943ca 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4412,7 +4412,7 @@ module ts { } /** - * Check if a Type was written as a tuple type literal. + * Check if a Type was written as a tuple type literal. * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. */ function isTupleType(type: Type) : boolean { @@ -11384,9 +11384,9 @@ module ts { if (node.kind === SyntaxKind.ArrowFunction) { var arrowFunction = node; var sourceFile = getSourceFileOfNode(node); - var equalsGreaterThanLine = getLineAndCharacterOfPosition(sourceFile, getTokenPosOfNode(arrowFunction.equalsGreaterThanToken, sourceFile)).line; - var parametersLine = getLineAndCharacterOfPosition(sourceFile, arrowFunction.parameters.end).line; - if (equalsGreaterThanLine !== parametersLine) { + var startLine = getLineAndCharacterOfPosition(sourceFile, arrowFunction.equalsGreaterThanToken.pos).line; + var endLine = getLineAndCharacterOfPosition(sourceFile, arrowFunction.equalsGreaterThanToken.end).line; + if (startLine !== endLine) { return grammarErrorOnNode(arrowFunction.equalsGreaterThanToken, Diagnostics.Line_terminator_not_permitted_before_arrow); } } diff --git a/tests/baselines/reference/disallowLineTerminatorBeforeArrow.errors.txt b/tests/baselines/reference/disallowLineTerminatorBeforeArrow.errors.txt index e441752338c..fbd9be772fa 100644 --- a/tests/baselines/reference/disallowLineTerminatorBeforeArrow.errors.txt +++ b/tests/baselines/reference/disallowLineTerminatorBeforeArrow.errors.txt @@ -6,15 +6,19 @@ tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(1 tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(12,7): error TS1200: Line terminator not permitted before arrow. tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(14,5): error TS1200: Line terminator not permitted before arrow. tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(16,7): error TS1200: Line terminator not permitted before arrow. -tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(20,5): error TS1200: Line terminator not permitted before arrow. -tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(22,5): error TS1200: Line terminator not permitted before arrow. -tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(27,13): error TS1200: Line terminator not permitted before arrow. -tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(31,13): error TS1200: Line terminator not permitted before arrow. -tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(36,13): error TS1200: Line terminator not permitted before arrow. -tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(40,9): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(18,5): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(21,5): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(23,8): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(26,8): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(52,5): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(54,5): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(59,13): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(63,13): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(68,13): error TS1200: Line terminator not permitted before arrow. +tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(72,9): error TS1200: Line terminator not permitted before arrow. -==== tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts (14 errors) ==== +==== tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts (18 errors) ==== var f1 = () => { } ~~ @@ -47,6 +51,46 @@ tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts(4 */ => { } ~~ !!! error TS1200: Line terminator not permitted before arrow. + var f9 = (a: number): number + => a; + ~~ +!!! error TS1200: Line terminator not permitted before arrow. + var f10 = (a: number) : + number + => a + ~~ +!!! error TS1200: Line terminator not permitted before arrow. + var f11 = (a: number): number /* + */ => a; + ~~ +!!! error TS1200: Line terminator not permitted before arrow. + var f12 = (a: number) : + number /* + */ => a + ~~ +!!! error TS1200: Line terminator not permitted before arrow. + + // Should be valid. + var f11 = (a: number + ) => a; + + // Should be valid. + var f12 = (a: number) + : number => a; + + // Should be valid. + var f13 = (a: number): + number => a; + + // Should be valid. + var f14 = () /* */ => {} + + // Should be valid. + var f15 = (a: number): number /* */ => a + + // Should be valid. + var f16 = (a: number, b = 10): + number /* */ => a + b; function foo(func: () => boolean) { } foo(() diff --git a/tests/baselines/reference/disallowLineTerminatorBeforeArrow.js b/tests/baselines/reference/disallowLineTerminatorBeforeArrow.js index 122c229d7d9..610cbe62c1b 100644 --- a/tests/baselines/reference/disallowLineTerminatorBeforeArrow.js +++ b/tests/baselines/reference/disallowLineTerminatorBeforeArrow.js @@ -15,6 +15,38 @@ var f7 = (x: string, y: number, z = 10) => { } var f8 = (x: string, y: number, z = 10) /* */ => { } +var f9 = (a: number): number + => a; +var f10 = (a: number) : + number + => a +var f11 = (a: number): number /* + */ => a; +var f12 = (a: number) : + number /* + */ => a + +// Should be valid. +var f11 = (a: number + ) => a; + +// Should be valid. +var f12 = (a: number) + : number => a; + +// Should be valid. +var f13 = (a: number): + number => a; + +// Should be valid. +var f14 = () /* */ => {} + +// Should be valid. +var f15 = (a: number): number /* */ => a + +// Should be valid. +var f16 = (a: number, b = 10): + number /* */ => a + b; function foo(func: () => boolean) { } foo(() @@ -77,6 +109,42 @@ var f7 = function (x, y, z) { var f8 = function (x, y, z) { if (z === void 0) { z = 10; } }; +var f9 = function (a) { + return a; +}; +var f10 = function (a) { + return a; +}; +var f11 = function (a) { + return a; +}; +var f12 = function (a) { + return a; +}; +// Should be valid. +var f11 = function (a) { + return a; +}; +// Should be valid. +var f12 = function (a) { + return a; +}; +// Should be valid. +var f13 = function (a) { + return a; +}; +// Should be valid. +var f14 = function () { +}; +// Should be valid. +var f15 = function (a) { + return a; +}; +// Should be valid. +var f16 = function (a, b) { + if (b === void 0) { b = 10; } + return a + b; +}; function foo(func) { } foo(function () { diff --git a/tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts b/tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts index f11fed6b478..bd984ba4da0 100644 --- a/tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts +++ b/tests/cases/conformance/es6/arrowFunction/disallowLineTerminatorBeforeArrow.ts @@ -14,6 +14,38 @@ var f7 = (x: string, y: number, z = 10) => { } var f8 = (x: string, y: number, z = 10) /* */ => { } +var f9 = (a: number): number + => a; +var f10 = (a: number) : + number + => a +var f11 = (a: number): number /* + */ => a; +var f12 = (a: number) : + number /* + */ => a + +// Should be valid. +var f11 = (a: number + ) => a; + +// Should be valid. +var f12 = (a: number) + : number => a; + +// Should be valid. +var f13 = (a: number): + number => a; + +// Should be valid. +var f14 = () /* */ => {} + +// Should be valid. +var f15 = (a: number): number /* */ => a + +// Should be valid. +var f16 = (a: number, b = 10): + number /* */ => a + b; function foo(func: () => boolean) { } foo(()