diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 7c90f3f320fbc..6acae42dc15e1 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -271,6 +271,12 @@ func (b *NodeBuilderImpl) checkTypeExpandability(t *Type) { } func (b *NodeBuilderImpl) appendReferenceToType(root *ast.TypeNode, ref *ast.TypeNode) *ast.TypeNode { + if !ast.IsImportTypeNode(root) && (ast.IsExpressionWithTypeArguments(root) || ast.IsExpressionWithTypeArguments(ref)) { + // Type arguments precede the last name of root or ref, so the + // reference is an expression with type arguments rather than a + // qualified name. + return b.appendReferenceToExpression(b.referenceToExpression(root), b.referenceToExpression(ref)) + } if ast.IsImportTypeNode(root) { // first shift type arguments @@ -323,6 +329,30 @@ func (b *NodeBuilderImpl) appendReferenceToType(root *ast.TypeNode, ref *ast.Typ return expr } +// referenceToExpression returns a type reference, or a reference that is +// already an expression, as an expression. +func (b *NodeBuilderImpl) referenceToExpression(ref *ast.Node) *ast.Expression { + if ast.IsTypeReferenceNode(ref) { + return b.createExpressionWithTypeArguments(b.createAccessExpression(ref.AsTypeReferenceNode().TypeName), ref.TypeArgumentList()) + } + return b.createAccessExpression(ref) +} + +// appendReferenceToExpression returns ref, a reference expression, as a +// reference to a member of root. +func (b *NodeBuilderImpl) appendReferenceToExpression(root *ast.Expression, ref *ast.Expression) *ast.Expression { + switch { + case ast.IsIdentifier(ref): + return b.f.NewPropertyAccessExpression(root, nil /*questionDotToken*/, ref, ast.NodeFlagsNone) + case ast.IsPropertyAccessExpression(ref): + return b.f.NewPropertyAccessExpression(b.appendReferenceToExpression(root, ref.Expression()), nil /*questionDotToken*/, ref.Name(), ast.NodeFlagsNone) + case ast.IsExpressionWithTypeArguments(ref): + return b.f.NewExpressionWithTypeArguments(b.appendReferenceToExpression(root, ref.Expression()), ref.AsExpressionWithTypeArguments().TypeArguments) + default: + panic("unexpected reference kind: " + ref.Kind.String()) + } +} + func getAccessStack(ref *ast.Node) []*ast.Node { var state *ast.Node = ref.AsTypeReferenceNode().TypeName ids := []*ast.Node{} diff --git a/tsc/internal/fourslash/tests/inlayHintsAccessorTypeCrash_test.go b/tsc/internal/fourslash/tests/inlayHintsAccessorTypeCrash_test.go new file mode 100644 index 0000000000000..a69c062e2bf67 --- /dev/null +++ b/tsc/internal/fourslash/tests/inlayHintsAccessorTypeCrash_test.go @@ -0,0 +1,19 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/core" + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/ls/lsutil" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestInlayHintsAccessorTypeCrash(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `var divergent = { get a(): number { return 0; }, set a(value: string) {} };` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{IncludeInlayVariableTypeHints: core.TSTrue}) +} diff --git a/tsc/internal/fourslash/tests/quickInfoLocalTypeInGenericFunctionCrash_test.go b/tsc/internal/fourslash/tests/quickInfoLocalTypeInGenericFunctionCrash_test.go new file mode 100644 index 0000000000000..b5957bd8436e7 --- /dev/null +++ b/tsc/internal/fourslash/tests/quickInfoLocalTypeInGenericFunctionCrash_test.go @@ -0,0 +1,30 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/fourslash" + "github.com/microsoft/TypeScript/tsc/internal/testutil" +) + +func TestQuickInfoLocalTypeInGenericFunctionCrash(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `function /*1*/foo() { + class /*2*/X { + /*3*/m() { + return (function () { + class /*4*/Y { + } + return new /*5*/Y(); + })(); + } + } + var /*6*/x = new X(); + return x./*7*/m(); +} +var /*8*/x = foo();` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyBaselineHover(t) +} diff --git a/tsc/internal/ls/inlay_hints.go b/tsc/internal/ls/inlay_hints.go index f2aadcb6eca22..c4d4cebf39717 100644 --- a/tsc/internal/ls/inlay_hints.go +++ b/tsc/internal/ls/inlay_hints.go @@ -705,6 +705,22 @@ func (s *inlayHintState) getInlayHintLabelParts(node *ast.Node, idToSymbol map[* parts = append(parts, &lsproto.InlayHintLabelPart{Value: ": "}) visitForDisplayParts(node.Type()) } + case ast.KindGetAccessor, ast.KindSetAccessor: + if len(node.ModifierNodes()) > 0 { + visitDisplayPartList(node.ModifierNodes(), " ") + parts = append(parts, &lsproto.InlayHintLabelPart{Value: " "}) + } + if node.Kind == ast.KindGetAccessor { + parts = append(parts, &lsproto.InlayHintLabelPart{Value: "get "}) + } else { + parts = append(parts, &lsproto.InlayHintLabelPart{Value: "set "}) + } + visitForDisplayParts(node.Name()) + visitParametersAndTypeParameters(node) + if node.Type() != nil { + parts = append(parts, &lsproto.InlayHintLabelPart{Value: ": "}) + visitForDisplayParts(node.Type()) + } case ast.KindCallSignature: visitParametersAndTypeParameters(node) if node.Type() != nil { diff --git a/tsc/testdata/baselines/reference/fourslash/inlayHints/inlayHintsAccessorTypeCrash.baseline b/tsc/testdata/baselines/reference/fourslash/inlayHints/inlayHintsAccessorTypeCrash.baseline new file mode 100644 index 0000000000000..e31326af313d0 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/inlayHints/inlayHintsAccessorTypeCrash.baseline @@ -0,0 +1,109 @@ +// === Inlay Hints === +var divergent = { get a(): number { return 0; }, set a(value: string) {} }; + ^ +{ + "position": { + "line": 0, + "character": 13 + }, + "label": [ + { + "value": ": " + }, + { + "value": "{" + }, + { + "value": " " + }, + { + "value": "get " + }, + { + "value": "a", + "location": { + "uri": "file:///inlayHintsAccessorTypeCrash.ts", + "range": { + "start": { + "line": 0, + "character": 22 + }, + "end": { + "line": 0, + "character": 23 + } + } + } + }, + { + "value": "(" + }, + { + "value": ")" + }, + { + "value": ": " + }, + { + "value": "number" + }, + { + "value": "; " + }, + { + "value": "set " + }, + { + "value": "a", + "location": { + "uri": "file:///inlayHintsAccessorTypeCrash.ts", + "range": { + "start": { + "line": 0, + "character": 22 + }, + "end": { + "line": 0, + "character": 23 + } + } + } + }, + { + "value": "(" + }, + { + "value": "value", + "location": { + "uri": "file:///inlayHintsAccessorTypeCrash.ts", + "range": { + "start": { + "line": 0, + "character": 55 + }, + "end": { + "line": 0, + "character": 60 + } + } + } + }, + { + "value": ": " + }, + { + "value": "string" + }, + { + "value": ")" + }, + { + "value": " " + }, + { + "value": "}" + } + ], + "kind": 1, + "paddingLeft": true +} \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/fourslash/quickInfo/quickInfoLocalTypeInGenericFunctionCrash.baseline b/tsc/testdata/baselines/reference/fourslash/quickInfo/quickInfoLocalTypeInGenericFunctionCrash.baseline new file mode 100644 index 0000000000000..7f2af9703e124 --- /dev/null +++ b/tsc/testdata/baselines/reference/fourslash/quickInfo/quickInfoLocalTypeInGenericFunctionCrash.baseline @@ -0,0 +1,298 @@ +// === QuickInfo === +=== /quickInfoLocalTypeInGenericFunctionCrash.ts === +// function foo() { +// ^^^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | function foo(): X.m.(Anonymous function).Y +// | ``` +// | +// | ---------------------------------------------------------------------- +// class X { +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | class X +// | ``` +// | +// | ---------------------------------------------------------------------- +// m() { +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | (method) X.m(): (Anonymous function).Y +// | ``` +// | +// | ---------------------------------------------------------------------- +// return (function () { +// class Y { +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | class Y +// | ``` +// | +// | ---------------------------------------------------------------------- +// } +// return new Y(); +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | constructor Y(): Y +// | ``` +// | +// | ---------------------------------------------------------------------- +// })(); +// } +// } +// var x = new X(); +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | var x: X +// | ``` +// | +// | ---------------------------------------------------------------------- +// return x.m(); +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | (method) X.m(): X.m.(Anonymous function).Y +// | ``` +// | +// | ---------------------------------------------------------------------- +// } +// var x = foo(); +// ^ +// | ---------------------------------------------------------------------- +// | ```typescript +// | var x: foo.X.m.(Anonymous function).Y +// | ``` +// | +// | ---------------------------------------------------------------------- +[ + { + "marker": { + "Position": 9, + "LSPosition": { + "line": 0, + "character": 9 + }, + "Name": "1", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\nfunction foo(): X.m.(Anonymous function).Y\n```\n" + }, + "range": { + "start": { + "line": 0, + "character": 9 + }, + "end": { + "line": 0, + "character": 12 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 30, + "LSPosition": { + "line": 1, + "character": 10 + }, + "Name": "2", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\nclass X\n```\n" + }, + "range": { + "start": { + "line": 1, + "character": 10 + }, + "end": { + "line": 1, + "character": 11 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 42, + "LSPosition": { + "line": 2, + "character": 8 + }, + "Name": "3", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\n(method) X.m(): (Anonymous function).Y\n```\n" + }, + "range": { + "start": { + "line": 2, + "character": 8 + }, + "end": { + "line": 2, + "character": 9 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 113, + "LSPosition": { + "line": 4, + "character": 22 + }, + "Name": "4", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\nclass Y\n```\n" + }, + "range": { + "start": { + "line": 4, + "character": 22 + }, + "end": { + "line": 4, + "character": 23 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 165, + "LSPosition": { + "line": 6, + "character": 27 + }, + "Name": "5", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\nconstructor Y(): Y\n```\n" + }, + "range": { + "start": { + "line": 6, + "character": 27 + }, + "end": { + "line": 6, + "character": 28 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 226, + "LSPosition": { + "line": 10, + "character": 8 + }, + "Name": "6", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\nvar x: X\n```\n" + }, + "range": { + "start": { + "line": 10, + "character": 8 + }, + "end": { + "line": 10, + "character": 9 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 252, + "LSPosition": { + "line": 11, + "character": 13 + }, + "Name": "7", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\n(method) X.m(): X.m.(Anonymous function).Y\n```\n" + }, + "range": { + "start": { + "line": 11, + "character": 13 + }, + "end": { + "line": 11, + "character": 14 + } + }, + "canIncreaseVerbosity": true + } + }, + { + "marker": { + "Position": 280, + "LSPosition": { + "line": 13, + "character": 4 + }, + "Name": "8", + "Data": {} + }, + "item": { + "contents": { + "kind": "markdown", + "value": "```typescript\nvar x: foo.X.m.(Anonymous function).Y\n```\n" + }, + "range": { + "start": { + "line": 13, + "character": 4 + }, + "end": { + "line": 13, + "character": 5 + } + }, + "canIncreaseVerbosity": true + } + } +] \ No newline at end of file