From 34b2d31c4bfa88a012ff3f4dd72dc9c345b0ab93 Mon Sep 17 00:00:00 2001 From: "Lyu, Wei Da" Date: Fri, 12 Nov 2021 18:03:22 +0800 Subject: [PATCH 1/5] fix svelte component auto import when importModuleSpecifierEnding=js --- .../typescript/features/CodeActionsProvider.ts | 5 ++++- .../typescript/features/CompletionProvider.ts | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts index 8200b3e88..dfae0423a 100644 --- a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts @@ -328,6 +328,9 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { const name = node.tagName.getText(); const suffixedName = name + '__SvelteComponent_'; + const errorPreventingUserPreferences = + this.completionProvider.fixUserPreferencesForSvelteComponentImport(userPreferences); + const toFix = (c: ts.CompletionEntry) => lang .getCompletionEntryDetails( @@ -336,7 +339,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { c.name, {}, c.source, - userPreferences, + errorPreventingUserPreferences, c.data ) ?.codeActions?.map((a) => ({ diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index b23e47892..fd7dbb48a 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -392,6 +392,19 @@ export class CompletionsProviderImpl implements CompletionsProvider, @@ -409,6 +422,9 @@ export class CompletionsProviderImpl implements CompletionsProvider Date: Mon, 15 Nov 2021 09:56:04 +0800 Subject: [PATCH 2/5] test --- .../typescript/features/preferences.test.ts | 58 +++++++++++++++++++ .../testfiles/preferences/code-action.svelte | 2 + 2 files changed, 60 insertions(+) diff --git a/packages/language-server/test/plugins/typescript/features/preferences.test.ts b/packages/language-server/test/plugins/typescript/features/preferences.test.ts index e319c45a2..265a6512b 100644 --- a/packages/language-server/test/plugins/typescript/features/preferences.test.ts +++ b/packages/language-server/test/plugins/typescript/features/preferences.test.ts @@ -132,4 +132,62 @@ describe('ts user preferences', () => { const item = completions?.items.find((item) => item.label === 'definition'); assert.strictEqual(item, undefined, 'Expected no auto import suggestions'); }); + + const expectedComponentImportEdit = "import Imports from '~/imports.svelte';"; + + function setupImportModuleSpecifierEndingJs() { + const { docManager, document } = setup('code-action.svelte'); + const lsAndTsDocResolver = createLSAndTSDocResolver(docManager, { + preferences: { + importModuleSpecifier: 'non-relative', + importModuleSpecifierEnding: 'js', + quoteStyle: 'single' + } + }); + + return { document, lsAndTsDocResolver }; + } + + it('provides auto import for svelte component when importModuleSpecifierEnding is js', async () => { + const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); + + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + + const completions = await completionProvider.getCompletions( + document, + Position.create(4, 8) + ); + + const item = completions?.items.find((item) => item.label === 'Imports'); + const { additionalTextEdits } = await completionProvider.resolveCompletion(document, item!); + assert.strictEqual(additionalTextEdits![0].newText.trim(), expectedComponentImportEdit); + }); + + it('provides import code action for svelte component when importModuleSpecifierEnding is js', async () => { + const range = Range.create(Position.create(4, 1), Position.create(4, 8)); + const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); + + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + const codeActionProvider = new CodeActionsProviderImpl( + lsAndTsDocResolver, + completionProvider + ); + + const codeAction = await codeActionProvider.getCodeActions(document, range, { + diagnostics: [ + Diagnostic.create( + range, + "Cannot find name 'Imports'", + DiagnosticSeverity.Error, + 2304, + 'ts' + ) + ] + }); + + const documentChange = codeAction[0].edit?.documentChanges?.[0] as + | TextDocumentEdit + | undefined; + assert.strictEqual(documentChange?.edits[0].newText.trim(), expectedComponentImportEdit); + }); }); diff --git a/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte b/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte index 51a32446e..077fd1ec0 100644 --- a/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte +++ b/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte @@ -1,3 +1,5 @@ + + \ No newline at end of file From 51c6c2c23d11ad64980cc631f9ef0876575f7b3b Mon Sep 17 00:00:00 2001 From: "Lyu, Wei Da" Date: Tue, 16 Nov 2021 09:45:52 +0800 Subject: [PATCH 3/5] auto would still failed if there's existing --- .../src/plugins/typescript/features/CompletionProvider.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index fd7dbb48a..37c787b25 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -398,7 +398,7 @@ export class CompletionsProviderImpl implements CompletionsProvider Date: Tue, 16 Nov 2021 09:46:47 +0800 Subject: [PATCH 4/5] fix context=module export --- .../typescript/features/preferences.test.ts | 20 ++++++++++++++++++- .../testfiles/preferences/code-action.svelte | 2 -- .../preferences/module-specifier-js.svelte | 5 +++++ .../preferences/with-context-module.svelte | 3 +++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 packages/language-server/test/plugins/typescript/testfiles/preferences/module-specifier-js.svelte create mode 100644 packages/language-server/test/plugins/typescript/testfiles/preferences/with-context-module.svelte diff --git a/packages/language-server/test/plugins/typescript/features/preferences.test.ts b/packages/language-server/test/plugins/typescript/features/preferences.test.ts index 265a6512b..d6ab6c6c2 100644 --- a/packages/language-server/test/plugins/typescript/features/preferences.test.ts +++ b/packages/language-server/test/plugins/typescript/features/preferences.test.ts @@ -136,7 +136,7 @@ describe('ts user preferences', () => { const expectedComponentImportEdit = "import Imports from '~/imports.svelte';"; function setupImportModuleSpecifierEndingJs() { - const { docManager, document } = setup('code-action.svelte'); + const { docManager, document } = setup('module-specifier-js.svelte'); const lsAndTsDocResolver = createLSAndTSDocResolver(docManager, { preferences: { importModuleSpecifier: 'non-relative', @@ -163,6 +163,24 @@ describe('ts user preferences', () => { assert.strictEqual(additionalTextEdits![0].newText.trim(), expectedComponentImportEdit); }); + it('provides auto import for context="module" export when importModuleSpecifierEnding is js', async () => { + const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); + + const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver); + + const completions = await completionProvider.getCompletions( + document, + Position.create(1, 6) + ); + + const item = completions?.items.find((item) => item.label === 'hi'); + const { additionalTextEdits } = await completionProvider.resolveCompletion(document, item!); + assert.strictEqual( + additionalTextEdits![0].newText.trim(), + "import { hi } from '~/with-context-module.svelte';" + ); + }); + it('provides import code action for svelte component when importModuleSpecifierEnding is js', async () => { const range = Range.create(Position.create(4, 1), Position.create(4, 8)); const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs(); diff --git a/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte b/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte index 077fd1ec0..51a32446e 100644 --- a/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte +++ b/packages/language-server/test/plugins/typescript/testfiles/preferences/code-action.svelte @@ -1,5 +1,3 @@ - - \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/testfiles/preferences/module-specifier-js.svelte b/packages/language-server/test/plugins/typescript/testfiles/preferences/module-specifier-js.svelte new file mode 100644 index 000000000..84f1708a9 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/testfiles/preferences/module-specifier-js.svelte @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/testfiles/preferences/with-context-module.svelte b/packages/language-server/test/plugins/typescript/testfiles/preferences/with-context-module.svelte new file mode 100644 index 000000000..f58a2ed0a --- /dev/null +++ b/packages/language-server/test/plugins/typescript/testfiles/preferences/with-context-module.svelte @@ -0,0 +1,3 @@ + From c11a2bac34d74950581939dae2a18630fd0cc51a Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 16 Nov 2021 09:00:10 +0100 Subject: [PATCH 5/5] add explaining comment --- .../src/plugins/typescript/features/CompletionProvider.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index 37c787b25..01c35244a 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -392,6 +392,11 @@ export class CompletionsProviderImpl implements CompletionsProvider