Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create diagnostics, move to checker grammar check
  • Loading branch information
jakebailey committed Jun 22, 2022
commit 728c9b4c61a26762f9e5edd730bc7c497ed772ae
7 changes: 7 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28912,7 +28912,14 @@ namespace ts {
return nonNullType;
}

function checkGrammarPropertyAccessExpression(node: PropertyAccessExpression) {
if (node.expression.kind === SyntaxKind.ExpressionWithTypeArguments) {
grammarErrorOnNode(node.name, Diagnostics.Instantiation_expression_cannot_be_followed_by_property_access);
}
}

function checkPropertyAccessExpression(node: PropertyAccessExpression, checkMode: CheckMode | undefined) {
checkGrammarPropertyAccessExpression(node);
return node.flags & NodeFlags.OptionalChain ? checkPropertyAccessChain(node as PropertyAccessChain, checkMode) :
checkPropertyAccessExpressionOrQualifiedName(node, node.expression, checkNonNullExpression(node.expression), node.name, checkMode);
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,10 @@
"category": "Message",
"code": 1476
},
"Instantiation expression cannot be followed by property access.": {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this instead complain on the type arguments and say they should be removed? And quickfix?

"category": "Error",
"code": 1477
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5683,10 +5683,6 @@ namespace ts {
}

if (isPropertyAccess) {
// TODO: issue a better error?
if (expression.kind === SyntaxKind.ExpressionWithTypeArguments) {
return expression as MemberExpression;
}
expression = parsePropertyAccessExpressionRest(pos, expression, questionDotToken);
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/genericCallWithoutArgs.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/genericCallWithoutArgs.ts(4,18): error TS1003: Identifier expected.


==== tests/cases/compiler/genericCallWithoutArgs.ts (1 errors) ====
function f<X, Y>(x: X, y: Y) {
}

f<number,string>.

!!! error TS1003: Identifier expected.
2 changes: 1 addition & 1 deletion tests/baselines/reference/genericCallWithoutArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ f<number,string>.
//// [genericCallWithoutArgs.js]
function f(x, y) {
}
f;
f.;
2 changes: 2 additions & 0 deletions tests/baselines/reference/genericCallWithoutArgs.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ function f<X, Y>(x: X, y: Y) {
}

f<number,string>.
>f<number,string>. : any
>f<number,string> : (x: number, y: string) => void
>f : <X, Y>(x: X, y: Y) => void
> : any

Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(7,22): error TS1005: ',' expected.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(7,22): error TS2451: Cannot redeclare block-scoped variable 'g'.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(7,22): error TS7005: Variable 'g' implicitly has an 'any' type.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(8,22): error TS1005: ',' expected.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(8,22): error TS2451: Cannot redeclare block-scoped variable 'g'.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(8,22): error TS7005: Variable 'g' implicitly has an 'any' type.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(8,23): error TS1005: ',' expected.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(8,31): error TS1109: Expression expected.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(13,12): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'string[]'.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(13,14): error TS2693: 'number' only refers to a type, but is being used as a value here.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(18,12): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'number'.
Expand All @@ -14,10 +6,6 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpr
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(19,24): error TS2635: Type '{ (): number; g<U>(): U; }' has no signatures for which the type argument list is applicable.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(23,23): error TS1005: '(' expected.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(26,24): error TS2558: Expected 0 type arguments, but got 1.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(30,13): error TS2451: Cannot redeclare block-scoped variable 'g'.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(32,14): error TS2635: Type 'any' has no signatures for which the type argument list is applicable.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(33,14): error TS2635: Type 'any' has no signatures for which the type argument list is applicable.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(34,14): error TS2635: Type 'any' has no signatures for which the type argument list is applicable.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(39,2): error TS2554: Expected 0 arguments, but got 1.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(43,12): error TS2365: Operator '<' cannot be applied to types '{ <T>(): T; g<U>(): U; }' and 'boolean'.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(44,12): error TS2365: Operator '<' cannot be applied to types '{ <T>(): T; g<U>(): U; }' and 'boolean'.
Expand All @@ -26,31 +14,15 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpr
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(45,12): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'number'.


==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts (26 errors) ====
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts (14 errors) ====
declare let f: { <T>(): T, g<U>(): U };

// Type arguments in member expressions

const a1 = f<number>; // { (): number; g<U>(): U; }
const a2 = f.g<number>; // () => number
const a3 = f<number>.g; // <U>() => U
~
!!! error TS1005: ',' expected.
~
!!! error TS2451: Cannot redeclare block-scoped variable 'g'.
~
!!! error TS7005: Variable 'g' implicitly has an 'any' type.
const a4 = f<number>.g<number>; // () => number
~
!!! error TS1005: ',' expected.
~
!!! error TS2451: Cannot redeclare block-scoped variable 'g'.
~
!!! error TS7005: Variable 'g' implicitly has an 'any' type.
~
!!! error TS1005: ',' expected.
~
!!! error TS1109: Expression expected.
const a5 = f['g']<number>; // () => number

// `[` is an expression starter and cannot immediately follow a type argument list
Expand Down Expand Up @@ -89,18 +61,10 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpr
// Instantiation expression and binary operators

declare let g: (<T>(x: T) => T) | undefined;
~
!!! error TS2451: Cannot redeclare block-scoped variable 'g'.

const c1 = g<string> || ((x: string) => x);
~~~~~~
!!! error TS2635: Type 'any' has no signatures for which the type argument list is applicable.
const c2 = g<string> ?? ((x: string) => x);
~~~~~~
!!! error TS2635: Type 'any' has no signatures for which the type argument list is applicable.
const c3 = g<string> && ((x: string) => x);
~~~~~~
!!! error TS2635: Type 'any' has no signatures for which the type argument list is applicable.

// Parsed as function call, even though this differs from JavaScript

Expand Down
21 changes: 7 additions & 14 deletions tests/baselines/reference/instantiationExpressionErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ var _a, _b, _c;
// Type arguments in member expressions
var a1 = (f); // { (): number; g<U>(): U; }
var a2 = (f.g); // () => number
var a3 = (f), g; // <U>() => U
var a4 = (f), g;
; // () => number
var a3 = f.g; // <U>() => U
var a4 = (f.g); // () => number
var a5 = (f['g']); // () => number
// `[` is an expression starter and cannot immediately follow a type argument list
var a6 = f < number > ['g']; // Error
Expand Down Expand Up @@ -200,14 +199,8 @@ declare const a1: {
g<U>(): U;
};
declare const a2: () => number;
declare const a3: {
(): number;
g<U>(): U;
}, g: any;
declare const a4: {
(): number;
g<U>(): U;
}, g: any;
declare const a3: <U>() => U;
declare const a4: () => number;
declare const a5: () => number;
declare const a6: boolean;
declare const a7: <U>() => U;
Expand All @@ -220,9 +213,9 @@ declare const b2: number;
declare const b3: number;
declare const b4: number;
declare let g: (<T>(x: T) => T) | undefined;
declare const c1: any;
declare const c2: any;
declare const c3: any;
declare const c1: (x: string) => string;
declare const c2: (x: string) => string;
declare const c3: ((x: string) => string) | undefined;
declare const x1: true;
declare const r1: boolean;
declare const r2: boolean;
Expand Down
12 changes: 7 additions & 5 deletions tests/baselines/reference/instantiationExpressionErrors.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const a2 = f.g<number>; // () => number

const a3 = f<number>.g; // <U>() => U
>a3 : Symbol(a3, Decl(instantiationExpressionErrors.ts, 6, 5))
>f<number>.g : Symbol(g, Decl(instantiationExpressionErrors.ts, 0, 26))
>f : Symbol(f, Decl(instantiationExpressionErrors.ts, 0, 11))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 6, 21))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 0, 26))

const a4 = f<number>.g<number>; // () => number
>a4 : Symbol(a4, Decl(instantiationExpressionErrors.ts, 7, 5))
>f<number>.g : Symbol(g, Decl(instantiationExpressionErrors.ts, 0, 26))
>f : Symbol(f, Decl(instantiationExpressionErrors.ts, 0, 11))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 7, 21))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 0, 26))

const a5 = f['g']<number>; // () => number
>a5 : Symbol(a5, Decl(instantiationExpressionErrors.ts, 8, 5))
Expand Down Expand Up @@ -84,19 +86,19 @@ declare let g: (<T>(x: T) => T) | undefined;

const c1 = g<string> || ((x: string) => x);
>c1 : Symbol(c1, Decl(instantiationExpressionErrors.ts, 31, 5))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 6, 21))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 29, 11))
>x : Symbol(x, Decl(instantiationExpressionErrors.ts, 31, 26))
>x : Symbol(x, Decl(instantiationExpressionErrors.ts, 31, 26))

const c2 = g<string> ?? ((x: string) => x);
>c2 : Symbol(c2, Decl(instantiationExpressionErrors.ts, 32, 5))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 6, 21))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 29, 11))
>x : Symbol(x, Decl(instantiationExpressionErrors.ts, 32, 26))
>x : Symbol(x, Decl(instantiationExpressionErrors.ts, 32, 26))

const c3 = g<string> && ((x: string) => x);
>c3 : Symbol(c3, Decl(instantiationExpressionErrors.ts, 33, 5))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 6, 21))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 29, 11))
>x : Symbol(x, Decl(instantiationExpressionErrors.ts, 33, 26))
>x : Symbol(x, Decl(instantiationExpressionErrors.ts, 33, 26))

Expand Down
37 changes: 19 additions & 18 deletions tests/baselines/reference/instantiationExpressionErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ const a2 = f.g<number>; // () => number
>g : <U>() => U

const a3 = f<number>.g; // <U>() => U
>a3 : { (): number; g<U>(): U; }
>a3 : <U>() => U
>f<number>.g : <U>() => U
>f<number> : { (): number; g<U>(): U; }
>f : { <T>(): T; g<U>(): U; }
>g : any
>g : <U>() => U

const a4 = f<number>.g<number>; // () => number
>a4 : { (): number; g<U>(): U; }
>a4 : () => number
>f<number>.g<number> : () => number
>f<number>.g : <U>() => U
>f<number> : { (): number; g<U>(): U; }
>f : { <T>(): T; g<U>(): U; }
>g : any
><number> : number
> : any
>g : <U>() => U

const a5 = f['g']<number>; // () => number
>a5 : () => number
Expand Down Expand Up @@ -106,30 +107,30 @@ declare let g: (<T>(x: T) => T) | undefined;
>x : T

const c1 = g<string> || ((x: string) => x);
>c1 : any
>g<string> || ((x: string) => x) : any
>g<string> : any
>g : any
>c1 : (x: string) => string
>g<string> || ((x: string) => x) : (x: string) => string
>g<string> : ((x: string) => string) | undefined
>g : (<T>(x: T) => T) | undefined
>((x: string) => x) : (x: string) => string
>(x: string) => x : (x: string) => string
>x : string
>x : string

const c2 = g<string> ?? ((x: string) => x);
>c2 : any
>g<string> ?? ((x: string) => x) : any
>g<string> : any
>g : any
>c2 : (x: string) => string
>g<string> ?? ((x: string) => x) : (x: string) => string
>g<string> : ((x: string) => string) | undefined
>g : (<T>(x: T) => T) | undefined
>((x: string) => x) : (x: string) => string
>(x: string) => x : (x: string) => string
>x : string
>x : string

const c3 = g<string> && ((x: string) => x);
>c3 : any
>g<string> && ((x: string) => x) : any
>g<string> : any
>g : any
>c3 : ((x: string) => string) | undefined
>g<string> && ((x: string) => x) : ((x: string) => string) | undefined
>g<string> : ((x: string) => string) | undefined
>g : (<T>(x: T) => T) | undefined
>((x: string) => x) : (x: string) => string
>(x: string) => x : (x: string) => string
>x : string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error TS1005: ';' expected.
tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error TS2304: Cannot find name 'd'.
tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error TS1477: Instantiation expression cannot be followed by property access.


==== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts (2 errors) ====
==== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts (1 errors) ====
declare namespace A {
export class b<T> {
static d: number;
Expand All @@ -16,9 +15,7 @@ tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error T

a?.b<c>.d;
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'd'.
!!! error TS1477: Instantiation expression cannot be followed by property access.

a?.b.d

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ a?.b.d


//// [optionalChainWithInstantiationExpression1.js]
(a === null || a === void 0 ? void 0 : a.b);
d;
(a === null || a === void 0 ? void 0 : a.b).d;
a === null || a === void 0 ? void 0 : a.b.d;
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ declare const a: typeof A | undefined;
>A : Symbol(A, Decl(optionalChainWithInstantiationExpression1.ts, 0, 0))

a?.b<c>.d;
>a?.b<c>.d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))
>a?.b : Symbol(A.b, Decl(optionalChainWithInstantiationExpression1.ts, 0, 21))
>a : Symbol(a, Decl(optionalChainWithInstantiationExpression1.ts, 9, 13))
>b : Symbol(A.b, Decl(optionalChainWithInstantiationExpression1.ts, 0, 21))
>c : Symbol(c, Decl(optionalChainWithInstantiationExpression1.ts, 5, 1))
>d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))

a?.b.d
>a?.b.d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ declare const a: typeof A | undefined;
>A : typeof A

a?.b<c>.d;
>a?.b<c>.d : number
>a?.b<c> : { new (x: unknown): A.b<unknown>; prototype: A.b<any>; d: number; }
>a?.b : typeof A.b
>a : typeof A
>b : typeof A.b
>d : any
>d : number

a?.b.d
>a?.b.d : number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error TS1005: ';' expected.
tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error TS2304: Cannot find name 'd'.
tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error TS1477: Instantiation expression cannot be followed by property access.


==== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts (2 errors) ====
==== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts (1 errors) ====
declare namespace A {
export class b<T> {
static d: number;
Expand All @@ -16,9 +15,7 @@ tests/cases/compiler/optionalChainWithInstantiationExpression1.ts(12,9): error T

a?.b<c>.d;
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'd'.
!!! error TS1477: Instantiation expression cannot be followed by property access.

a?.b.d

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ a?.b.d


//// [optionalChainWithInstantiationExpression1.js]
a?.b;
d;
a?.b.d;
a?.b.d;
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ declare const a: typeof A | undefined;
>A : Symbol(A, Decl(optionalChainWithInstantiationExpression1.ts, 0, 0))

a?.b<c>.d;
>a?.b<c>.d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))
>a?.b : Symbol(A.b, Decl(optionalChainWithInstantiationExpression1.ts, 0, 21))
>a : Symbol(a, Decl(optionalChainWithInstantiationExpression1.ts, 9, 13))
>b : Symbol(A.b, Decl(optionalChainWithInstantiationExpression1.ts, 0, 21))
>c : Symbol(c, Decl(optionalChainWithInstantiationExpression1.ts, 5, 1))
>d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))

a?.b.d
>a?.b.d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))
Expand Down
Loading