From b76c13cfd5724510158857d88c5607ccf1cbf080 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 24 Jul 2014 18:05:05 -0700 Subject: [PATCH] Addressed code review feedback. --- src/compiler/parser.ts | 40 ++++++++++--------- .../arrowFunctionsMissingTokens.errors.txt | 2 +- .../compiler/arrowFunctionsMissingTokens.ts | 2 +- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index df45db2a415..ba1bb0ed700 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1468,14 +1468,14 @@ module ts { // *Maybe* we had an arrow function and we need to try to parse it out, // rolling back and trying other parses if we fail. - var sig = tryParse(parseSignatureIfArrowOrBraceFollows); - if (sig === undefined) { - return undefined; - } - else { + var sig = tryParseSignatureIfArrowOrBraceFollows(); + if (sig) { parseExpected(SyntaxKind.EqualsGreaterThanToken); return parseArrowExpressionTail(pos, sig, /*noIn:*/ false); } + else { + return undefined; + } } // True -> We definitely expect a parenthesized arrow function here. @@ -1549,22 +1549,24 @@ module ts { return Tristate.False; } - function parseSignatureIfArrowOrBraceFollows(): ParsedSignature { - var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken); + function tryParseSignatureIfArrowOrBraceFollows(): ParsedSignature { + return tryParse(() => { + var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken); - // Parsing a signature isn't enough. - // Parenthesized arrow signatures often look like other valid expressions. - // For instance: - // - "(x = 10)" is an assignment expression parsed as a signature with a default parameter value. - // - "(x,y)" is a comma expression parsed as a signature with two parameters. - // - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation. - // - // So we need just a bit of lookahead to ensure that it can only be a signature. - if (token === SyntaxKind.EqualsGreaterThanToken || token === SyntaxKind.OpenBraceToken) { - return sig; - } + // Parsing a signature isn't enough. + // Parenthesized arrow signatures often look like other valid expressions. + // For instance: + // - "(x = 10)" is an assignment expression parsed as a signature with a default parameter value. + // - "(x,y)" is a comma expression parsed as a signature with two parameters. + // - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation. + // + // So we need just a bit of lookahead to ensure that it can only be a signature. + if (token === SyntaxKind.EqualsGreaterThanToken || token === SyntaxKind.OpenBraceToken) { + return sig; + } - return undefined; + 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 f6072db0839..69b94cd7d46 100644 --- a/tests/baselines/reference/arrowFunctionsMissingTokens.errors.txt +++ b/tests/baselines/reference/arrowFunctionsMissingTokens.errors.txt @@ -80,7 +80,7 @@ ~ !!! Declaration or statement expected. - module ceci_nEst_pas_une_arrow_function { + module ce_nEst_pas_une_arrow_function { var a = (); ~ !!! Expression expected. diff --git a/tests/cases/compiler/arrowFunctionsMissingTokens.ts b/tests/cases/compiler/arrowFunctionsMissingTokens.ts index 0e1e6931807..bd3e25d56b4 100644 --- a/tests/cases/compiler/arrowFunctionsMissingTokens.ts +++ b/tests/cases/compiler/arrowFunctionsMissingTokens.ts @@ -41,7 +41,7 @@ module missingCurliesWithArrow { } } -module ceci_nEst_pas_une_arrow_function { +module ce_nEst_pas_une_arrow_function { var a = (); var b = (): void;