Skip to content
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

False positive snippet usage before assignment errors #2417

Closed
brunnerh opened this issue Jun 25, 2024 · 2 comments
Closed

False positive snippet usage before assignment errors #2417

brunnerh opened this issue Jun 25, 2024 · 2 comments
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@brunnerh
Copy link
Member

Describe the bug

Snippets are available anywhere in their scope.
The language tools falsely surface errors if the order of definition and usage is reversed on the same level.

Reproduction

<script>
    const address = null;
</script>

<div>
	{@render (address ?? defaultAddress)()}
	{#snippet defaultAddress()}Unknown address{/snippet}
</div>

Variable 'defaultAddress' is used before being assigned. js(2454)

screenshot of error

Expected behaviour

No error.

System Info

  • OS: Win 10
  • IDE: VSCode
  • Extension version: v108.5.2

Which package is the issue about?

No response

Additional Information, eg. Screenshots

No response

@brunnerh brunnerh added the bug Something isn't working label Jun 25, 2024
@dummdidumm
Copy link
Member

Top level snippets are hoisted to the top, guess we need to do that for nested snippets, too (top of their scope)

@jasonlyu123
Copy link
Member

Leaving here as a record. I thought it would be easier to just transform it into a function declaration but snippets are actually block-scoped so the following won't be flagged as an error if it was transformed to a var or a function declaration but would result in a runtime error. This might also mean, at least nested one, const is more correct than the current var.

<div>
  {#snippet hi()}{/snippet}
</div>

{@render hi()}

@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Jul 30, 2024
dummdidumm pushed a commit that referenced this issue Jul 30, 2024
#2444 #2417 #2425 #2385 (comment)

The snippet is now transformed into a function with the return type so that The default parameter type can be inferred:

`const foo = (a: string, b = 1): ReturnType<import('svelte').Snippet> => {}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

3 participants