Skip to content

Commit 8615eec

Browse files
Enhancement new expression with type arguments without parenthesized argument list error message (microsoft#37576)
* Enhancement new expression with type arguments without parenthesized argument list error message Signed-off-by: Rustin-Liu <[email protected]> * add baselines Signed-off-by: Rustin-Liu <[email protected]> * use parseErrorAt Signed-off-by: Rustin-Liu <[email protected]> * refine code Signed-off-by: Rustin-Liu <[email protected]> * Space before paren Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent b7b2c33 commit 8615eec

12 files changed

+30
-23
lines changed

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,10 @@
11471147
"category": "Error",
11481148
"code": 1383
11491149
},
1150+
"A 'new' expression with type arguments must always be followed by a parenthesized argument list.": {
1151+
"category": "Error",
1152+
"code": 1384
1153+
},
11501154

11511155
"The types of '{0}' are incompatible between these types.": {
11521156
"category": "Error",

src/compiler/parser.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5199,9 +5199,12 @@ namespace ts {
51995199
const node = <NewExpression>createNode(SyntaxKind.NewExpression, fullStart);
52005200
node.expression = expression;
52015201
node.typeArguments = typeArguments;
5202-
if (node.typeArguments || token() === SyntaxKind.OpenParenToken) {
5202+
if (token() === SyntaxKind.OpenParenToken) {
52035203
node.arguments = parseArgumentList();
52045204
}
5205+
else if (node.typeArguments) {
5206+
parseErrorAt(fullStart, scanner.getStartPos(), Diagnostics.A_new_expression_with_type_arguments_must_always_be_followed_by_a_parenthesized_argument_list);
5207+
}
52055208
return finishNode(node);
52065209
}
52075210

tests/baselines/reference/genericCallsWithoutParens.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/genericCallsWithoutParens.ts(2,18): error TS1005: '(' expected.
2-
tests/cases/compiler/genericCallsWithoutParens.ts(7,22): error TS1005: '(' expected.
2+
tests/cases/compiler/genericCallsWithoutParens.ts(7,8): error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
33

44

55
==== tests/cases/compiler/genericCallsWithoutParens.ts (2 errors) ====
@@ -12,7 +12,7 @@ tests/cases/compiler/genericCallsWithoutParens.ts(7,22): error TS1005: '(' expec
1212
foo: T;
1313
}
1414
var c = new C<number>; // parse error
15-
~
16-
!!! error TS1005: '(' expected.
15+
~~~~~~~~~~~~~~
16+
!!! error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
1717

1818

tests/baselines/reference/genericCallsWithoutParens.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ var C = /** @class */ (function () {
1717
}
1818
return C;
1919
}());
20-
var c = new C(); // parse error
20+
var c = new C; // parse error

tests/baselines/reference/genericConstructExpressionWithoutArgs.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/genericConstructExpressionWithoutArgs.ts(10,1): error TS1005: '(' expected.
1+
tests/cases/compiler/genericConstructExpressionWithoutArgs.ts(9,9): error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
22

33

44
==== tests/cases/compiler/genericConstructExpressionWithoutArgs.ts (1 errors) ====
@@ -11,6 +11,6 @@ tests/cases/compiler/genericConstructExpressionWithoutArgs.ts(10,1): error TS100
1111

1212
var c = new C // C<any>
1313
var c2 = new C<number> // error, type params are actually part of the arg list so you need both
14-
15-
16-
!!! error TS1005: '(' expected.
14+
~~~~~~~~~~~~~~
15+
!!! error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
16+

tests/baselines/reference/genericConstructExpressionWithoutArgs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ var C = /** @class */ (function () {
2323
return C;
2424
}());
2525
var c = new C; // C<any>
26-
var c2 = new C(); // error, type params are actually part of the arg list so you need both
26+
var c2 = new C; // error, type params are actually part of the arg list so you need both
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/genericObjectCreationWithoutTypeArgs.ts(6,26): error TS1005: '(' expected.
1+
tests/cases/compiler/genericObjectCreationWithoutTypeArgs.ts(6,9): error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
22

33

44
==== tests/cases/compiler/genericObjectCreationWithoutTypeArgs.ts (1 errors) ====
@@ -8,8 +8,8 @@ tests/cases/compiler/genericObjectCreationWithoutTypeArgs.ts(6,26): error TS1005
88

99
var x1 = new SS<number>(); // OK
1010
var x2 = new SS < number>; // Correctly give error
11-
~
12-
!!! error TS1005: '(' expected.
11+
~~~~~~~~~~~~~~~~~
12+
!!! error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
1313
var x3 = new SS(); // OK
1414
var x4 = new SS; // Should be allowed, but currently give error ('supplied parameters do not match any signature of the call target')
1515

tests/baselines/reference/genericObjectCreationWithoutTypeArgs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ var SS = /** @class */ (function () {
1616
return SS;
1717
}());
1818
var x1 = new SS(); // OK
19-
var x2 = new SS(); // Correctly give error
19+
var x2 = new SS; // Correctly give error
2020
var x3 = new SS(); // OK
2121
var x4 = new SS; // Should be allowed, but currently give error ('supplied parameters do not match any signature of the call target')

tests/baselines/reference/newOperatorErrorCases.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases.ts(26,16): error TS1005: ',' expected.
22
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases.ts(26,16): error TS2695: Left side of comma operator is unused and has no side effects.
3-
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases.ts(31,23): error TS1005: '(' expected.
3+
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases.ts(31,9): error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
44
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases.ts(36,9): error TS2350: Only a void function can be called with the 'new' keyword.
55

66

@@ -40,8 +40,8 @@ tests/cases/conformance/expressions/newOperator/newOperatorErrorCases.ts(36,9):
4040
var c1 = new T;
4141
var c1: T<{}>;
4242
var c2 = new T<string>; // Parse error
43-
~
44-
!!! error TS1005: '(' expected.
43+
~~~~~~~~~~~~~~
44+
!!! error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
4545

4646

4747
// Construct expression of non-void returning function

tests/baselines/reference/newOperatorErrorCases.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var b = new C0;
6262
// Generic construct expression with no parentheses
6363
var c1 = new T;
6464
var c1;
65-
var c2 = new T(); // Parse error
65+
var c2 = new T; // Parse error
6666
// Construct expression of non-void returning function
6767
function fnNumber() { return 32; }
6868
var s = new fnNumber(); // Error
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
tests/cases/conformance/parser/ecmascript5/Generics/parserConstructorAmbiguity3.ts(1,1): error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
12
tests/cases/conformance/parser/ecmascript5/Generics/parserConstructorAmbiguity3.ts(1,10): error TS2304: Cannot find name 'A'.
23
tests/cases/conformance/parser/ecmascript5/Generics/parserConstructorAmbiguity3.ts(1,10): error TS2558: Expected 0 type arguments, but got 1.
3-
tests/cases/conformance/parser/ecmascript5/Generics/parserConstructorAmbiguity3.ts(1,12): error TS1005: '(' expected.
44

55

66
==== tests/cases/conformance/parser/ecmascript5/Generics/parserConstructorAmbiguity3.ts (3 errors) ====
77
new Date<A>
8+
~~~~~~~~~~~
9+
!!! error TS1384: A 'new' expression with type arguments must always be followed by a parenthesized argument list.
810
~
911
!!! error TS2304: Cannot find name 'A'.
1012
~
11-
!!! error TS2558: Expected 0 type arguments, but got 1.
12-
13-
!!! error TS1005: '(' expected.
13+
!!! error TS2558: Expected 0 type arguments, but got 1.

tests/baselines/reference/parserConstructorAmbiguity3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
new Date<A>
33

44
//// [parserConstructorAmbiguity3.js]
5-
new Date();
5+
new Date;

0 commit comments

Comments
 (0)