Skip to content

Commit ea82bb3

Browse files
committed
fix: better migration for leading and trailing comments
1 parent f579a3b commit ea82bb3

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

.changeset/popular-dolphins-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: better migration for leading and trailing comments

packages/svelte/src/compiler/migrate/index.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,25 @@ const instance_script = {
549549
labeled_statement &&
550550
(labeled_has_single_assignment || is_expression_assignment)
551551
) {
552+
const indent = state.str.original.substring(
553+
state.str.original.lastIndexOf('\n', /** @type {number} */ (node.start)) + 1,
554+
/** @type {number} */ (node.start)
555+
);
556+
// transfer all the leading comments
557+
if (
558+
labeled_statement.body.type === 'BlockStatement' &&
559+
labeled_statement.body.body[0].leadingComments
560+
) {
561+
for (let comment of labeled_statement.body.body[0].leadingComments) {
562+
state.str.prependLeft(
563+
/** @type {number} */ (node.start),
564+
comment.type === 'Block'
565+
? `/*${comment.value}*/\n${indent}`
566+
: `// ${comment.value}\n${indent}`
567+
);
568+
}
569+
}
570+
552571
// Someone wrote a `$: { ... }` statement which we can turn into a `$derived`
553572
state.str.appendRight(
554573
/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
@@ -573,6 +592,21 @@ const instance_script = {
573592
')'
574593
);
575594
state.derived_labeled_statements.add(labeled_statement);
595+
596+
// transfer all the trailing comments
597+
if (
598+
labeled_statement.body.type === 'BlockStatement' &&
599+
labeled_statement.body.body[0].trailingComments
600+
) {
601+
for (let comment of labeled_statement.body.body[0].trailingComments) {
602+
state.str.appendRight(
603+
/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
604+
comment.type === 'Block'
605+
? `\n${indent}/*${comment.value}*/`
606+
: `\n${indent}// ${comment.value}`
607+
);
608+
}
609+
}
576610
} else {
577611
state.str.prependLeft(
578612
/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
@@ -1261,11 +1295,18 @@ function handle_events(element, state) {
12611295
* Returns start and end of the node. If the start is preceeded with white-space-only before a line break,
12621296
* the start will be the start of the line.
12631297
* @param {string} source
1264-
* @param {Node} node
1298+
* @param {LabeledStatement} node
12651299
*/
12661300
function get_node_range(source, node) {
1267-
let start = /** @type {number} */ (node.start);
1268-
let end = /** @type {number} */ (node.end);
1301+
const first_leading_comment = node.leadingComments?.[0];
1302+
const last_trailing_comment = node.trailingComments?.[node.trailingComments.length - 1];
1303+
1304+
// @ts-expect-error the type of `Comment` seems to be wrong...the node actually contains
1305+
// start and end but the type seems to only contain a `range` (which doesn't actually exists)
1306+
let start = /** @type {number} */ (first_leading_comment?.start ?? node.start);
1307+
// @ts-expect-error the type of `Comment` seems to be wrong...the node actually contains
1308+
// start and end but the type seems to only contain a `range` (which doesn't actually exists)
1309+
let end = /** @type {number} */ (last_trailing_comment?.end ?? node.end);
12691310

12701311
let idx = start;
12711312
while (source[idx - 1] !== '\n' && source[idx - 1] !== '\r') {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script>
2+
let count = 0;
3+
4+
$: {
5+
console.log({ count, double });
6+
}
7+
8+
// this comment should remain attached to this declaration after migration
9+
$: double = count * 2; // this too
10+
11+
// triple
12+
let triple;
13+
$: {
14+
// update triple
15+
triple = count * 3;
16+
// trailing comment
17+
// in triple
18+
}
19+
20+
function increment() {
21+
count += 1;
22+
}
23+
</script>
24+
25+
<button onclick={increment}>
26+
clicks: {count}
27+
</button>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script>
2+
import { run } from 'svelte/legacy';
3+
4+
let count = $state(0);
5+
6+
7+
8+
// triple
9+
// update triple
10+
let triple = $derived(count * 3)
11+
// trailing comment
12+
// in triple;
13+
14+
function increment() {
15+
count += 1;
16+
}
17+
// this comment should remain attached to this declaration after migration
18+
let double = $derived(count * 2); // this too
19+
run(() => {
20+
console.log({ count, double });
21+
});
22+
23+
</script>
24+
25+
<button onclick={increment}>
26+
clicks: {count}
27+
</button>

0 commit comments

Comments
 (0)