Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow expression with type parameters as left side of property access #49464

Merged
merged 8 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 4 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5605,6 +5605,10 @@ namespace ts {
}

if (isPropertyAccess) {
// TODO: issue a better error?
if (expression.kind === SyntaxKind.ExpressionWithTypeArguments) {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
return expression as MemberExpression;
}
expression = parsePropertyAccessExpressionRest(pos, expression, questionDotToken);
continue;
}
Expand Down
10 changes: 0 additions & 10 deletions tests/baselines/reference/genericCallWithoutArgs.errors.txt

This file was deleted.

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: 0 additions & 2 deletions tests/baselines/reference/genericCallWithoutArgs.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ 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,3 +1,11 @@
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 @@ -10,15 +18,31 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpr
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts(35,12): error TS2365: Operator '<' cannot be applied to types '{ <T>(): T; g<U>(): U; }' and 'boolean'.


==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts (10 errors) ====
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts (18 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
15 changes: 11 additions & 4 deletions tests/baselines/reference/instantiationExpressionErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ var _a, _b;
// 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 @@ -90,8 +91,14 @@ declare const a1: {
g<U>(): U;
};
declare const a2: () => number;
declare const a3: <U>() => U;
declare const a4: () => number;
declare const a3: {
(): number;
g<U>(): U;
}, g: any;
declare const a4: {
(): number;
g<U>(): U;
}, g: any;
declare const a5: () => number;
declare const a6: boolean;
declare const a7: <U>() => U;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ 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, 0, 26))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 6, 21))

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, 0, 26))
>g : Symbol(g, Decl(instantiationExpressionErrors.ts, 7, 21))

const a5 = f['g']<number>; // () => number
>a5 : Symbol(a5, Decl(instantiationExpressionErrors.ts, 8, 5))
Expand Down
13 changes: 6 additions & 7 deletions tests/baselines/reference/instantiationExpressionErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ const a2 = f.g<number>; // () => number
>g : <U>() => U

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

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

const a5 = f['g']<number>; // () => number
>a5 : () => number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 (2 errors) ====
declare namespace A {
export class b<T> {
static d: number;
constructor(x: T);
}
}

type c = unknown;

declare const a: typeof A | undefined;

a?.b<c>.d;
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'd'.

a?.b.d

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [optionalChainWithInstantiationExpression1.ts]
declare namespace A {
export class b<T> {
static d: number;
constructor(x: T);
}
}

type c = unknown;

declare const a: typeof A | undefined;

a?.b<c>.d;

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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts ===
declare namespace A {
>A : Symbol(A, Decl(optionalChainWithInstantiationExpression1.ts, 0, 0))

export class b<T> {
>b : Symbol(b, Decl(optionalChainWithInstantiationExpression1.ts, 0, 21))
>T : Symbol(T, Decl(optionalChainWithInstantiationExpression1.ts, 1, 19))

static d: number;
>d : Symbol(b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))

constructor(x: T);
>x : Symbol(x, Decl(optionalChainWithInstantiationExpression1.ts, 3, 20))
>T : Symbol(T, Decl(optionalChainWithInstantiationExpression1.ts, 1, 19))
}
}

type c = unknown;
>c : Symbol(c, Decl(optionalChainWithInstantiationExpression1.ts, 5, 1))

declare const a: typeof A | undefined;
>a : Symbol(a, Decl(optionalChainWithInstantiationExpression1.ts, 9, 13))
>A : Symbol(A, Decl(optionalChainWithInstantiationExpression1.ts, 0, 0))

a?.b<c>.d;
>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))

a?.b.d
>a?.b.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))
>d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts ===
declare namespace A {
>A : typeof A

export class b<T> {
>b : b<T>

static d: number;
>d : number

constructor(x: T);
>x : T
}
}

type c = unknown;
>c : unknown

declare const a: typeof A | undefined;
>a : typeof A
>A : typeof A

a?.b<c>.d;
>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

a?.b.d
>a?.b.d : number
>a?.b : typeof A.b
>a : typeof A
>b : typeof A.b
>d : number

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 (2 errors) ====
declare namespace A {
export class b<T> {
static d: number;
constructor(x: T);
}
}

type c = unknown;

declare const a: typeof A | undefined;

a?.b<c>.d;
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'd'.

a?.b.d

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [optionalChainWithInstantiationExpression1.ts]
declare namespace A {
export class b<T> {
static d: number;
constructor(x: T);
}
}

type c = unknown;

declare const a: typeof A | undefined;

a?.b<c>.d;

a?.b.d


//// [optionalChainWithInstantiationExpression1.js]
a?.b;
d;
a?.b.d;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/optionalChainWithInstantiationExpression1.ts ===
declare namespace A {
>A : Symbol(A, Decl(optionalChainWithInstantiationExpression1.ts, 0, 0))

export class b<T> {
>b : Symbol(b, Decl(optionalChainWithInstantiationExpression1.ts, 0, 21))
>T : Symbol(T, Decl(optionalChainWithInstantiationExpression1.ts, 1, 19))

static d: number;
>d : Symbol(b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))

constructor(x: T);
>x : Symbol(x, Decl(optionalChainWithInstantiationExpression1.ts, 3, 20))
>T : Symbol(T, Decl(optionalChainWithInstantiationExpression1.ts, 1, 19))
}
}

type c = unknown;
>c : Symbol(c, Decl(optionalChainWithInstantiationExpression1.ts, 5, 1))

declare const a: typeof A | undefined;
>a : Symbol(a, Decl(optionalChainWithInstantiationExpression1.ts, 9, 13))
>A : Symbol(A, Decl(optionalChainWithInstantiationExpression1.ts, 0, 0))

a?.b<c>.d;
>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))

a?.b.d
>a?.b.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))
>d : Symbol(A.b.d, Decl(optionalChainWithInstantiationExpression1.ts, 1, 23))

Loading