Skip to content
Closed
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
14 changes: 14 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ func (n *Node) Text() string {
return strings.Join(n.AsJSDocLinkCode().text, "")
case KindJSDocLinkPlain:
return strings.Join(n.AsJSDocLinkPlain().text, "")
case KindComputedPropertyName:
expression := n.Expression()
if IsStringOrNumericLiteralLike(expression) {
return "[" + expression.Text() + "]"
}
if source := GetSourceFileOfNode(expression); source != nil {
text := source.Text()
start := expression.Pos()
end := expression.End()
if start >= 0 && end >= start && end <= len(text) {
return "[" + text[start:end] + "]"
}
}
return "[]"
}
panic(fmt.Sprintf("Unhandled case in Node.Text: %T", n.data))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestInlayHintsComputedPropertyNameCrashCrash(t *testing.T) {
t.Parallel()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `const propertyName = "hello";
class Foo {
static readonly [propertyName] = true;
}
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{
InlayHints: lsutil.InlayHintsPreferences{
IncludeInlayPropertyDeclarationTypeHints: true,
},
})
}

func TestInlayHintsComputedPropertyNameBinaryExpression(t *testing.T) {
t.Parallel()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `const foo = "a";
const bar = "b";
function f() { return "c"; }
class Foo {
static readonly [foo + bar + f()] = 1;
[foo + bar + f()] = 2;
}
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{
InlayHints: lsutil.InlayHintsPreferences{
IncludeInlayPropertyDeclarationTypeHints: true,
},
})
}

func TestInlayHintsComputedPropertyNameConditionalExpression(t *testing.T) {
t.Parallel()

defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `const foo = "a";
const bar = "b";
class Foo {
static readonly [foo ? bar : "c"] = true;
[foo ? bar : "c"] = false;
}
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineInlayHints(t, nil /*span*/, &lsutil.UserPreferences{
InlayHints: lsutil.InlayHintsPreferences{
IncludeInlayPropertyDeclarationTypeHints: true,
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// === Inlay Hints ===
static readonly [foo + bar + f()] = 1;
^
{
"position": {
"line": 4,
"character": 35
},
"label": [
{
"value": ": "
},
{
"value": "1"
}
],
"kind": 1,
"paddingLeft": true
}

[foo + bar + f()] = 2;
^
{
"position": {
"line": 5,
"character": 19
},
"label": [
{
"value": ": "
},
{
"value": "number"
}
],
"kind": 1,
"paddingLeft": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// === Inlay Hints ===
static readonly [foo ? bar : "c"] = true;
^
{
"position": {
"line": 3,
"character": 35
},
"label": [
{
"value": ": "
},
{
"value": "true"
}
],
"kind": 1,
"paddingLeft": true
}

[foo ? bar : "c"] = false;
^
{
"position": {
"line": 4,
"character": 19
},
"label": [
{
"value": ": "
},
{
"value": "boolean"
}
],
"kind": 1,
"paddingLeft": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// === Inlay Hints ===
static readonly [propertyName] = true;
^
{
"position": {
"line": 2,
"character": 32
},
"label": [
{
"value": ": "
},
{
"value": "true"
}
],
"kind": 1,
"paddingLeft": true
}