Skip to content

Commit

Permalink
fix: adjust $props() comment type logic (#2294)
Browse files Browse the repository at this point in the history
Look at the last comment before the node first, check for `@type` and prevent `@typedef` false positive
sveltejs/svelte#10541 (comment)
  • Loading branch information
dummdidumm authored Feb 20, 2024
1 parent efe8463 commit 1b5e913
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1b5e913

Please sign in to comment.