-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Using @const with shadowed variable compiles incorrect code #7423
Comments
To anyone else running into this, one workaround for this issue is simply moving the executing code out: <script>
const errors = [{ legId: 'L1' }]
const legs = [{ id: 'L1' }]
const finder = error => rel => rel.id === error?.legId
</script>
<ul>
{#each errors as error}
{@const legId = legs.findIndex(finder(error))}
<li>
Leg ID: {legId}
</li>
{/each}
</ul> |
It doesn't seem that optional chaining is tripping the compiler. It's because in <ul>
{#each errors as error}
{@const myLeg = legs.findIndex(rel => rel.id === error?.legId)}
<li>
Leg ID: {myLeg}
</li>
{/each}
</ul> |
dummdidumm
changed the title
Using @const with Optional Chaining compiles incorrect code
Using @const with shadowed variable compiles incorrect code
Apr 7, 2022
Duplicate of #7206 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
If I use Optional Chaining with
findIndex
as part of@const
the output code is incorrect. See REPL for a reproduction.What I did
The Svelte line is:
{@const legId = legs.findIndex(rel => rel.id === error?.legId)}
What happens
The compiled relevant line of JS output is:
In particular the part
/*error*/ child_ctx[2]?./*legId*/ child_ctx[3]
This throws the error
child_ctx[2].child_ctx is undefined
.What I expect to happen
The compiled line of JS output, for that
findIndex
function, should be:Reproduction
https://svelte.dev/repl/7e3c2e5fe9f14634a2bc4cbdfeffb615?version=3.46.6
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: