From d3943fc86fc2f7d828cee58c1dbcac1a6a07aeef Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:18:31 -0700 Subject: [PATCH] Disallow line break between entity name and type arguments in typeof expression (#48755) --- src/compiler/parser.ts | 3 ++- .../reference/newLineInTypeofInstantiation.js | 9 +++++++++ .../reference/newLineInTypeofInstantiation.symbols | 12 ++++++++++++ .../reference/newLineInTypeofInstantiation.types | 9 +++++++++ tests/cases/compiler/newLineInTypeofInstantiation.ts | 5 +++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/newLineInTypeofInstantiation.js create mode 100644 tests/baselines/reference/newLineInTypeofInstantiation.symbols create mode 100644 tests/baselines/reference/newLineInTypeofInstantiation.types create mode 100644 tests/cases/compiler/newLineInTypeofInstantiation.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 247381f0251..e57a3fa98f3 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3189,7 +3189,8 @@ namespace ts { const pos = getNodePos(); parseExpected(SyntaxKind.TypeOfKeyword); const entityName = parseEntityName(/*allowReservedWords*/ true, /*allowPrivateIdentifiers*/ true); - const typeArguments = tryParseTypeArguments(); + // Make sure we perform ASI to prevent parsing the next line's type arguments as part of an instantiation expression. + const typeArguments = !scanner.hasPrecedingLineBreak() ? tryParseTypeArguments() : undefined; return finishNode(factory.createTypeQueryNode(entityName, typeArguments), pos); } diff --git a/tests/baselines/reference/newLineInTypeofInstantiation.js b/tests/baselines/reference/newLineInTypeofInstantiation.js new file mode 100644 index 00000000000..b43b6770305 --- /dev/null +++ b/tests/baselines/reference/newLineInTypeofInstantiation.js @@ -0,0 +1,9 @@ +//// [newLineInTypeofInstantiation.ts] +interface Example { + (a: number): typeof a + + (): void +} + + +//// [newLineInTypeofInstantiation.js] diff --git a/tests/baselines/reference/newLineInTypeofInstantiation.symbols b/tests/baselines/reference/newLineInTypeofInstantiation.symbols new file mode 100644 index 00000000000..2a3ccf8097e --- /dev/null +++ b/tests/baselines/reference/newLineInTypeofInstantiation.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/newLineInTypeofInstantiation.ts === +interface Example { +>Example : Symbol(Example, Decl(newLineInTypeofInstantiation.ts, 0, 0)) + + (a: number): typeof a +>a : Symbol(a, Decl(newLineInTypeofInstantiation.ts, 1, 5)) +>a : Symbol(a, Decl(newLineInTypeofInstantiation.ts, 1, 5)) + + (): void +>T : Symbol(T, Decl(newLineInTypeofInstantiation.ts, 3, 5)) +} + diff --git a/tests/baselines/reference/newLineInTypeofInstantiation.types b/tests/baselines/reference/newLineInTypeofInstantiation.types new file mode 100644 index 00000000000..a57d7df124b --- /dev/null +++ b/tests/baselines/reference/newLineInTypeofInstantiation.types @@ -0,0 +1,9 @@ +=== tests/cases/compiler/newLineInTypeofInstantiation.ts === +interface Example { + (a: number): typeof a +>a : number +>a : number + + (): void +} + diff --git a/tests/cases/compiler/newLineInTypeofInstantiation.ts b/tests/cases/compiler/newLineInTypeofInstantiation.ts new file mode 100644 index 00000000000..99259acec79 --- /dev/null +++ b/tests/cases/compiler/newLineInTypeofInstantiation.ts @@ -0,0 +1,5 @@ +interface Example { + (a: number): typeof a + + (): void +}