Skip to content

Commit f51424e

Browse files
authored
(fix) remove wrong import type (#723)
By checking if import happens outside of script tags. If so, always remove it #687
1 parent d3662e3 commit f51424e

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
VersionedTextDocumentIdentifier,
99
WorkspaceEdit
1010
} from 'vscode-languageserver';
11-
import { Document, mapRangeToOriginal, isRangeInTag } from '../../../lib/documents';
11+
import { Document, mapRangeToOriginal, isRangeInTag, isInTag } from '../../../lib/documents';
1212
import { pathToUrl, flatten } from '../../../utils';
1313
import { CodeActionsProvider } from '../../interfaces';
1414
import { SnapshotFragment, SvelteSnapshotFragment } from '../DocumentSnapshot';
@@ -141,7 +141,9 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
141141
document,
142142
doc,
143143
edit,
144-
true
144+
true,
145+
isInTag(range.start, document.scriptInfo) ||
146+
isInTag(range.start, document.moduleScriptInfo)
145147
);
146148
}
147149
return TextEdit.replace(

packages/language-server/src/plugins/typescript/features/CompletionProvider.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,14 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
307307
for (const action of actions) {
308308
for (const change of action.changes) {
309309
edit.push(
310-
...this.codeActionChangesToTextEdit(document, fragment, change, isImport)
310+
...this.codeActionChangesToTextEdit(
311+
document,
312+
fragment,
313+
change,
314+
isImport,
315+
isInTag(comp.position, document.scriptInfo) ||
316+
isInTag(comp.position, document.moduleScriptInfo)
317+
)
311318
);
312319
}
313320
}
@@ -342,20 +349,28 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
342349
doc: Document,
343350
fragment: SvelteSnapshotFragment,
344351
changes: ts.FileTextChanges,
345-
isImport: boolean
352+
isImport: boolean,
353+
actionTriggeredInScript: boolean
346354
): TextEdit[] {
347355
return changes.textChanges.map((change) =>
348-
this.codeActionChangeToTextEdit(doc, fragment, change, isImport)
356+
this.codeActionChangeToTextEdit(
357+
doc,
358+
fragment,
359+
change,
360+
isImport,
361+
actionTriggeredInScript
362+
)
349363
);
350364
}
351365

352366
codeActionChangeToTextEdit(
353367
doc: Document,
354368
fragment: SvelteSnapshotFragment,
355369
change: ts.TextChange,
356-
isImport: boolean
370+
isImport: boolean,
371+
actionTriggeredInScript: boolean
357372
): TextEdit {
358-
change.newText = this.changeSvelteComponentImport(change.newText);
373+
change.newText = this.changeComponentImport(change.newText, actionTriggeredInScript);
359374

360375
const scriptTagInfo = fragment.scriptInfo;
361376
if (!scriptTagInfo) {
@@ -427,10 +442,11 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
427442
return name.replace(/(\w+)__SvelteComponent_/, '$1');
428443
}
429444

430-
private changeSvelteComponentImport(importText: string) {
445+
private changeComponentImport(importText: string, actionTriggeredInScript: boolean) {
431446
const changedName = this.changeSvelteComponentName(importText);
432-
if (importText !== changedName) {
433-
// For some reason, TS sometimes adds the `type` modifier. Remove it.
447+
if (importText !== changedName || !actionTriggeredInScript) {
448+
// For some reason, TS sometimes adds the `type` modifier. Remove it
449+
// in case of Svelte component imports or if import triggered from markup.
434450
return changedName.replace(' type ', ' ');
435451
}
436452

0 commit comments

Comments
 (0)