fix: Keep inlined JSDoc comments in property conversion of svelte-migrate ()

* 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
pull/15551/head
Robert Gieseke 2 weeks ago committed by GitHub
parent 6915c12b58
commit 1a5fb8fd51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
Keep inlined trailing JSDoc comments of properties when running svelte-migrate

@ -1592,7 +1592,6 @@ function extract_type_and_comment(declarator, state, path) {
const comment_start = /** @type {any} */ (comment_node)?.start;
const comment_end = /** @type {any} */ (comment_node)?.end;
let comment = comment_node && str.original.substring(comment_start, comment_end);
if (comment_node) {
str.update(comment_start, comment_end, '');
}
@ -1673,6 +1672,11 @@ function extract_type_and_comment(declarator, state, path) {
state.has_type_or_fallback = true;
const match = /@type {(.+)}/.exec(comment_node.value);
if (match) {
// try to find JSDoc comments after a hyphen `-`
const jsdocComment = /@type {.+} (?:\w+|\[.*?\]) - (.+)/.exec(comment_node.value);
if (jsdocComment) {
cleaned_comment += jsdocComment[1]?.trim();
}
return {
type: match[1],
comment: cleaned_comment,
@ -1693,7 +1697,6 @@ function extract_type_and_comment(declarator, state, path) {
};
}
}
return {
type: 'any',
comment: state.uses_ts ? comment : cleaned_comment,

@ -21,6 +21,9 @@
*/
export let type_no_comment;
/** @type {boolean} type_with_comment - One-line declaration with comment */
export let type_with_comment;
/**
* This is optional
*/
@ -40,4 +43,10 @@
export let inline_multiline_trailing_comment = 'world'; /*
* this is a same-line trailing multiline comment
**/
/** @type {number} [default_value=1] */
export let default_value = 1;
/** @type {number} [comment_default_value=1] - This has a comment and an optional value. */
export let comment_default_value = 1;
</script>

@ -9,12 +9,18 @@
/**
* @typedef {Object} Props
* @property {string} comment - My wonderful comment
@ -22,11 +28,14 @@
* @property {any} one_line - one line comment
* @property {any} no_comment
* @property {boolean} type_no_comment
* @property {boolean} type_with_comment - One-line declaration with comment
* @property {any} [optional] - This is optional
* @property {any} inline_commented - this should stay a comment
* @property {any} inline_commented_merged - This comment should be merged - with this inline comment
* @property {string} [inline_multiline_leading_comment] - this is a same-line leading multiline comment
* @property {string} [inline_multiline_trailing_comment] - this is a same-line trailing multiline comment
* @property {number} [default_value]
* @property {number} [comment_default_value] - This has a comment and an optional value.
*/
/** @type {Props} */
@ -36,10 +45,13 @@
one_line,
no_comment,
type_no_comment,
type_with_comment,
optional = {stuff: true},
inline_commented,
inline_commented_merged,
inline_multiline_leading_comment = 'world',
inline_multiline_trailing_comment = 'world'
inline_multiline_trailing_comment = 'world',
default_value = 1,
comment_default_value = 1
} = $props();
</script>
Loading…
Cancel
Save