language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

False positive snippet usage before assignment errors

Open brunnerh opened this issue 1 year ago • 1 comments

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 avatar Jun 25 '24 10:06 brunnerh

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

dummdidumm avatar Jun 25 '24 11:06 dummdidumm

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()}

jasonlyu123 avatar Jul 23 '24 09:07 jasonlyu123