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

Svelte 5: unable to name a variable with the "same name" as a rune #12328

Open
WarningImHack3r opened this issue Jul 7, 2024 · 14 comments
Open
Milestone

Comments

@WarningImHack3r
Copy link

WarningImHack3r commented Jul 7, 2024

Describe the bug

When naming a variable with the same name as a rune, without the $, like state or derived or others, the compiler considers them as the same, thus leading to errors like Cannot access 'state' before initialization or errors relating to rune re-affectation.

Even if it's not a bug, runes re-affectation should be prohibited.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACj2N0QqDMAxFfyUEH9wQHHsaVQf7jrkH10ZWqG1pU2GI_z46N99yT3JPFhy1oYjivqAdJkKBN--xQn77HOJMhgkrjC4FmUm7IeE8a2cjhGQpQn3tbW_bKIP2nGc2xCBdsgwdFJEHpvJ0aPJGOhsZvgg6uGS2U0VBz6Sggx5fZIzrsfnblEtPkyvF76rc_Ec4Z3Fb79-xwskpPWpSKDgkWh_rB8K78zLqAAAA

Logs

No response

System Info

REPL, Svelte 5 next 175

Severity

annoyance

@trueadm trueadm added the bug label Jul 7, 2024
@trueadm trueadm self-assigned this Jul 7, 2024
@trueadm trueadm added this to the 5.0 milestone Jul 7, 2024
@7nik
Copy link

7nik commented Jul 7, 2024

It's not really about runes but stores — if you declare a variable state, then $state is considered a store value, and thus, the error is that you are trying to read a store before its declaration. So, in general, the error is correct, though it can be misleading.

@WarningImHack3r
Copy link
Author

WarningImHack3r commented Jul 7, 2024

@7nik yes indeed, makes sense! I'm quite new to using runes, so I kind of forgot about stores as I was focused on playing around with runes, but yeah, that makes sense.

In my opinion, there are two possible solutions here:

  • Allow naming variables with the same name as runes, but prohibit creating stores with those names (seems quite hard with JS versatility)
  • Completely disallow variable creation with those names

@MotionlessTrain
Copy link
Contributor

Both are not easily feasible:

  • when you have code like let state = getSomeResultFromSomewhereElse(), it is not feasible for svelte to detect what type(s) that could be. That would make this a runtime warning, which is not ideal at all
  • Forbidding variables with the same names as runes would make adding new runes a breaking change (as more variable names are forbidden, which could break code).

Afaik because of those reasons, runes with the same names as variables are automatically disabled

@7nik
Copy link

7nik commented Jul 7, 2024

The first solution is impossible. Static analysis cannot cover all possible cases.
IIRC, variables shadowing runes is intention behaviour - this allows the introduction of new runes without breaking existing code. But a warning should probably be emitted in this case.
BTW, due to this, your REPL is considered to be in legacy mode.

@WarningImHack3r
Copy link
Author

BTW, due to this, your REPL is considered to be in legacy mode.

What does that mean?

  • Forbidding variables with the same names as runes would make adding new runes a breaking change (as more variable names are forbidden, which could break code).

That's true... What's the best way around it then? A simply better warning message?

@trueadm trueadm removed their assignment Jul 7, 2024
@7nik
Copy link

7nik commented Jul 8, 2024

BTW, due to this, your REPL is considered to be in legacy mode.

What does that mean?

Svelte 4 / non-rune mode.
The mode can be switched with <svelte:options runes /> or runes: true in the compiler options.

@WarningImHack3r
Copy link
Author

Svelte 4 / non-rune mode.

Weird, it was in Svelte 5 when I posted it. Fixed.

@trueadm trueadm self-assigned this Jul 8, 2024
@trueadm
Copy link
Contributor

trueadm commented Jul 8, 2024

There is already a warning for this problem, as you can see in the REPL.

@WarningImHack3r
Copy link
Author

The current warning implies overriding runes is allowed, not that such a thing is not possible and the variable should be renamed

@trueadm
Copy link
Contributor

trueadm commented Jul 8, 2024

The current warning implies overriding runes is allowed, not that such a thing is not possible and the variable should be renamed

I didn't interpret the warning to imply that you could override runes, more that the naming of your variable conflicts with a rune in scope, so rather than acting like a rune, it will act like a store. So renaming the variable avoids it from acting like a store.

@trueadm trueadm removed the bug label Jul 8, 2024
@trueadm trueadm removed their assignment Jul 8, 2024
@dummdidumm
Copy link
Member

This works as expected. We decided to give stores precedence over runes in this situation.
We can revisit this decision in Svelte 6.

@dummdidumm dummdidumm modified the milestones: 5.0, 6.0 Jul 8, 2024
@Conduitry
Copy link
Member

It looks like this was a duplicate of #12218. I don't know which one we want to keep.

@dummdidumm
Copy link
Member

That issue sounds different to me - I understand it as a request to not reserve the $ prefix

@WarningImHack3r
Copy link
Author

WarningImHack3r commented Jul 13, 2024

@Conduitry I agree with Simon: my request was to better handle/warn about using "rune-named" variables, whereas the other issue is about allowing $-prefixed variables (which I didn't know was not allowed until now - don't such variables even conflict with stores?).

However, both issues would be solved by disabling some/all impacted runes when a conflicting name is found.

Out of curiosity, how did you come up with this idea of prefixing runes with $? I guess it might have something to do with how reactivity used to work in Svelte, but conflicts with stores and regular $-prefixed variables were inevitable, weren't they?
On the other hand, I can't think of a better way to differentiate runes. Maybe going the Solid way, moving the dollar sign at the end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants