Skip to content

Bug report: no error when using @const variable before initialization #8518

Open
@brandonmcconnell

Description

@brandonmcconnell

Describe the bug

I was trying to include a space after a comma in an if-statement. Consider this repro:

<script>
  const length = 10;
</script>

{#each { length } as _, i}
  {@const nth = i + 1}
  {nth}{#if nth !== length}, {/if}
{/each}

REPL

I did want the space to be a breaking space, so &nbsp; wouldn't work here. I tried &#32; which I believe is the equivalent code for a traditional breaking space, but that did not work either. (e.g. {nth}{#if nth !== length},&#32;{/if}).

Finally, I did something accidentally and reordered the two lines inside the enclosing {#each} block, only to find that change did achieve what I was trying to do even without a space or special char, but I feel like it shouldn't since the variable is now declared after it's used.

<script>
  const length = 10;
</script>

{#each { length } as _, i}
  {nth}{#if nth !== length},{/if}
  {@const nth = i + 1}
{/each}

REPL

Reproduction

REPL links above

Logs

No response

System Info

macOS v13.1 (22C65)
Svelte v3.58.0 (in REPL)

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompilerChanges relating to the compiler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions