Skip to content
Merged
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
4 changes: 2 additions & 2 deletions internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,8 +1207,8 @@ func (b *nodeBuilderImpl) getSpecifierForModuleSymbol(symbol *ast.Symbol, overri
contextFile,
host,
modulespecifiers.UserPreferences{
ImportModuleSpecifierPreference: specifierPref,
ImportModuleSpecifierEndingPreference: endingPref,
ImportModuleSpecifierPreference: specifierPref,
ImportModuleSpecifierEnding: endingPref,
},
modulespecifiers.ModuleSpecifierOptions{
OverrideImportMode: overrideImportMode,
Expand Down
22 changes: 18 additions & 4 deletions internal/fourslash/_scripts/convertFourslash.mts
Original file line number Diff line number Diff line change
Expand Up @@ -911,17 +911,28 @@ function parseBaselineRenameArgs(funcName: string, args: readonly ts.Expression[
}];
}

function stringToTristate(s: string): string {
switch (s) {
case "true":
return "core.TSTrue";
case "false":
return "core.TSFalse";
default:
return "core.TSUnknown";
}
}

function parseUserPreferences(arg: ts.ObjectLiteralExpression): string | undefined {
const preferences: string[] = [];
for (const prop of arg.properties) {
if (ts.isPropertyAssignment(prop)) {
switch (prop.name.getText()) {
// !!! other preferences
case "providePrefixAndSuffixTextForRename":
preferences.push(`UseAliasesForRename: PtrTo(${prop.initializer.getText()})`);
preferences.push(`UseAliasesForRename: ${stringToTristate(prop.initializer.getText())}`);
break;
case "quotePreference":
preferences.push(`QuotePreference: PtrTo(ls.QuotePreference(${prop.initializer.getText()}))`);
preferences.push(`QuotePreference: ls.QuotePreference(${prop.initializer.getText()})`);
break;
}
}
Expand Down Expand Up @@ -1465,13 +1476,16 @@ function generateGoTest(failingTests: Set<string>, test: GoTest): string {
const commands = test.commands.map(cmd => generateCmd(cmd)).join("\n");
const imports = [`"github.com/microsoft/typescript-go/internal/fourslash"`];
// Only include these imports if the commands use them to avoid unused import errors.
if (commands.includes("core.")) {
imports.unshift(`"github.com/microsoft/typescript-go/internal/core"`);
}
if (commands.includes("ls.")) {
imports.push(`"github.com/microsoft/typescript-go/internal/ls"`);
}
if (commands.includes("lsproto.")) {
imports.push(`"github.com/microsoft/typescript-go/internal/lsp/lsproto"`);
}
if (usesHelper(commands)) {
if (usesFourslashUtil(commands)) {
imports.push(`. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"`);
}
imports.push(`"github.com/microsoft/typescript-go/internal/testutil"`);
Expand All @@ -1494,7 +1508,7 @@ func Test${testName}(t *testing.T) {
return template;
}

function usesHelper(goTxt: string): boolean {
function usesFourslashUtil(goTxt: string): boolean {
for (const [_, constant] of completionConstants) {
if (goTxt.includes(constant)) {
return true;
Expand Down
8 changes: 4 additions & 4 deletions internal/fourslash/fourslash.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,11 +1611,11 @@ func (f *FourslashTest) verifyBaselineRename(

var renameOptions strings.Builder
if preferences != nil {
if preferences.UseAliasesForRename != nil {
fmt.Fprintf(&renameOptions, "// @useAliasesForRename: %v\n", *preferences.UseAliasesForRename)
if preferences.UseAliasesForRename != core.TSUnknown {
fmt.Fprintf(&renameOptions, "// @useAliasesForRename: %v\n", preferences.UseAliasesForRename.IsTrue())
}
if preferences.QuotePreference != nil {
fmt.Fprintf(&renameOptions, "// @quotePreference: %v\n", *preferences.QuotePreference)
if preferences.QuotePreference != ls.QuotePreferenceUnknown {
fmt.Fprintf(&renameOptions, "// @quotePreference: %v\n", preferences.QuotePreference)
}
}

Expand Down
13 changes: 7 additions & 6 deletions internal/fourslash/tests/autoImportCompletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
Expand All @@ -26,8 +27,8 @@ a/**/
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
UserPreferences: &ls.UserPreferences{
IncludeCompletionsForModuleExports: PtrTo(true),
IncludeCompletionsForImportStatements: PtrTo(true),
IncludeCompletionsForModuleExports: core.TSTrue,
IncludeCompletionsForImportStatements: core.TSTrue,
},
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
Expand Down Expand Up @@ -56,8 +57,8 @@ a/**/
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
UserPreferences: &ls.UserPreferences{
IncludeCompletionsForModuleExports: PtrTo(true),
IncludeCompletionsForImportStatements: PtrTo(true),
IncludeCompletionsForModuleExports: core.TSTrue,
IncludeCompletionsForImportStatements: core.TSTrue,
},
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
Expand Down Expand Up @@ -87,8 +88,8 @@ b/**/
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
UserPreferences: &ls.UserPreferences{
IncludeCompletionsForModuleExports: PtrTo(true),
IncludeCompletionsForImportStatements: PtrTo(true),
IncludeCompletionsForModuleExports: core.TSTrue,
IncludeCompletionsForImportStatements: core.TSTrue,
},
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
Expand Down
4 changes: 2 additions & 2 deletions internal/fourslash/tests/gen/renameExportSpecifier2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -20,5 +20,5 @@ export { name/**/ };
import { name } from './a';
const x = name.toString();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(false)}, "")
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSFalse}, "")
}
4 changes: 2 additions & 2 deletions internal/fourslash/tests/gen/renameExportSpecifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -20,5 +20,5 @@ export { name as name/**/ };
import { name } from './a';
const x = name.toString();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(false)}, "")
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSFalse}, "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -16,5 +16,5 @@ func TestRenameModuleExportsProperties1(t *testing.T) {
const content = `[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
module.exports = { [|A|] }`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, f.Ranges()[1], f.Ranges()[2])
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, f.Ranges()[1], f.Ranges()[2])
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -18,5 +18,5 @@ func TestRenameModuleExportsProperties3(t *testing.T) {
[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
module.exports = { [|A|] }`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, f.Ranges()[1], f.Ranges()[2])
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, f.Ranges()[1], f.Ranges()[2])
}
4 changes: 2 additions & 2 deletions internal/fourslash/tests/gen/renameNamedImport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -28,5 +28,5 @@ someExportedVariable;
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.GoToFile(t, "/home/src/workspaces/project/lib/index.ts")
f.GoToFile(t, "/home/src/workspaces/project/src/index.ts")
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, "i")
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, "i")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -16,5 +15,5 @@ func TestRenameNumericalIndexSingleQuoted(t *testing.T) {
const content = `const foo = { [|0|]: true };
foo[[|0|]];`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineRenameAtRangesWithText(t, &ls.UserPreferences{QuotePreference: PtrTo(ls.QuotePreference("single"))}, "0")
f.VerifyBaselineRenameAtRangesWithText(t, &ls.UserPreferences{QuotePreference: ls.QuotePreference("single")}, "0")
}
4 changes: 2 additions & 2 deletions internal/fourslash/tests/gen/renameRestBindingElement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fourslash_test
import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/fourslash"
. "github.com/microsoft/typescript-go/internal/fourslash/tests/util"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/testutil"
)
Expand All @@ -22,5 +22,5 @@ function foo([|{ a, ...[|{| "contextRangeIndex": 0 |}rest|] }: I|]) {
[|rest|];
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: PtrTo(true)}, f.Ranges()[1])
f.VerifyBaselineRename(t, &ls.UserPreferences{UseAliasesForRename: core.TSTrue}, f.Ranges()[1])
}
4 changes: 2 additions & 2 deletions internal/ls/autoimportfixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (ct *changeTracker) getNewImports(
// even though it's not an error, it would add unnecessary runtime emit.
topLevelTypeOnly := (defaultImport == nil || needsTypeOnly(defaultImport.addAsTypeOnly)) &&
core.Every(namedImports, func(i *Import) bool { return needsTypeOnly(i.addAsTypeOnly) }) ||
(compilerOptions.VerbatimModuleSyntax.IsTrue() || ptrIsTrue(preferences.PreferTypeOnlyAutoImports)) &&
(compilerOptions.VerbatimModuleSyntax.IsTrue() || preferences.PreferTypeOnlyAutoImports) &&
defaultImport != nil && defaultImport.addAsTypeOnly != AddAsTypeOnlyNotAllowed && !core.Some(namedImports, func(i *Import) bool { return i.addAsTypeOnly == AddAsTypeOnlyNotAllowed })

var defaultImportNode *ast.Node
Expand Down Expand Up @@ -321,5 +321,5 @@ func needsTypeOnly(addAsTypeOnly AddAsTypeOnly) bool {
}

func shouldUseTypeOnly(addAsTypeOnly AddAsTypeOnly, preferences *UserPreferences) bool {
return needsTypeOnly(addAsTypeOnly) || addAsTypeOnly != AddAsTypeOnlyNotAllowed && preferences.PreferTypeOnlyAutoImports != nil && *preferences.PreferTypeOnlyAutoImports
return needsTypeOnly(addAsTypeOnly) || addAsTypeOnly != AddAsTypeOnlyNotAllowed && preferences.PreferTypeOnlyAutoImports
}
29 changes: 11 additions & 18 deletions internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (l *LanguageService) getCompletionsAtPosition(

if triggerCharacter != nil && *triggerCharacter == " " {
// `isValidTrigger` ensures we are at `import |`
if ptrIsTrue(preferences.IncludeCompletionsForImportStatements) {
if preferences.IncludeCompletionsForImportStatements.IsTrue() {
return &lsproto.CompletionList{
IsIncomplete: true,
}
Expand Down Expand Up @@ -586,7 +586,7 @@ func (l *LanguageService) getCompletionData(
}
keywordFilters = keywordFiltersFromSyntaxKind(importStatementCompletionInfo.keywordCompletion)
}
if importStatementCompletionInfo.replacementSpan != nil && ptrIsTrue(preferences.IncludeCompletionsForImportStatements) {
if importStatementCompletionInfo.replacementSpan != nil && preferences.IncludeCompletionsForImportStatements.IsTrue() {
// !!! flags |= CompletionInfoFlags.IsImportStatementCompletion;
importStatementCompletion = &importStatementCompletionInfo
isNewIdentifierLocation = importStatementCompletionInfo.isNewIdentifierLocation
Expand Down Expand Up @@ -936,7 +936,7 @@ func (l *LanguageService) getCompletionData(
insertQuestionDot := false
if typeChecker.IsNullableType(t) {
canCorrectToQuestionDot := isRightOfDot && !isRightOfQuestionDot &&
!ptrIsFalse(preferences.IncludeAutomaticOptionalChainCompletions)
!preferences.IncludeAutomaticOptionalChainCompletions.IsFalse()
if canCorrectToQuestionDot || isRightOfQuestionDot {
t = typeChecker.GetNonNullableType(t)
if canCorrectToQuestionDot {
Expand All @@ -963,7 +963,7 @@ func (l *LanguageService) getCompletionData(
insertQuestionDot := false
if typeChecker.IsNullableType(t) {
canCorrectToQuestionDot := isRightOfDot && !isRightOfQuestionDot &&
!ptrIsFalse(preferences.IncludeAutomaticOptionalChainCompletions)
!preferences.IncludeAutomaticOptionalChainCompletions.IsFalse()

if canCorrectToQuestionDot || isRightOfQuestionDot {
t = typeChecker.GetNonNullableType(t)
Expand Down Expand Up @@ -1128,7 +1128,7 @@ func (l *LanguageService) getCompletionData(
symbols = append(symbols, filteredMembers...)

// Set sort texts.
transformObjectLiteralMembers := ptrIsTrue(preferences.IncludeCompletionsWithObjectLiteralMethodSnippets) &&
transformObjectLiteralMembers := preferences.IncludeCompletionsWithObjectLiteralMethodSnippets.IsTrue() &&
objectLikeContainer.Kind == ast.KindObjectLiteralExpression
for _, member := range filteredMembers {
symbolId := ast.GetSymbolId(member)
Expand Down Expand Up @@ -1156,7 +1156,7 @@ func (l *LanguageService) getCompletionData(
return true
}
// If not already a module, must have modules enabled.
if !ptrIsTrue(preferences.IncludeCompletionsForModuleExports) {
if !preferences.IncludeCompletionsForModuleExports.IsTrue() {
return false
}
// Always using ES modules in 6.0+
Expand Down Expand Up @@ -2240,7 +2240,7 @@ func (l *LanguageService) createCompletionItem(
}
}

if ptrIsTrue(preferences.IncludeCompletionsWithClassMemberSnippets) &&
if preferences.IncludeCompletionsWithClassMemberSnippets.IsTrue() &&
data.completionKind == CompletionKindMemberLike &&
isClassLikeMemberCompletion(symbol, data.location, file) {
// !!! class member completions
Expand All @@ -2261,13 +2261,13 @@ func (l *LanguageService) createCompletionItem(
if data.isJsxIdentifierExpected &&
!data.isRightOfOpenTag &&
clientSupportsItemSnippet(clientOptions) &&
!jsxAttributeCompletionStyleIs(preferences.JsxAttributeCompletionStyle, JsxAttributeCompletionStyleNone) &&
preferences.JsxAttributeCompletionStyle != JsxAttributeCompletionStyleNone &&
!(ast.IsJsxAttribute(data.location.Parent) && data.location.Parent.Initializer() != nil) {
useBraces := jsxAttributeCompletionStyleIs(preferences.JsxAttributeCompletionStyle, JsxAttributeCompletionStyleBraces)
useBraces := preferences.JsxAttributeCompletionStyle == JsxAttributeCompletionStyleBraces
t := typeChecker.GetTypeOfSymbolAtLocation(symbol, data.location)

// If is boolean like or undefined, don't return a snippet, we want to return just the completion.
if jsxAttributeCompletionStyleIs(preferences.JsxAttributeCompletionStyle, JsxAttributeCompletionStyleAuto) &&
if preferences.JsxAttributeCompletionStyle == JsxAttributeCompletionStyleAuto &&
!t.IsBooleanLike() &&
!(t.IsUnion() && core.Some(t.Types(), (*checker.Type).IsBooleanLike)) {
if t.IsStringLike() ||
Expand Down Expand Up @@ -2533,13 +2533,6 @@ func boolToPtr(v bool) *bool {
return nil
}

func jsxAttributeCompletionStyleIs(preferenceStyle *JsxAttributeCompletionStyle, style JsxAttributeCompletionStyle) bool {
if preferenceStyle == nil {
return false
}
return *preferenceStyle == style
}

func getLineOfPosition(file *ast.SourceFile, pos int) int {
line, _ := scanner.GetECMALineAndCharacterOfPosition(file, pos)
return line
Expand Down Expand Up @@ -5170,7 +5163,7 @@ func (l *LanguageService) getSymbolCompletionFromItemData(
}
}

completionData := l.getCompletionData(ctx, ch, file, position, &UserPreferences{IncludeCompletionsForModuleExports: ptrTo(true), IncludeCompletionsForImportStatements: ptrTo(true)})
completionData := l.getCompletionData(ctx, ch, file, position, &UserPreferences{IncludeCompletionsForModuleExports: core.TSTrue, IncludeCompletionsForImportStatements: core.TSTrue})
if completionData == nil {
return detailsData{}
}
Expand Down
Loading