-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Respect quote preference for object property completions #64359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
雪代 / Yukishiro (yksr-melt)
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
yksr-melt:fix/tsgo-object-property-quote-preference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
tsc/internal/fourslash/tests/completionsObjectPropertyName_quotePreference_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/TypeScript/tsc/internal/fourslash" | ||
| . "github.com/microsoft/TypeScript/tsc/internal/fourslash/tests/util" | ||
| "github.com/microsoft/TypeScript/tsc/internal/ls" | ||
| "github.com/microsoft/TypeScript/tsc/internal/ls/lsutil" | ||
| "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" | ||
| "github.com/microsoft/TypeScript/tsc/internal/testutil" | ||
| ) | ||
|
|
||
| // The name of an object literal property that is not an identifier is completed as a string literal, which has to | ||
| // follow the quote preference. The preference is configured for the whole session, rather than per request, | ||
| // so that resolving a completion item sees the same preference as the completion request did. | ||
| func configureQuotePreference(t *testing.T, f *fourslash.FourslashTest, quotePreference lsutil.QuotePreference) { | ||
| t.Helper() | ||
| opts := f.GetOptions() | ||
| opts.QuotePreference = quotePreference | ||
| f.Configure(t, opts) | ||
| } | ||
|
|
||
| // requiredKey is the completion of a required property, whose name is `name` (already quoted). | ||
| func requiredKey(name string) *lsproto.CompletionItem { | ||
| return &lsproto.CompletionItem{Label: name} | ||
| } | ||
|
|
||
| // optionalKey is the completion of an optional property, whose name is `name` (already quoted). | ||
| func optionalKey(name string) *lsproto.CompletionItem { | ||
| return &lsproto.CompletionItem{ | ||
| Label: name + "?", | ||
| InsertText: new(name), | ||
| FilterText: new(name), | ||
| SortText: new(string(ls.SortTextOptionalMember)), | ||
| } | ||
| } | ||
|
|
||
| func verifyObjectLiteralKeyCompletions(t *testing.T, f *fourslash.FourslashTest, items ...*lsproto.CompletionItem) { | ||
| t.Helper() | ||
| expected := make([]fourslash.CompletionsExpectedItem, len(items)) | ||
| for i, item := range items { | ||
| expected[i] = item | ||
| } | ||
| f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{ | ||
| IsIncomplete: false, | ||
| ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ | ||
| CommitCharacters: &DefaultCommitCharacters, | ||
| EditRange: Ignored, | ||
| }, | ||
| Items: &fourslash.CompletionsExpectedItems{Exact: expected}, | ||
| }) | ||
| } | ||
|
|
||
| const objectPropertyNameContent = `interface Options { | ||
| "a-b": number; | ||
| "c d"?: string; | ||
| "it's"?: number; | ||
| 'say "hi"'?: number; | ||
| plain: boolean; | ||
| } | ||
| const o: Options = { | ||
| /**/ | ||
| };` | ||
|
|
||
| func TestCompletionsObjectPropertyName_quotePreferenceSingle(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `type TableName = 't1' | 't2'; | ||
| type ColumnName = 'id' | 'value'; | ||
| type TableColumn = ` + "`${TableName}.${ColumnName}`" + `; | ||
|
|
||
| type Join = { | ||
| on: Partial<Record<TableColumn, TableColumn>>; | ||
| }; | ||
|
|
||
| const test: Join = { | ||
| on: { | ||
| /**/ | ||
| } | ||
| };` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| configureQuotePreference(t, f, lsutil.QuotePreferenceSingle) | ||
| verifyObjectLiteralKeyCompletions(t, f, | ||
| optionalKey(`'t1.id'`), | ||
| optionalKey(`'t1.value'`), | ||
| optionalKey(`'t2.id'`), | ||
| optionalKey(`'t2.value'`), | ||
| ) | ||
| } | ||
|
|
||
| func TestCompletionsObjectPropertyName_quotePreferenceSingleEscaping(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, objectPropertyNameContent) | ||
| defer done() | ||
| configureQuotePreference(t, f, lsutil.QuotePreferenceSingle) | ||
| // Resolving an item finds its symbol by name, so the detail checks that the name matches with the preference too. | ||
| spaced := optionalKey(`'c d'`) | ||
| spaced.Detail = new(`(property) Options["c d"]?: string | undefined`) | ||
| verifyObjectLiteralKeyCompletions(t, f, | ||
| requiredKey(`'a-b'`), | ||
| requiredKey(`plain`), | ||
| spaced, | ||
| optionalKey(`'it\'s'`), | ||
| optionalKey(`'say "hi"'`), | ||
| ) | ||
| } | ||
|
|
||
| func TestCompletionsObjectPropertyName_quotePreferenceDouble(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, objectPropertyNameContent) | ||
| defer done() | ||
| configureQuotePreference(t, f, lsutil.QuotePreferenceDouble) | ||
| verifyObjectLiteralKeyCompletions(t, f, | ||
| requiredKey(`"a-b"`), | ||
| requiredKey(`plain`), | ||
| optionalKey(`"c d"`), | ||
| optionalKey(`"it's"`), | ||
| optionalKey(`"say \"hi\""`), | ||
| ) | ||
| } | ||
|
|
||
| func TestCompletionsObjectPropertyName_quotePreferenceAuto(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @filename: /a.ts | ||
| export const a = 1; | ||
| // @filename: /b.ts | ||
| import { a } from './a'; | ||
|
|
||
| interface Options { | ||
| "a-b"?: number; | ||
| } | ||
| const o: Options = { | ||
| /**/ | ||
| };` | ||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| f.GoToFile(t, "/b.ts") | ||
| configureQuotePreference(t, f, lsutil.QuotePreferenceAuto) | ||
| verifyObjectLiteralKeyCompletions(t, f, optionalKey(`'a-b'`)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have we not needed these helpers so far? seems surprising to need them now when our tests are already so extensive.