Skip to content

Commit 03971d7

Browse files
authored
fix: parsing errors for blocks and js comments (#262)
* fix: parsing errors for blocks and js comments * Create rotten-moons-glow.md * add test
1 parent 0f90b6c commit 03971d7

16 files changed

+3042
-9
lines changed

.changeset/rotten-moons-glow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: parsing errors (or wrong AST) for js comments in template

src/context/script-let.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,16 @@ function getNodeRange(
8888
node.trailingComments[node.trailingComments.length - 1]
8989
).end;
9090
}
91-
if (start != null && end != null) {
92-
return [start, end];
93-
}
9491

95-
if ("range" in node) {
96-
return [start ?? node.range![0], end ?? node.range![1]];
97-
}
98-
const loc = getWithLoc(node);
99-
return [start ?? loc.start, end ?? loc.end];
92+
const loc =
93+
"range" in node
94+
? { start: node.range![0], end: node.range![1] }
95+
: getWithLoc(node);
96+
97+
return [
98+
start ? Math.min(start, loc.start) : loc.start,
99+
end ? Math.max(end, loc.end) : loc.end,
100+
];
100101
}
101102

102103
type RestoreCallback = {
@@ -702,7 +703,9 @@ export class ScriptLetContext {
702703
(t) => restoreCallback.start <= t.range[0]
703704
),
704705
};
705-
706+
if (startIndex.comment === -1) {
707+
startIndex.comment = comments.length;
708+
}
706709
const endIndex = {
707710
token: tokens.findIndex(
708711
(t) => restoreCallback.end < t.range[1],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import Foo from './foo.svelte'
3+
</script>
4+
{#each Array(
5+
//comment
6+
) as i}<Foo>{i}</Foo>{/each}

0 commit comments

Comments
 (0)