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

Instantiation expression can be followed by line break or binary operator #49353

Merged
merged 9 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5732,6 +5732,12 @@ namespace ts {
case SyntaxKind.NoSubstitutionTemplateLiteral: // foo<T> `...`
case SyntaxKind.TemplateHead: // foo<T> `...${100}...`
return true;
// These strict mode reserved words are valid identifiers in non-strict mode, and can thus
// start an expression. However, when they follow a type argument list and are immediately
// preceded by a line break, we don't consider them expression starters.
DanielRosenwasser marked this conversation as resolved.
Show resolved Hide resolved
case SyntaxKind.InterfaceKeyword:
case SyntaxKind.LetKeyword:
Copy link
Member

Choose a reason for hiding this comment

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

You need to cover more than just these cases and have tests for them - for example

class C {
    static specialFoo = f<string>
    static bar = 123;
}

class D {
    public specialFoo = f<string>
    public bar = 123;
}

class E {
    private specialFoo = f<string>
    private bar = 123;
}

class F {
    protected specialFoo = f<string>
    protected bar = 123;
}

return scanner.hasPrecedingLineBreak();
}
// Consider something a type argument list only if the following token can't start an expression.
return !isStartOfExpression();
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/instantiationExpressionErrors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpr

const x4 = f<true>
if (true) {}

// Parsed as instantiation expression

const x5 = f<true>
let yy = 0;

// Parsed as instantiation expression

const x6 = f<true>
interface I {}

26 changes: 26 additions & 0 deletions tests/baselines/reference/instantiationExpressionErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ true;

const x4 = f<true>
if (true) {}

// Parsed as instantiation expression

const x5 = f<true>
let yy = 0;

// Parsed as instantiation expression

const x6 = f<true>
interface I {}


//// [instantiationExpressionErrors.js]
Expand Down Expand Up @@ -78,6 +88,11 @@ true;
// Parsed as instantiation expression
var x4 = (f);
if (true) { }
// Parsed as instantiation expression
var x5 = (f);
var yy = 0;
// Parsed as instantiation expression
var x6 = (f);


//// [instantiationExpressionErrors.d.ts]
Expand Down Expand Up @@ -113,3 +128,14 @@ declare const x4: {
(): true;
g<U>(): U;
};
declare const x5: {
(): true;
g<U>(): U;
};
declare let yy: number;
declare const x6: {
(): true;
g<U>(): U;
};
interface I {
}
18 changes: 18 additions & 0 deletions tests/baselines/reference/instantiationExpressionErrors.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,21 @@ const x4 = f<true>

if (true) {}

// Parsed as instantiation expression

const x5 = f<true>
>x5 : Symbol(x5, Decl(instantiationExpressionErrors.ts, 49, 5))
>f : Symbol(f, Decl(instantiationExpressionErrors.ts, 0, 11))

let yy = 0;
>yy : Symbol(yy, Decl(instantiationExpressionErrors.ts, 50, 3))

// Parsed as instantiation expression

const x6 = f<true>
>x6 : Symbol(x6, Decl(instantiationExpressionErrors.ts, 54, 5))
>f : Symbol(f, Decl(instantiationExpressionErrors.ts, 0, 11))

interface I {}
>I : Symbol(I, Decl(instantiationExpressionErrors.ts, 54, 18))

20 changes: 20 additions & 0 deletions tests/baselines/reference/instantiationExpressionErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,23 @@ const x4 = f<true>
if (true) {}
>true : true

// Parsed as instantiation expression

const x5 = f<true>
>x5 : { (): true; g<U>(): U; }
>f : { <T>(): T; g<U>(): U; }
>true : true

let yy = 0;
>yy : number
>0 : 0

// Parsed as instantiation expression

const x6 = f<true>
>x6 : { (): true; g<U>(): U; }
>f : { <T>(): T; g<U>(): U; }
>true : true

interface I {}

Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ true;

const x4 = f<true>
if (true) {}

// Parsed as instantiation expression

const x5 = f<true>
let yy = 0;

// Parsed as instantiation expression

const x6 = f<true>
interface I {}