Skip to content

fix: error in snippets with type annotations #573

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 2 commits into from
Oct 16, 2024
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/wise-mice-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: error in snippets with type annotations
15 changes: 10 additions & 5 deletions src/parser/converts/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,13 +651,18 @@ export function convertSnippetBlock(
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
};

let beforeClosingParen: ESTree.Node;
if (node.parameters.length > 0) {
const lastParam = node.parameters[
node.parameters.length - 1
] as ESTree.Pattern & { typeAnnotation?: ESTree.Node };
beforeClosingParen = lastParam.typeAnnotation ?? lastParam;
} else {
beforeClosingParen = node.expression;
}
const closeParenIndex = ctx.code.indexOf(
")",
getWithLoc(
node.parameters.length > 0
? node.parameters[node.parameters.length - 1]
: node.expression,
).end,
getWithLoc(beforeClosingParen).end,
);

const scopeKind =
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/parser/ast/svelte5/ts-snippet03-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
function hello() {
console.log('Hello');
}
</script>

{@render foo({ onclick: hello })}

{#snippet foo(o: {onclick: () => void})}
<button on:click={o.onclick}>Click me</button>
{/snippet}
Loading
Loading