Skip to content

Commit 89cc22b

Browse files
authored
fix: ensure imports without semicolon doesn't break intellisense (#2610)
...by adding a generated semicolon at the end. That prevents TypeScript from going up until the next semicolon it finds, which may be from another generated code, messing up the generated->original end position #2608 #2607
1 parent fda35fe commit 89cc22b

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,20 +1755,9 @@ describe('CodeActionsProvider', function () {
17551755
line: 1
17561756
}
17571757
}
1758-
},
1759-
{
1760-
newText: "import { } from './somepng.png';\n",
1761-
range: {
1762-
end: {
1763-
character: 0,
1764-
line: 4
1765-
},
1766-
start: {
1767-
character: 4,
1768-
line: 3
1769-
}
1770-
}
17711758
}
1759+
// Because the generated code adds a ; after the last import, the
1760+
// second import is not appearing in the edits here
17721761
],
17731762
textDocument: {
17741763
uri: getUri('organize-imports-leading-comment.svelte'),

packages/svelte2tsx/src/svelte2tsx/nodes/handleImportDeclaration.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ export function handleFirstInstanceImport(
2525
hasModuleScript: boolean,
2626
str: MagicString
2727
) {
28-
const firstImport = tsAst.statements
29-
.filter(ts.isImportDeclaration)
30-
.sort((a, b) => a.end - b.end)[0];
28+
const imports = tsAst.statements.filter(ts.isImportDeclaration).sort((a, b) => a.end - b.end);
29+
const firstImport = imports[0];
3130
if (!firstImport) {
3231
return;
3332
}
@@ -42,4 +41,12 @@ export function handleFirstInstanceImport(
4241
: firstImport.getStart();
4342

4443
str.appendRight(start + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
44+
45+
// Add a semi-colon to the last import if it doesn't have one, to prevent auto completion
46+
// and imports from being added at the wrong position
47+
const lastImport = imports[imports.length - 1];
48+
const end = lastImport.end + astOffset - 1;
49+
if (str.original[end] !== ';') {
50+
str.overwrite(end, lastImport.end + astOffset, str.original[end] + ';\n');
51+
}
4552
}

packages/svelte2tsx/test/svelte2tsx/samples/imports/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;
33
import { a as b } from "./test.svelte"
44

5-
import * as c from "b.ts"
5+
import * as c from "b.ts";
66
function render() {
77

88

packages/svelte2tsx/test/svelte2tsx/samples/jsdoc-before-first-import/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;// non-leading comment
33
/**@typedef {{ a: string }} Foo */
44

5-
import ''
5+
import '';
66
function render() {
77

88

packages/svelte2tsx/test/svelte2tsx/samples/ts-runes-hoistable-props-2.v5/expectedv2.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
interface Dependency {
66
a: number;
77
b: typeof value;
8-
};;type $$ComponentProps = { a: Dependency, b: string };function render() {
8+
};
9+
;type $$ComponentProps = { a: Dependency, b: string };function render() {
910

1011

1112

packages/svelte2tsx/test/svelte2tsx/samples/ts-runes.v5/expectedv2.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
///<reference types="svelte" />
2-
;;type $$ComponentProps = { a: number, b: string };function render() {
2+
;
3+
;type $$ComponentProps = { a: number, b: string };function render() {
34

45
let { a, b }:/*Ωignore_startΩ*/$$ComponentProps/*Ωignore_endΩ*/ = $props();
56
let x = $state(0);

packages/svelte2tsx/test/svelte2tsx/samples/ts-sveltekit-autotypes-$props-rune-unchanged.v5/expectedv2.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
///<reference types="svelte" />
2-
;;type $$ComponentProps = {form: boolean, data: true };function render() {
2+
;
3+
;type $$ComponentProps = {form: boolean, data: true };function render() {
34

45
const snapshot: any = {};
56
let { form, data }:/*Ωignore_startΩ*/$$ComponentProps/*Ωignore_endΩ*/ = $props();

0 commit comments

Comments
 (0)