Skip to content

Commit 137998e

Browse files
authored
fix(3810): handle deprecated tags in completions (#3828)
1 parent a8b2a52 commit 137998e

14 files changed

Lines changed: 103 additions & 11 deletions

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,8 @@ function parseExpectedCompletionItem(expr: ts.Expression, codeActionArgs?: Verif
10771077
return getGoStringLiteral(strExpr.text);
10781078
}
10791079
if (strExpr = getObjectLiteralExpression(expr)) {
1080-
let isDeprecated = false; // !!!
10811080
let isOptional = false;
1081+
const completionItemTags = new Set<string>();
10821082
let sourceInit: ts.StringLiteralLike | undefined;
10831083
let extensions: string[] = []; // !!!
10841084
let itemProps: string[] = [];
@@ -1104,11 +1104,14 @@ function parseExpectedCompletionItem(expr: ts.Expression, codeActionArgs?: Verif
11041104
break;
11051105
}
11061106
case "sortText":
1107-
const result = parseSortText(init);
1108-
itemProps.push(`SortText: new(string(${result})),`);
1109-
if (result === "ls.SortTextOptionalMember") {
1107+
const sortText = parseSortText(init);
1108+
itemProps.push(`SortText: new(string(${sortText.expression})),`);
1109+
if (sortText.expression === "ls.SortTextOptionalMember") {
11101110
isOptional = true;
11111111
}
1112+
if (sortText.deprecated) {
1113+
completionItemTags.add("lsproto.CompletionItemTagDeprecated");
1114+
}
11121115
break;
11131116
case "insertText": {
11141117
let insertTextInit;
@@ -1144,7 +1147,10 @@ function parseExpectedCompletionItem(expr: ts.Expression, codeActionArgs?: Verif
11441147
break;
11451148
case "kindModifiers":
11461149
const modifiers = parseKindModifiers(init);
1147-
({ isDeprecated, isOptional, extensions } = modifiers);
1150+
({ isOptional, extensions } = modifiers);
1151+
if (modifiers.isDeprecated) {
1152+
completionItemTags.add("lsproto.CompletionItemTagDeprecated");
1153+
}
11481154
break;
11491155
case "text": {
11501156
let textInit;
@@ -1258,6 +1264,8 @@ function parseExpectedCompletionItem(expr: ts.Expression, codeActionArgs?: Verif
12581264
}
12591265
if (filterText) itemProps.unshift(`FilterText: new(${getGoStringLiteral(filterText)}),`);
12601266
if (insertText) itemProps.unshift(`InsertText: new(${getGoStringLiteral(insertText)}),`);
1267+
const tags = formatCompletionItemTags(completionItemTags);
1268+
if (tags) itemProps.push(tags);
12611269
itemProps.unshift(`Label: ${getGoStringLiteral(name!)},`);
12621270
return `&lsproto.CompletionItem{\n${itemProps.join("\n")}}`;
12631271
}
@@ -3158,11 +3166,24 @@ function parseKindModifiers(expr: ts.Expression): { isOptional: boolean; isDepre
31583166
};
31593167
}
31603168

3161-
function parseSortText(expr: ts.Expression): string {
3169+
interface ParsedSortText {
3170+
expression: string;
3171+
deprecated: boolean;
3172+
}
3173+
3174+
function parseSortText(expr: ts.Expression): ParsedSortText {
31623175
if (ts.isCallExpression(expr) && expr.expression.getText() === "completion.SortText.Deprecated") {
3163-
return `ls.DeprecateSortText(${parseSortText(expr.arguments[0])})`;
3176+
const inner = parseSortText(expr.arguments[0]);
3177+
return {
3178+
expression: `ls.DeprecateSortText(${inner.expression})`,
3179+
deprecated: true,
3180+
};
31643181
}
3165-
const text = expr.getText();
3182+
3183+
return { expression: parseSortTextExpression(expr.getText()), deprecated: false };
3184+
}
3185+
3186+
function parseSortTextExpression(text: string): string {
31663187
switch (text) {
31673188
case "completion.SortText.LocalDeclarationPriority":
31683189
return "ls.SortTextLocalDeclarationPriority";
@@ -3187,6 +3208,13 @@ function parseSortText(expr: ts.Expression): string {
31873208
}
31883209
}
31893210
3211+
function formatCompletionItemTags(tags: Set<string>): string | undefined {
3212+
if (tags.size === 0) {
3213+
return undefined;
3214+
}
3215+
return `Tags: &[]lsproto.CompletionItemTag{${[...tags].join(", ")}},`;
3216+
}
3217+
31903218
function parseVerifyNavigateTo(args: ts.NodeArray<ts.Expression>): [VerifyNavToCmd] {
31913219
const goArgs = [];
31923220
for (const arg of args) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 TestCompletionsDeprecatedTags(t *testing.T) {
14+
t.Parallel()
15+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
16+
17+
const content = `const o = {
18+
/** @deprecated */
19+
a: 1,
20+
b: 2,
21+
c: 3,
22+
}
23+
o./**/`
24+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
25+
defer done()
26+
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
27+
IsIncomplete: false,
28+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
29+
CommitCharacters: &DefaultCommitCharacters,
30+
EditRange: Ignored,
31+
},
32+
Items: &fourslash.CompletionsExpectedItems{
33+
Includes: []fourslash.CompletionsExpectedItem{
34+
&lsproto.CompletionItem{
35+
Label: "a",
36+
Kind: new(lsproto.CompletionItemKindField),
37+
Tags: &[]lsproto.CompletionItemTag{lsproto.CompletionItemTagDeprecated},
38+
SortText: new(string(ls.DeprecateSortText(ls.SortTextLocationPriority))),
39+
},
40+
},
41+
},
42+
})
43+
}

internal/fourslash/tests/gen/completionListAfterRegularExpressionLiteral01_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionListAfterRegularExpressionLiteral1_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionListAfterStringLiteral1_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionsWithDeprecatedTag10_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionsWithDeprecatedTag1_test.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionsWithDeprecatedTag2_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionsWithDeprecatedTag5_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fourslash/tests/gen/completionsWithDeprecatedTag6_test.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)