Skip to content

Commit 1a5fb8f

Browse files
authored
fix: Keep inlined JSDoc comments in property conversion of svelte-migrate (#15567)
* Add failing JSDoc property svelte-migrate conversion tests * Add further test case and remove default value in JSDoc output * Look for inlined JSDoc comments after a hyphen * Add changeset
1 parent 6915c12 commit 1a5fb8f

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.changeset/happy-cameras-bow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
Keep inlined trailing JSDoc comments of properties when running svelte-migrate

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,6 @@ function extract_type_and_comment(declarator, state, path) {
15921592
const comment_start = /** @type {any} */ (comment_node)?.start;
15931593
const comment_end = /** @type {any} */ (comment_node)?.end;
15941594
let comment = comment_node && str.original.substring(comment_start, comment_end);
1595-
15961595
if (comment_node) {
15971596
str.update(comment_start, comment_end, '');
15981597
}
@@ -1673,6 +1672,11 @@ function extract_type_and_comment(declarator, state, path) {
16731672
state.has_type_or_fallback = true;
16741673
const match = /@type {(.+)}/.exec(comment_node.value);
16751674
if (match) {
1675+
// try to find JSDoc comments after a hyphen `-`
1676+
const jsdocComment = /@type {.+} (?:\w+|\[.*?\]) - (.+)/.exec(comment_node.value);
1677+
if (jsdocComment) {
1678+
cleaned_comment += jsdocComment[1]?.trim();
1679+
}
16761680
return {
16771681
type: match[1],
16781682
comment: cleaned_comment,
@@ -1693,7 +1697,6 @@ function extract_type_and_comment(declarator, state, path) {
16931697
};
16941698
}
16951699
}
1696-
16971700
return {
16981701
type: 'any',
16991702
comment: state.uses_ts ? comment : cleaned_comment,

packages/svelte/tests/migrate/samples/jsdoc-with-comments/input.svelte

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
*/
2222
export let type_no_comment;
2323
24+
/** @type {boolean} type_with_comment - One-line declaration with comment */
25+
export let type_with_comment;
26+
2427
/**
2528
* This is optional
2629
*/
@@ -40,4 +43,10 @@
4043
export let inline_multiline_trailing_comment = 'world'; /*
4144
* this is a same-line trailing multiline comment
4245
**/
46+
47+
/** @type {number} [default_value=1] */
48+
export let default_value = 1;
49+
50+
/** @type {number} [comment_default_value=1] - This has a comment and an optional value. */
51+
export let comment_default_value = 1;
4352
</script>

packages/svelte/tests/migrate/samples/jsdoc-with-comments/output.svelte

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,33 @@
99
1010
1111
12+
13+
14+
15+
1216
1317
1418
1519
20+
1621
1722
23+
1824
/**
1925
* @typedef {Object} Props
2026
* @property {string} comment - My wonderful comment
2127
* @property {number} another_comment - My wonderful other comment
2228
* @property {any} one_line - one line comment
2329
* @property {any} no_comment
2430
* @property {boolean} type_no_comment
31+
* @property {boolean} type_with_comment - One-line declaration with comment
2532
* @property {any} [optional] - This is optional
2633
* @property {any} inline_commented - this should stay a comment
2734
* @property {any} inline_commented_merged - This comment should be merged - with this inline comment
2835
* @property {string} [inline_multiline_leading_comment] - this is a same-line leading multiline comment
2936
* @property {string} [inline_multiline_trailing_comment] - this is a same-line trailing multiline comment
37+
* @property {number} [default_value]
38+
* @property {number} [comment_default_value] - This has a comment and an optional value.
3039
*/
3140
3241
/** @type {Props} */
@@ -36,10 +45,13 @@
3645
one_line,
3746
no_comment,
3847
type_no_comment,
48+
type_with_comment,
3949
optional = {stuff: true},
4050
inline_commented,
4151
inline_commented_merged,
4252
inline_multiline_leading_comment = 'world',
43-
inline_multiline_trailing_comment = 'world'
53+
inline_multiline_trailing_comment = 'world',
54+
default_value = 1,
55+
comment_default_value = 1
4456
} = $props();
4557
</script>

0 commit comments

Comments
 (0)