Skip to content

Commit 5d3abdf

Browse files
committed
Merge branch 'main' into fix-1790
2 parents 0427468 + ad93aec commit 5d3abdf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+867
-224
lines changed

internal/api/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (api *API) GetSymbolAtPosition(ctx context.Context, projectId Handle[projec
160160
return nil, errors.New("project not found")
161161
}
162162

163-
languageService := ls.NewLanguageService(project, snapshot.Converters())
163+
languageService := ls.NewLanguageService(project.GetProgram(), snapshot)
164164
symbol, err := languageService.GetSymbolAtPosition(ctx, fileName, position)
165165
if err != nil || symbol == nil {
166166
return nil, err
@@ -202,7 +202,7 @@ func (api *API) GetSymbolAtLocation(ctx context.Context, projectId Handle[projec
202202
if node == nil {
203203
return nil, fmt.Errorf("node of kind %s not found at position %d in file %q", kind.String(), pos, sourceFile.FileName())
204204
}
205-
languageService := ls.NewLanguageService(project, snapshot.Converters())
205+
languageService := ls.NewLanguageService(project.GetProgram(), snapshot)
206206
symbol := languageService.GetSymbolAtLocation(ctx, node)
207207
if symbol == nil {
208208
return nil, nil
@@ -232,7 +232,7 @@ func (api *API) GetTypeOfSymbol(ctx context.Context, projectId Handle[project.Pr
232232
if !ok {
233233
return nil, fmt.Errorf("symbol %q not found", symbolHandle)
234234
}
235-
languageService := ls.NewLanguageService(project, snapshot.Converters())
235+
languageService := ls.NewLanguageService(project.GetProgram(), snapshot)
236236
t := languageService.GetTypeOfSymbol(ctx, symbol)
237237
if t == nil {
238238
return nil, nil

internal/checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19746,7 +19746,7 @@ func (c *Checker) checkIfExpressionRefinesParameter(fn *ast.Node, expr *ast.Node
1974619746
// "x is T" means that x is T if and only if it returns true. If it returns false then x is not T.
1974719747
// This means that if the function is called with an argument of type trueType, there can't be anything left in the `else` branch. It must reduce to `never`.
1974819748
falseCondition := &ast.FlowNode{Flags: ast.FlowFlagsFalseCondition, Node: expr, Antecedent: antecedent}
19749-
falseSubtype := c.getFlowTypeOfReferenceEx(param.Name(), initType, trueType, fn, falseCondition)
19749+
falseSubtype := c.getReducedType(c.getFlowTypeOfReferenceEx(param.Name(), initType, trueType, fn, falseCondition))
1975019750
if falseSubtype.flags&TypeFlagsNever != 0 {
1975119751
return trueType
1975219752
}

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/core/core.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ func Coalesce[T *U, U any](a T, b T) T {
362362
}
363363
}
364364

365-
func ComputeECMALineStarts(text string) []TextPos {
365+
type ECMALineStarts []TextPos
366+
367+
func ComputeECMALineStarts(text string) ECMALineStarts {
366368
result := make([]TextPos, 0, strings.Count(text, "\n")+1)
367369
return slices.AppendSeq(result, ComputeECMALineStartsSeq(text))
368370
}
@@ -648,3 +650,22 @@ func Deduplicate[T comparable](slice []T) []T {
648650
}
649651
return slice
650652
}
653+
654+
func DeduplicateSorted[T any](slice []T, isEqual func(a, b T) bool) []T {
655+
if len(slice) == 0 {
656+
return slice
657+
}
658+
last := slice[0]
659+
deduplicated := slice[:1]
660+
for i := 1; i < len(slice); i++ {
661+
next := slice[i]
662+
if isEqual(last, next) {
663+
continue
664+
}
665+
666+
deduplicated = append(deduplicated, next)
667+
last = next
668+
}
669+
670+
return deduplicated
671+
}

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
}

0 commit comments

Comments
 (0)