Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions tsc/internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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{}
Expand Down
19 changes: 19 additions & 0 deletions tsc/internal/fourslash/tests/inlayHintsAccessorTypeCrash_test.go
Original file line number Diff line number Diff line change
@@ -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})
}
Original file line number Diff line number Diff line change
@@ -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<A>() {
class /*2*/X {
/*3*/m<B, C>() {
return (function <D>() {
class /*4*/Y<E> {
}
return new /*5*/Y<string>();
})<Date>();
}
}
var /*6*/x = new X();
return x./*7*/m<number, boolean>();
}
var /*8*/x = foo<void>();`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineHover(t)
}
16 changes: 16 additions & 0 deletions tsc/internal/ls/inlay_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Loading
Loading