From b50eae1047687ea5d24ad85df87986da9356408e Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 14 Aug 2020 15:53:28 +0200 Subject: [PATCH] (fix) make organize imports work with module script (#454) #221 ``` {c} ``` ----> ``` {c} ``` --- .../features/CodeActionsProvider.ts | 5 +- .../features/CodeActionsProvider.test.ts | 75 +++++++++++++++++++ .../organize-imports-with-module.svelte | 11 +++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 packages/language-server/test/plugins/typescript/testfiles/organize-imports-with-module.svelte diff --git a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts index 815ea1c86..c9e76d44e 100644 --- a/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts @@ -74,7 +74,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider { // Handle svelte2tsx wrong import mapping: // The character after the last import maps to the start of the script // TODO find a way to fix this in svelte2tsx and then remove this - if (range.end.line === 0 && range.end.character === 1) { + if ( + (range.end.line === 0 && range.end.character === 1) || + range.end.line < range.start.line + ) { edit.span.length -= 1; range = mapRangeToOriginal(fragment, convertRange(fragment, edit.span)); range.end.character += 1; diff --git a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts index af205e761..13916f2f4 100644 --- a/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts @@ -165,6 +165,81 @@ describe('CodeActionsProvider', () => { ]); }); + it('organizes imports with module script', async () => { + const { provider, document } = setup('organize-imports-with-module.svelte'); + + const codeActions = await provider.getCodeActions( + document, + Range.create(Position.create(1, 4), Position.create(1, 5)), // irrelevant + { + diagnostics: [], + only: [CodeActionKind.SourceOrganizeImports], + }, + ); + (codeActions[0]?.edit?.documentChanges?.[0])?.edits.forEach( + (edit) => (edit.newText = harmonizeNewLines(edit.newText)), + ); + + assert.deepStrictEqual(codeActions, [ + { + edit: { + documentChanges: [ + { + edits: [ + { + // eslint-disable-next-line max-len + newText: "import A from './A';\nimport { c } from './c';\n", + range: { + start: { + line: 1, + character: 2, + }, + end: { + line: 2, + character: 0, + }, + }, + }, + { + newText: '', + range: { + start: { + line: 6, + character: 2, + }, + end: { + line: 7, + character: 2, + }, + }, + }, + { + newText: '', + range: { + start: { + line: 7, + character: 2, + }, + end: { + line: 7, + character: 22, + }, + }, + }, + ], + textDocument: { + uri: getUri('organize-imports-with-module.svelte'), + version: null, + }, + }, + ], + }, + kind: CodeActionKind.SourceOrganizeImports, + title: 'Organize Imports', + }, + ]); + }); + it('should do extract into const refactor', async () => { const { provider, document } = setup('codeactions.svelte'); diff --git a/packages/language-server/test/plugins/typescript/testfiles/organize-imports-with-module.svelte b/packages/language-server/test/plugins/typescript/testfiles/organize-imports-with-module.svelte new file mode 100644 index 000000000..b593aa6ae --- /dev/null +++ b/packages/language-server/test/plugins/typescript/testfiles/organize-imports-with-module.svelte @@ -0,0 +1,11 @@ + + + + +{c} \ No newline at end of file