From 7c69dd84b9996d631cd0ad47bf97a496dec18b17 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 6 Sep 2017 13:11:35 -0700 Subject: [PATCH] Disable lookahead in isStartOfParameter/isStartOfType --- src/compiler/core.ts | 2 +- src/compiler/parser.ts | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 20f757c3df8..a9138e0ab5c 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1283,7 +1283,7 @@ namespace ts { args[i] = arguments[i]; } - return t => reduceLeft<(t: T) => T, T>(args, (u, f) => f(u), t); + return t => reduceLeft(args, (u, f) => f(u), t); } else if (d) { return t => d(c(b(a(t)))); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 71c7d3aac49..e4847517252 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2237,7 +2237,14 @@ namespace ts { return token() === SyntaxKind.DotDotDotToken || isIdentifierOrPattern() || isModifierKind(token()) || - token() === SyntaxKind.AtToken || isStartOfType(); + token() === SyntaxKind.AtToken || + // a jsdoc parameter can start directly with a type, but shouldn't look ahead + // in order to avoid confusion between parenthesized types and arrow functions + // eg + // declare function f(cb: function(number): void): void; + // vs + // f((n) => console.log(n)); + isStartOfType(/*disableLookahead*/ true); } function parseParameter(): ParameterDeclaration { @@ -2698,7 +2705,7 @@ namespace ts { } } - function isStartOfType(): boolean { + function isStartOfType(disableLookahead?: boolean): boolean { switch (token()) { case SyntaxKind.AnyKeyword: case SyntaxKind.StringKeyword: @@ -2728,11 +2735,11 @@ namespace ts { case SyntaxKind.DotDotDotToken: return true; case SyntaxKind.MinusToken: - return lookAhead(nextTokenIsNumericLiteral); + return !disableLookahead && lookAhead(nextTokenIsNumericLiteral); case SyntaxKind.OpenParenToken: // Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier, // or something that starts a type. We don't want to consider things like '(1)' a type. - return lookAhead(isStartOfParenthesizedOrFunctionType); + return !disableLookahead && lookAhead(isStartOfParenthesizedOrFunctionType); default: return isIdentifier(); }