Skip to content

Commit 0a6fa8a

Browse files
committed
feat[sv migrate]: keep inline/trailing comments when migrating export let types to type definition
1 parent 551284c commit 0a6fa8a

File tree

1 file changed

+13
-7
lines changed
  • packages/svelte/src/compiler/migrate

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export function migrate(source, { filename, use_ts } = {}) {
279279
type = `interface ${type_name} {${newline_separator}${state.props
280280
.map((prop) => {
281281
const comment = prop.comment ? `${prop.comment}${newline_separator}` : '';
282-
return `${comment}${prop.exported}${prop.optional ? '?' : ''}: ${prop.type};`;
282+
return `${comment}${prop.exported}${prop.optional ? '?' : ''}: ${prop.type};${prop.trailing_comment ? ' //' + prop.trailing_comment : ''}`;
283283
})
284284
.join(newline_separator)}`;
285285
if (analysis.uses_props || analysis.uses_rest_props) {
@@ -289,7 +289,7 @@ export function migrate(source, { filename, use_ts } = {}) {
289289
} else {
290290
type = `/**\n${indent} * @typedef {Object} ${type_name}${state.props
291291
.map((prop) => {
292-
return `\n${indent} * @property {${prop.type}} ${prop.optional ? `[${prop.exported}]` : prop.exported}${prop.comment ? ` - ${prop.comment}` : ''}`;
292+
return `\n${indent} * @property {${prop.type}} ${prop.optional ? `[${prop.exported}]` : prop.exported}${prop.comment ? ` - ${prop.comment}` : ''}${prop.trailing_comment ? ` - ${prop.trailing_comment.trim()}` : ''}`;
293293
})
294294
.join(``)}\n${indent} */`;
295295
}
@@ -414,7 +414,7 @@ export function migrate(source, { filename, use_ts } = {}) {
414414
* analysis: ComponentAnalysis;
415415
* filename?: string;
416416
* indent: string;
417-
* props: Array<{ local: string; exported: string; init: string; bindable: boolean; slot_name?: string; optional: boolean; type: string; comment?: string; type_only?: boolean; needs_refine_type?: boolean; }>;
417+
* props: Array<{ local: string; exported: string; init: string; bindable: boolean; slot_name?: string; optional: boolean; type: string; comment?: string; trailing_comment?: string; type_only?: boolean; needs_refine_type?: boolean; }>;
418418
* props_insertion_point: number;
419419
* has_props_rune: boolean;
420420
* has_type_or_fallback: boolean;
@@ -1497,13 +1497,15 @@ function extract_type_and_comment(declarator, state, path) {
14971497
str.update(comment_start, comment_end, '');
14981498
}
14991499

1500+
const trailing_comment = /** @type {Node} */ (parent)?.trailingComments?.at(0)?.value;
1501+
15001502
if (declarator.id.typeAnnotation) {
15011503
state.has_type_or_fallback = true;
15021504
let start = declarator.id.typeAnnotation.start + 1; // skip the colon
15031505
while (str.original[start] === ' ') {
15041506
start++;
15051507
}
1506-
return { type: str.original.substring(start, declarator.id.typeAnnotation.end), comment };
1508+
return { type: str.original.substring(start, declarator.id.typeAnnotation.end), comment, trailing_comment };
15071509
}
15081510

15091511
let cleaned_comment_arr = comment
@@ -1531,7 +1533,7 @@ function extract_type_and_comment(declarator, state, path) {
15311533
state.has_type_or_fallback = true;
15321534
const match = /@type {(.+)}/.exec(comment_node.value);
15331535
if (match) {
1534-
return { type: match[1], comment: cleaned_comment };
1536+
return { type: match[1], comment: cleaned_comment, trailing_comment };
15351537
}
15361538
}
15371539

@@ -1540,11 +1542,11 @@ function extract_type_and_comment(declarator, state, path) {
15401542
state.has_type_or_fallback = true; // only assume type if it's trivial to infer - else someone would've added a type annotation
15411543
const type = typeof declarator.init.value;
15421544
if (type === 'string' || type === 'number' || type === 'boolean') {
1543-
return { type, comment: state.uses_ts ? comment : cleaned_comment };
1545+
return { type, comment: state.uses_ts ? comment : cleaned_comment, trailing_comment };
15441546
}
15451547
}
15461548

1547-
return { type: 'any', comment: state.uses_ts ? comment : cleaned_comment };
1549+
return { type: 'any', comment: state.uses_ts ? comment : cleaned_comment, trailing_comment };
15481550
}
15491551

15501552
// Ensure modifiers are applied in the same order as Svelte 4
@@ -1779,10 +1781,13 @@ function handle_identifier(node, state, path) {
17791781
comment = state.str.original.substring(comment_node.start, comment_node.end);
17801782
}
17811783

1784+
const trailing_comment = member.trailingComments?.at(0)?.value;
1785+
17821786
if (prop) {
17831787
prop.type = type;
17841788
prop.optional = member.optional;
17851789
prop.comment = comment ?? prop.comment;
1790+
prop.trailing_comment = trailing_comment ?? prop.trailing_comment;
17861791
} else {
17871792
state.props.push({
17881793
local: member.key.name,
@@ -1792,6 +1797,7 @@ function handle_identifier(node, state, path) {
17921797
optional: member.optional,
17931798
type,
17941799
comment,
1800+
trailing_comment,
17951801
type_only: true
17961802
});
17971803
}

0 commit comments

Comments
 (0)