Skip to content

fix: parsing errors for blocks and js comments #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-moons-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: parsing errors (or wrong AST) for js comments in template
21 changes: 12 additions & 9 deletions src/context/script-let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ function getNodeRange(
node.trailingComments[node.trailingComments.length - 1]
).end;
}
if (start != null && end != null) {
return [start, end];
}

if ("range" in node) {
return [start ?? node.range![0], end ?? node.range![1]];
}
const loc = getWithLoc(node);
return [start ?? loc.start, end ?? loc.end];
const loc =
"range" in node
? { start: node.range![0], end: node.range![1] }
: getWithLoc(node);

return [
start ? Math.min(start, loc.start) : loc.start,
end ? Math.max(end, loc.end) : loc.end,
];
}

type RestoreCallback = {
Expand Down Expand Up @@ -702,7 +703,9 @@ export class ScriptLetContext {
(t) => restoreCallback.start <= t.range[0]
),
};

if (startIndex.comment === -1) {
startIndex.comment = comments.length;
}
const endIndex = {
token: tokens.findIndex(
(t) => restoreCallback.end < t.range[1],
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/parser/ast/each-with-comment-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import Foo from './foo.svelte'
</script>
{#each Array(
//comment
) as i}<Foo>{i}</Foo>{/each}
Loading