From 1b5e9134d40b623c88490e14d3338af94f6b552c Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 20 Feb 2024 15:10:34 +0100 Subject: [PATCH] fix: adjust `$props()` comment type logic (#2294) Look at the last comment before the node first, check for `@type` and prevent `@typedef` false positive https://github.com/sveltejs/svelte/issues/10541#issuecomment-1953620468 --- .../svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts index 4c1d28556..2a363774f 100644 --- a/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts +++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts @@ -187,18 +187,21 @@ export class ExportedNames { const text = node.getSourceFile().getFullText(); let start = -1; let comment: string; - for (const c of ts.getLeadingCommentRanges(text, node.pos) || []) { + // reverse because we want to look at the last comment before the node first + for (const c of [...(ts.getLeadingCommentRanges(text, node.pos) || [])].reverse()) { const potential_match = text.substring(c.pos, c.end); - if (potential_match.includes('@type')) { + if (/@type\b/.test(potential_match)) { comment = potential_match; start = c.pos + this.astOffset; break; } } if (!comment) { - for (const c of ts.getLeadingCommentRanges(text, node.parent.pos) || []) { + for (const c of [ + ...(ts.getLeadingCommentRanges(text, node.parent.pos) || []).reverse() + ]) { const potential_match = text.substring(c.pos, c.end); - if (potential_match.includes('@type')) { + if (/@type\b/.test(potential_match)) { comment = potential_match; start = c.pos + this.astOffset; break;