diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 85eb373c449..cdcf9345be9 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1445,7 +1445,7 @@ module ts { function tryParseParenthesizedArrowFunctionExpression(): Expression { var pos = getNodePos(); - // Whether we are certain that we should parse an arrow expression. + // Indicates whether we are certain that we should parse an arrow expression. var triState = isParenthesizedArrowFunctionExpression(); // It is not a parenthesized arrow function. @@ -1470,11 +1470,12 @@ module ts { // Otherwise, *maybe* we had an arrow function and we need to *try* to parse it out // (which will ensure we rollback if we fail). - var sig = tryParse(parseSignatureAndArrow); + var sig = tryParse(parseSignatureIfArrowOrBraceFollows); if (sig === undefined) { return undefined; } else { + parseExpected(SyntaxKind.EqualsGreaterThanToken); return parseArrowExpressionTail(pos, sig, /*noIn:*/ false); } } @@ -1512,8 +1513,9 @@ module ts { return Tristate.True; } - // We had "(" not followed by an identifier. This definitely doesn't - // look like a lambda. Note: we could be a little more lenient and allow + // If we had "(" followed by something that's not an identifier, + // then this definitely doesn't look like a lambda. + // Note: we could be a little more lenient and allow // "(public" or "(private". These would not ever actually be allowed, // but we could provide a good error message instead of bailing out. if (!isIdentifier()) { @@ -1543,10 +1545,12 @@ module ts { return Tristate.False; } - function parseSignatureAndArrow(): ParsedSignature { + function parseSignatureIfArrowOrBraceFollows(): ParsedSignature { var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken); - parseExpected(SyntaxKind.EqualsGreaterThanToken); - return sig; + if (token === SyntaxKind.EqualsGreaterThanToken || token === SyntaxKind.OpenBraceToken) { + return sig; + } + return undefined; } function parseArrowExpressionTail(pos: number, sig: ParsedSignature, noIn: boolean): FunctionExpression { diff --git a/tests/baselines/reference/arrowFunctionsMissingTokens.errors.txt b/tests/baselines/reference/arrowFunctionsMissingTokens.errors.txt index b9d139dba25..61a3bb002e7 100644 --- a/tests/baselines/reference/arrowFunctionsMissingTokens.errors.txt +++ b/tests/baselines/reference/arrowFunctionsMissingTokens.errors.txt @@ -1,4 +1,4 @@ -==== tests/cases/compiler/arrowFunctionsMissingTokens.ts (39 errors) ==== +==== tests/cases/compiler/arrowFunctionsMissingTokens.ts (31 errors) ==== module missingArrowsWithCurly { var a = () { }; @@ -11,31 +11,15 @@ var c = (x) { }; ~ -!!! ',' expected. - ~ -!!! Cannot find name 'x'. +!!! '=>' expected. var d = (x: number, y: string) { }; - ~ -!!! ')' expected. - ~ -!!! ',' expected. ~ -!!! Variable declaration expected. - ~ -!!! Cannot find name 'x'. +!!! '=>' expected. var e = (x: number, y: string): void { }; - ~ -!!! ')' expected. - ~ -!!! ',' expected. - ~ -!!! Variable declaration expected. - ~~~~ -!!! Variable declaration expected. - ~ -!!! Cannot find name 'x'. + ~ +!!! '=>' expected. } module missingCurliesWithArrow { diff --git a/tests/baselines/reference/fatarrowfunctionsErrors.errors.txt b/tests/baselines/reference/fatarrowfunctionsErrors.errors.txt index d04b93b10ad..132e369a643 100644 --- a/tests/baselines/reference/fatarrowfunctionsErrors.errors.txt +++ b/tests/baselines/reference/fatarrowfunctionsErrors.errors.txt @@ -1,4 +1,4 @@ -==== tests/cases/compiler/fatarrowfunctionsErrors.ts (25 errors) ==== +==== tests/cases/compiler/fatarrowfunctionsErrors.ts (18 errors) ==== foo((...Far:any[])=>{return 0;}) ~~~ !!! Cannot find name 'foo'. @@ -39,25 +39,11 @@ ~ !!! '=>' expected. var x2 = (a:number) :void {}; - ~ -!!! ')' expected. - ~ -!!! ',' expected. - ~ -!!! Variable declaration expected. - ~~~~ -!!! Variable declaration expected. - ~ -!!! Cannot find name 'a'. + ~ +!!! '=>' expected. var x3 = (a:number) {}; - ~ -!!! ')' expected. - ~ -!!! ',' expected. ~ -!!! Variable declaration expected. - ~ -!!! Cannot find name 'a'. +!!! '=>' expected. var x4= (...a: any[]) { }; ~ !!! '=>' expected. \ No newline at end of file diff --git a/tests/baselines/reference/invalidTryStatements2.errors.txt b/tests/baselines/reference/invalidTryStatements2.errors.txt index 3a9aa15b540..fcb8ad3560d 100644 --- a/tests/baselines/reference/invalidTryStatements2.errors.txt +++ b/tests/baselines/reference/invalidTryStatements2.errors.txt @@ -1,4 +1,4 @@ -==== tests/cases/conformance/statements/tryStatements/invalidTryStatements2.ts (5 errors) ==== +==== tests/cases/conformance/statements/tryStatements/invalidTryStatements2.ts (4 errors) ==== function fn() { try { } catch { // syntax error, missing '(x)' @@ -10,9 +10,7 @@ ~~~~~ !!! Statement expected. ~ -!!! ';' expected. - ~ -!!! Cannot find name 'x'. +!!! '=>' expected. finally{ } // error missing try ~~~~~~~