diff --git a/tsc/internal/fourslash/tests/superTaggedTemplateTypeArgumentsNoCrash_test.go b/tsc/internal/fourslash/tests/superTaggedTemplateTypeArgumentsNoCrash_test.go new file mode 100644 index 0000000000000..91e9bf883a14e --- /dev/null +++ b/tsc/internal/fourslash/tests/superTaggedTemplateTypeArgumentsNoCrash_test.go @@ -0,0 +1,26 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestSuperTaggedTemplateTypeArgumentsNoCrash(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @Filename: /main.ts +class SomeBase { + a!: A; b!: B; c!: C; +} +class SomeDerived extends SomeBase { + constructor() { + /*2*/super ` + "`hello world`" + `; + } +}` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyBaselineDocumentHighlights(t, nil /*preferences*/, "1", "2", "3") + f.VerifyBaselineFindAllReferences(t, "1", "2", "3") +} diff --git a/tsc/internal/parser/parser.go b/tsc/internal/parser/parser.go index c89104f31263f..cc456b05bd0b7 100644 --- a/tsc/internal/parser/parser.go +++ b/tsc/internal/parser/parser.go @@ -5408,9 +5408,17 @@ func (p *Parser) parseSuperExpression() *ast.Expression { typeArguments := p.tryParseTypeArgumentsInExpression() if typeArguments != nil { p.parseErrorAt(startPos, p.nodePos(), diagnostics.X_super_may_not_use_type_arguments) - if !p.isTemplateStartOfTaggedTemplate() { - expression = p.finishNode(p.factory.NewExpressionWithTypeArguments(expression, typeArguments), pos) + if p.isTemplateStartOfTaggedTemplate() { + // `super` followed by a template is a tagged template whose tag is `super.` with a missing name. + // The property access ends at `super`, and the type arguments are wrapped around it so that + // parseMemberExpressionRest moves them onto the tagged template, which keeps them out of the + // range of the property access. + p.parseErrorAtCurrentToken(diagnostics.X_super_must_be_followed_by_an_argument_list_or_member_access) + name := p.finishNodeWithEnd(p.newIdentifier(""), startPos, startPos) + access := p.finishNodeWithEnd(p.factory.NewPropertyAccessExpression(expression, nil /*questionDotToken*/, name, ast.NodeFlagsNone), pos, startPos) + return p.finishNode(p.factory.NewExpressionWithTypeArguments(access, typeArguments), pos) } + expression = p.finishNode(p.factory.NewExpressionWithTypeArguments(expression, typeArguments), pos) } } if p.token == ast.KindOpenParenToken || p.token == ast.KindDotToken || p.token == ast.KindOpenBracketToken { diff --git a/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.symbols b/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.symbols index 7a2956a3d888c..05a6197d1c41e 100644 --- a/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.symbols +++ b/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.symbols @@ -80,5 +80,6 @@ class SomeDerived extends SomeBase { constructor() { super `hello world`; >super : Symbol(SomeBase, Decl(taggedTemplatesWithTypeArguments2.ts, 27, 10)) +>T : Symbol(T, Decl(taggedTemplatesWithTypeArguments2.ts, 33, 18)) } } diff --git a/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.types b/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.types index 1da07ea7a790b..36478cc8b9a1c 100644 --- a/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.types +++ b/tsc/testdata/baselines/reference/conformance/taggedTemplatesWithTypeArguments2.types @@ -92,7 +92,7 @@ class SomeDerived extends SomeBase { constructor() { super `hello world`; >super `hello world` : any ->super : any +>super : any >super : SomeBase > : any >`hello world` : "hello world" diff --git a/tsc/testdata/baselines/reference/fourslash/documentHighlights/superTaggedTemplateTypeArgumentsNoCrash.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/documentHighlights/superTaggedTemplateTypeArgumentsNoCrash.baseline.jsonc new file mode 100644 index 0000000000000..9a0799558b78f --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/documentHighlights/superTaggedTemplateTypeArgumentsNoCrash.baseline.jsonc @@ -0,0 +1,36 @@ +// === documentHighlights === +// === /main.ts === +// class SomeBase { +// a!: A; b!: B; c!: C; +// } +// class SomeDerived extends SomeBase { +// constructor() { +// super `hello world`; +// } +// } + + + +// === documentHighlights === +// === /main.ts === +// class SomeBase { +// a!: A; b!: B; c!: C; +// } +// class SomeDerived extends SomeBase { +// constructor() { +// /*HIGHLIGHTS*/[|super|] `hello world`; +// } +// } + + + +// === documentHighlights === +// === /main.ts === +// class SomeBase { +// a!: A; b!: B; c!: C; +// } +// class SomeDerived<[|T|]> extends SomeBase { +// constructor() { +// super `hello world`; +// } +// } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/fourslash/findAllReferences/superTaggedTemplateTypeArgumentsNoCrash.baseline.jsonc b/tsc/testdata/baselines/reference/fourslash/findAllReferences/superTaggedTemplateTypeArgumentsNoCrash.baseline.jsonc new file mode 100644 index 0000000000000..ad5c604fdc1f7 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/findAllReferences/superTaggedTemplateTypeArgumentsNoCrash.baseline.jsonc @@ -0,0 +1,36 @@ +// === findAllReferences === +// === /main.ts === +// class SomeBase { +// a!: A; b!: B; c!: C; +// } +// class SomeDerived extends SomeBase { +// constructor() { +// super `hello world`; +// } +// } + + + +// === findAllReferences === +// === /main.ts === +// class SomeBase { +// a!: A; b!: B; c!: C; +// } +// class SomeDerived extends SomeBase { +// constructor() { +// /*FIND ALL REFS*/[|super|] `hello world`; +// } +// } + + + +// === findAllReferences === +// === /main.ts === +// class SomeBase { +// a!: A; b!: B; c!: C; +// } +// class SomeDerived<[|T|]> extends SomeBase { +// constructor() { +// super `hello world`; +// } +// } \ No newline at end of file