Skip to content

Commit

Permalink
fix: ensure imports without semicolon doesn't break intellisense
Browse files Browse the repository at this point in the history
...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
  • Loading branch information
dummdidumm committed Nov 29, 2024
1 parent fda35fe commit a605dc0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1755,20 +1755,9 @@ describe('CodeActionsProvider', function () {
line: 1
}
}
},
{
newText: "import { } from './somepng.png';\n",
range: {
end: {
character: 0,
line: 4
},
start: {
character: 4,
line: 3
}
}
}
// Because the generated code adds a ; after the last import, the
// second import is not appearing in the edits here
],
textDocument: {
uri: getUri('organize-imports-leading-comment.svelte'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ export function handleFirstInstanceImport(
hasModuleScript: boolean,
str: MagicString
) {
const firstImport = tsAst.statements
.filter(ts.isImportDeclaration)
.sort((a, b) => a.end - b.end)[0];
const imports = tsAst.statements.filter(ts.isImportDeclaration).sort((a, b) => a.end - b.end);
const firstImport = imports[0];
if (!firstImport) {
return;
}
Expand All @@ -42,4 +41,12 @@ export function handleFirstInstanceImport(
: firstImport.getStart();

str.appendRight(start + astOffset, '\n' + (hasModuleScript ? '\n' : ''));

// Add a semi-colon to the last import if it doesn't have one, to prevent auto completion
// and imports from being added at the wrong position
const lastImport = imports[imports.length - 1];
const end = lastImport.end + astOffset - 1;
if (str.original[end] !== ';') {
str.overwrite(end, lastImport.end + astOffset, str.original[end] + ';\n');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
;
import { a as b } from "./test.svelte"

import * as c from "b.ts"
import * as c from "b.ts";
function render() {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
;// non-leading comment
/**@typedef {{ a: string }} Foo */

import ''
import '';
function render() {


Expand Down

0 comments on commit a605dc0

Please sign in to comment.