Skip to content

Commit 688c31d

Browse files
iisaduanCopilot
andauthored
refactor userpreferences (#1794)
Co-authored-by: Copilot <[email protected]>
1 parent f2d955b commit 688c31d

18 files changed

+276
-125
lines changed

internal/checker/nodebuilderimpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,8 @@ func (b *nodeBuilderImpl) getSpecifierForModuleSymbol(symbol *ast.Symbol, overri
12071207
contextFile,
12081208
host,
12091209
modulespecifiers.UserPreferences{
1210-
ImportModuleSpecifierPreference: specifierPref,
1211-
ImportModuleSpecifierEndingPreference: endingPref,
1210+
ImportModuleSpecifierPreference: specifierPref,
1211+
ImportModuleSpecifierEnding: endingPref,
12121212
},
12131213
modulespecifiers.ModuleSpecifierOptions{
12141214
OverrideImportMode: overrideImportMode,

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,28 @@ function parseBaselineRenameArgs(funcName: string, args: readonly ts.Expression[
911911
}];
912912
}
913913

914+
function stringToTristate(s: string): string {
915+
switch (s) {
916+
case "true":
917+
return "core.TSTrue";
918+
case "false":
919+
return "core.TSFalse";
920+
default:
921+
return "core.TSUnknown";
922+
}
923+
}
924+
914925
function parseUserPreferences(arg: ts.ObjectLiteralExpression): string | undefined {
915926
const preferences: string[] = [];
916927
for (const prop of arg.properties) {
917928
if (ts.isPropertyAssignment(prop)) {
918929
switch (prop.name.getText()) {
919930
// !!! other preferences
920931
case "providePrefixAndSuffixTextForRename":
921-
preferences.push(`UseAliasesForRename: PtrTo(${prop.initializer.getText()})`);
932+
preferences.push(`UseAliasesForRename: ${stringToTristate(prop.initializer.getText())}`);
922933
break;
923934
case "quotePreference":
924-
preferences.push(`QuotePreference: PtrTo(ls.QuotePreference(${prop.initializer.getText()}))`);
935+
preferences.push(`QuotePreference: ls.QuotePreference(${prop.initializer.getText()})`);
925936
break;
926937
}
927938
}
@@ -1465,13 +1476,16 @@ function generateGoTest(failingTests: Set<string>, test: GoTest): string {
14651476
const commands = test.commands.map(cmd => generateCmd(cmd)).join("\n");
14661477
const imports = [`"github.com/microsoft/typescript-go/internal/fourslash"`];
14671478
// Only include these imports if the commands use them to avoid unused import errors.
1479+
if (commands.includes("core.")) {
1480+
imports.unshift(`"github.com/microsoft/typescript-go/internal/core"`);
1481+
}
14681482
if (commands.includes("ls.")) {
14691483
imports.push(`"github.com/microsoft/typescript-go/internal/ls"`);
14701484
}
14711485
if (commands.includes("lsproto.")) {
14721486
imports.push(`"github.com/microsoft/typescript-go/internal/lsp/lsproto"`);
14731487
}
1474-
if (usesHelper(commands)) {
1488+
if (usesFourslashUtil(commands)) {
14751489
imports.push(`. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"`);
14761490
}
14771491
imports.push(`"github.com/microsoft/typescript-go/internal/testutil"`);
@@ -1494,7 +1508,7 @@ func Test${testName}(t *testing.T) {
14941508
return template;
14951509
}
14961510

1497-
function usesHelper(goTxt: string): boolean {
1511+
function usesFourslashUtil(goTxt: string): boolean {
14981512
for (const [_, constant] of completionConstants) {
14991513
if (goTxt.includes(constant)) {
15001514
return true;

internal/fourslash/fourslash.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,11 +1611,11 @@ func (f *FourslashTest) verifyBaselineRename(
16111611

16121612
var renameOptions strings.Builder
16131613
if preferences != nil {
1614-
if preferences.UseAliasesForRename != nil {
1615-
fmt.Fprintf(&renameOptions, "// @useAliasesForRename: %v\n", *preferences.UseAliasesForRename)
1614+
if preferences.UseAliasesForRename != core.TSUnknown {
1615+
fmt.Fprintf(&renameOptions, "// @useAliasesForRename: %v\n", preferences.UseAliasesForRename.IsTrue())
16161616
}
1617-
if preferences.QuotePreference != nil {
1618-
fmt.Fprintf(&renameOptions, "// @quotePreference: %v\n", *preferences.QuotePreference)
1617+
if preferences.QuotePreference != ls.QuotePreferenceUnknown {
1618+
fmt.Fprintf(&renameOptions, "// @quotePreference: %v\n", preferences.QuotePreference)
16191619
}
16201620
}
16211621

internal/fourslash/tests/autoImportCompletion_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/fourslash"
78
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
89
"github.com/microsoft/typescript-go/internal/ls"
@@ -26,8 +27,8 @@ a/**/
2627
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
2728
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
2829
UserPreferences: &ls.UserPreferences{
29-
IncludeCompletionsForModuleExports: PtrTo(true),
30-
IncludeCompletionsForImportStatements: PtrTo(true),
30+
IncludeCompletionsForModuleExports: core.TSTrue,
31+
IncludeCompletionsForImportStatements: core.TSTrue,
3132
},
3233
IsIncomplete: false,
3334
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
@@ -56,8 +57,8 @@ a/**/
5657
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
5758
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
5859
UserPreferences: &ls.UserPreferences{
59-
IncludeCompletionsForModuleExports: PtrTo(true),
60-
IncludeCompletionsForImportStatements: PtrTo(true),
60+
IncludeCompletionsForModuleExports: core.TSTrue,
61+
IncludeCompletionsForImportStatements: core.TSTrue,
6162
},
6263
IsIncomplete: false,
6364
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
@@ -87,8 +88,8 @@ b/**/
8788
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
8889
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
8990
UserPreferences: &ls.UserPreferences{
90-
IncludeCompletionsForModuleExports: PtrTo(true),
91-
IncludeCompletionsForImportStatements: PtrTo(true),
91+
IncludeCompletionsForModuleExports: core.TSTrue,
92+
IncludeCompletionsForImportStatements: core.TSTrue,
9293
},
9394
IsIncomplete: false,
9495
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{

internal/fourslash/tests/gen/renameExportSpecifier2_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/fourslash"
7-
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
88
"github.com/microsoft/typescript-go/internal/ls"
99
"github.com/microsoft/typescript-go/internal/testutil"
1010
)
@@ -20,5 +20,5 @@ export { name/**/ };
2020
import { name } from './a';
2121
const x = name.toString();`
2222
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
23-
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(false)}, "")
23+
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSFalse}, "")
2424
}

internal/fourslash/tests/gen/renameExportSpecifier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/fourslash"
7-
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
88
"github.com/microsoft/typescript-go/internal/ls"
99
"github.com/microsoft/typescript-go/internal/testutil"
1010
)
@@ -20,5 +20,5 @@ export { name as name/**/ };
2020
import { name } from './a';
2121
const x = name.toString();`
2222
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
23-
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(false)}, "")
23+
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSFalse}, "")
2424
}

internal/fourslash/tests/gen/renameModuleExportsProperties1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/fourslash"
7-
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
88
"github.com/microsoft/typescript-go/internal/ls"
99
"github.com/microsoft/typescript-go/internal/testutil"
1010
)
@@ -16,5 +16,5 @@ func TestRenameModuleExportsProperties1(t *testing.T) {
1616
const content = `[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
1717
module.exports = { [|A|] }`
1818
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
19-
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, f.Ranges()[1], f.Ranges()[2])
19+
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, f.Ranges()[1], f.Ranges()[2])
2020
}

internal/fourslash/tests/gen/renameModuleExportsProperties3_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/fourslash"
7-
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
88
"github.com/microsoft/typescript-go/internal/ls"
99
"github.com/microsoft/typescript-go/internal/testutil"
1010
)
@@ -18,5 +18,5 @@ func TestRenameModuleExportsProperties3(t *testing.T) {
1818
[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
1919
module.exports = { [|A|] }`
2020
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
21-
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, f.Ranges()[1], f.Ranges()[2])
21+
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, f.Ranges()[1], f.Ranges()[2])
2222
}

internal/fourslash/tests/gen/renameNamedImport_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/fourslash"
7-
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
88
"github.com/microsoft/typescript-go/internal/ls"
99
"github.com/microsoft/typescript-go/internal/testutil"
1010
)
@@ -28,5 +28,5 @@ someExportedVariable;
2828
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
2929
f.GoToFile(t, "/home/src/workspaces/project/lib/index.ts")
3030
f.GoToFile(t, "/home/src/workspaces/project/src/index.ts")
31-
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, "i")
31+
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, "i")
3232
}

internal/fourslash/tests/gen/renameNumericalIndexSingleQuoted_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/microsoft/typescript-go/internal/fourslash"
7-
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
87
"github.com/microsoft/typescript-go/internal/ls"
98
"github.com/microsoft/typescript-go/internal/testutil"
109
)
@@ -16,5 +15,5 @@ func TestRenameNumericalIndexSingleQuoted(t *testing.T) {
1615
const content = `const foo = { [|0|]: true };
1716
foo[[|0|]];`
1817
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
19-
f.VerifyBaselineRenameAtRangesWithText(t, &ls.UserPreferences{QuotePreference: PtrTo(ls.QuotePreference("single"))}, "0")
18+
f.VerifyBaselineRenameAtRangesWithText(t, &ls.UserPreferences{QuotePreference: ls.QuotePreference("single")}, "0")
2019
}

0 commit comments

Comments
 (0)