Skip to content

Commit 2bc9ecb

Browse files
authored
Merge pull request #18296 from Microsoft/disable-lookahead-isStartOfParameter
Disable isStartOfType's lookahead when called from isStartOfParameter
2 parents 397ff84 + 90d9f3d commit 2bc9ecb

File tree

9 files changed

+66
-21
lines changed

9 files changed

+66
-21
lines changed

src/compiler/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ namespace ts {
12911291
args[i] = arguments[i];
12921292
}
12931293

1294-
return t => reduceLeft<(t: T) => T, T>(args, (u, f) => f(u), t);
1294+
return t => reduceLeft(args, (u, f) => f(u), t);
12951295
}
12961296
else if (d) {
12971297
return t => d(c(b(a(t))));

src/compiler/parser.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,8 @@ namespace ts {
22372237
return token() === SyntaxKind.DotDotDotToken ||
22382238
isIdentifierOrPattern() ||
22392239
isModifierKind(token()) ||
2240-
token() === SyntaxKind.AtToken || isStartOfType();
2240+
token() === SyntaxKind.AtToken ||
2241+
isStartOfType(/*inStartOfParameter*/ true);
22412242
}
22422243

22432244
function parseParameter(): ParameterDeclaration {
@@ -2698,7 +2699,7 @@ namespace ts {
26982699
}
26992700
}
27002701

2701-
function isStartOfType(): boolean {
2702+
function isStartOfType(inStartOfParameter?: boolean): boolean {
27022703
switch (token()) {
27032704
case SyntaxKind.AnyKeyword:
27042705
case SyntaxKind.StringKeyword:
@@ -2728,11 +2729,11 @@ namespace ts {
27282729
case SyntaxKind.DotDotDotToken:
27292730
return true;
27302731
case SyntaxKind.MinusToken:
2731-
return lookAhead(nextTokenIsNumericLiteral);
2732+
return !inStartOfParameter && lookAhead(nextTokenIsNumericLiteral);
27322733
case SyntaxKind.OpenParenToken:
27332734
// Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier,
27342735
// or something that starts a type. We don't want to consider things like '(1)' a type.
2735-
return lookAhead(isStartOfParenthesizedOrFunctionType);
2736+
return !inStartOfParameter && lookAhead(isStartOfParenthesizedOrFunctionType);
27362737
default:
27372738
return isIdentifier();
27382739
}

tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors2.errors.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,15): error TS1003: Identifier expected.
1+
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,12): error TS2304: Cannot find name 'a'.
2+
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,12): error TS2695: Left side of comma operator is unused and has no side effects.
23
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,16): error TS2304: Cannot find name 'b'.
34
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,16): error TS2695: Left side of comma operator is unused and has no side effects.
45
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,19): error TS2304: Cannot find name 'c'.
6+
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,23): error TS1005: ';' expected.
7+
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,26): error TS2304: Cannot find name 'a'.
58
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,28): error TS2304: Cannot find name 'b'.
69
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(1,30): error TS2304: Cannot find name 'c'.
710
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(2,12): error TS2695: Left side of comma operator is unused and has no side effects.
@@ -18,16 +21,22 @@ tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(4,17): error TS1005
1821
tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts(4,20): error TS2304: Cannot find name 'a'.
1922

2023

21-
==== tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts (18 errors) ====
24+
==== tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors2.ts (21 errors) ====
2225
var tt1 = (a, (b, c)) => a+b+c;
23-
~
24-
!!! error TS1003: Identifier expected.
26+
~
27+
!!! error TS2304: Cannot find name 'a'.
28+
~
29+
!!! error TS2695: Left side of comma operator is unused and has no side effects.
2530
~
2631
!!! error TS2304: Cannot find name 'b'.
2732
~
2833
!!! error TS2695: Left side of comma operator is unused and has no side effects.
2934
~
3035
!!! error TS2304: Cannot find name 'c'.
36+
~~
37+
!!! error TS1005: ';' expected.
38+
~
39+
!!! error TS2304: Cannot find name 'a'.
3140
~
3241
!!! error TS2304: Cannot find name 'b'.
3342
~

tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors2.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ var tt2 = ((a), b, c) => a+b+c;
55
var tt3 = ((a)) => a;
66

77
//// [fatarrowfunctionsOptionalArgsErrors2.js]
8-
var tt1 = function (a, ) {
9-
if ( === void 0) { = (b, c); }
10-
return a + b + c;
11-
};
8+
var tt1 = (a, (b, c));
9+
a + b + c;
1210
var tt2 = ((a), b, c);
1311
a + b + c;
1412
var tt3 = ((a));

tests/baselines/reference/parser512325.errors.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,14): error TS1003: Identifier expected.
1+
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,11): error TS2304: Cannot find name 'a'.
2+
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,11): error TS2695: Left side of comma operator is unused and has no side effects.
23
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,15): error TS2304: Cannot find name 'b'.
34
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,15): error TS2695: Left side of comma operator is unused and has no side effects.
45
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,18): error TS2304: Cannot find name 'c'.
6+
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,22): error TS1005: ';' expected.
7+
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,25): error TS2304: Cannot find name 'a'.
58
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,27): error TS2304: Cannot find name 'b'.
69
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts(1,29): error TS2304: Cannot find name 'c'.
710

811

9-
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts (6 errors) ====
12+
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512325.ts (9 errors) ====
1013
var tt = (a, (b, c)) => a+b+c;
11-
~
12-
!!! error TS1003: Identifier expected.
14+
~
15+
!!! error TS2304: Cannot find name 'a'.
16+
~
17+
!!! error TS2695: Left side of comma operator is unused and has no side effects.
1318
~
1419
!!! error TS2304: Cannot find name 'b'.
1520
~
1621
!!! error TS2695: Left side of comma operator is unused and has no side effects.
1722
~
1823
!!! error TS2304: Cannot find name 'c'.
24+
~~
25+
!!! error TS1005: ';' expected.
26+
~
27+
!!! error TS2304: Cannot find name 'a'.
1928
~
2029
!!! error TS2304: Cannot find name 'b'.
2130
~

tests/baselines/reference/parser512325.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
var tt = (a, (b, c)) => a+b+c;
33

44
//// [parser512325.js]
5-
var tt = function (a, ) {
6-
if ( === void 0) { = (b, c); }
7-
return a + b + c;
8-
};
5+
var tt = (a, (b, c));
6+
a + b + c;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression5.ts(1,2): error TS2304: Cannot find name 'bar'.
2+
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression5.ts(1,6): error TS2304: Cannot find name 'x'.
3+
4+
5+
==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression5.ts (2 errors) ====
6+
(bar(x,
7+
~~~
8+
!!! error TS2304: Cannot find name 'bar'.
9+
~
10+
!!! error TS2304: Cannot find name 'x'.
11+
() => {},
12+
() => {}
13+
)
14+
)
15+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [parserArrowFunctionExpression5.ts]
2+
(bar(x,
3+
() => {},
4+
() => {}
5+
)
6+
)
7+
8+
9+
//// [parserArrowFunctionExpression5.js]
10+
(bar(x, function () { }, function () { }));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(bar(x,
2+
() => {},
3+
() => {}
4+
)
5+
)

0 commit comments

Comments
 (0)