Skip to content

Commit e4f543c

Browse files
authored
fix: show completion after class members with const assertion initializers (#2400)
1 parent da09eb6 commit e4f543c

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
8+
"github.com/microsoft/typescript-go/internal/ls"
9+
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
10+
"github.com/microsoft/typescript-go/internal/testutil"
11+
)
12+
13+
func TestClassMembersAfterConstAssertionInitializer(t *testing.T) {
14+
t.Parallel()
15+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
16+
const content = `
17+
interface A {
18+
a: number
19+
def: string
20+
}
21+
22+
class B implements A {
23+
a = 1 as const
24+
/**/
25+
}
26+
`
27+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
28+
defer done()
29+
30+
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
31+
IsIncomplete: false,
32+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
33+
CommitCharacters: &[]string{},
34+
EditRange: Ignored,
35+
},
36+
Items: &fourslash.CompletionsExpectedItems{
37+
Exact: append([]fourslash.CompletionsExpectedItem{
38+
&lsproto.CompletionItem{
39+
Label: "def",
40+
Kind: PtrTo(lsproto.CompletionItemKindField),
41+
SortText: PtrTo(string(ls.SortTextLocationPriority)),
42+
},
43+
}, CompletionClassElementKeywords...),
44+
},
45+
})
46+
}

internal/ls/completions.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4793,9 +4793,10 @@ func isSolelyIdentifierDefinitionLocation(
47934793
return ast.IsFunctionLike(parent) && !ast.IsMethodDeclaration(parent)
47944794
}
47954795

4796+
tokenKind := keywordForNode(contextToken)
47964797
// If the previous token is keyword corresponding to class member completion keyword
47974798
// there will be completion available here
4798-
if isClassMemberCompletionKeyword(keywordForNode(contextToken)) && isFromObjectTypeDeclaration(contextToken) {
4799+
if isClassMemberCompletionKeyword(tokenKind) && isFromObjectTypeDeclaration(contextToken) {
47994800
return false
48004801
}
48014802

@@ -4805,15 +4806,15 @@ func isSolelyIdentifierDefinitionLocation(
48054806
// - its name of the parameter and not being edited
48064807
// eg. constructor(a |<- this shouldnt show completion
48074808
if !ast.IsIdentifier(contextToken) ||
4808-
ast.IsParameterPropertyModifier(keywordForNode(contextToken)) ||
4809+
ast.IsParameterPropertyModifier(tokenKind) ||
48094810
isCurrentlyEditingNode(contextToken, file, position) {
48104811
return false
48114812
}
48124813
}
48134814

48144815
// Previous token may have been a keyword that was converted to an identifier.
48154816
switch keywordForNode(contextToken) {
4816-
case ast.KindAbstractKeyword, ast.KindClassKeyword, ast.KindConstKeyword, ast.KindDeclareKeyword,
4817+
case ast.KindAbstractKeyword, ast.KindClassKeyword, ast.KindDeclareKeyword,
48174818
ast.KindEnumKeyword, ast.KindFunctionKeyword, ast.KindInterfaceKeyword, ast.KindLetKeyword,
48184819
ast.KindPrivateKeyword, ast.KindProtectedKeyword, ast.KindPublicKeyword,
48194820
ast.KindStaticKeyword, ast.KindVarKeyword:
@@ -4848,7 +4849,9 @@ func isSolelyIdentifierDefinitionLocation(
48484849
return true
48494850
}
48504851
}
4851-
4852+
if tokenKind == ast.KindConstKeyword {
4853+
return true
4854+
}
48524855
return ast.IsDeclarationName(contextToken) &&
48534856
!ast.IsShorthandPropertyAssignment(parent) &&
48544857
!ast.IsJsxAttribute(parent) &&

0 commit comments

Comments
 (0)