svelte: Property syntax using curly braces and quotes is inconsistent.
Describe the bug
There is this special syntax prop="{value}" that does not cause string interpolation but instead just passes the value of the expression as is. This is inconsistent with how properties and string interpolation behave in all other cases.
As I see it, as soon as quotes are used, the value should be an interpolated string, no magic. Obviously, this would be a significant breaking change, so this is more of a suggestion for the future.
Reproduction
<Child value="42" /> <!-- string -->
<Child value={42} /> <!-- number -->
<Child value="{42}" /> <!-- (magically) number -->
<Child value="{42} " /> <!-- string -->
Logs
No response
System Info
REPL (Svelte Version 3.51.0)
Windows 10, 64bit
Chrome 106.0.5249.103
Severity
annoyance
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 26
- Comments: 25 (19 by maintainers)
Commits related to this issue
- feat: never quote single attribute expressions in Svelte 5 In Svelte 5, attributes are never quoted, because this will mean "stringify this attribute value" in a future Svelte version Related to htt... — committed to sveltejs/prettier-plugin-svelte by dummdidumm 2 years ago
- feat: never quote single attribute expressions in Svelte 5 (#451) In Svelte 5, attributes are never quoted, because this will mean "stringify this attribute value" in a future Svelte version Relat... — committed to sveltejs/prettier-plugin-svelte by dummdidumm 2 years ago
- breaking: warn on quotes single-expression attributes in runes mode In a future version, that will mean things are getting stringified, which is a departure from how things work today, therefore a wa... — committed to sveltejs/svelte by dummdidumm 2 years ago
- breaking: warn on quotes single-expression attributes in runes mode (#12479) * parse `foo={bar}` attribute as `value: { type: 'ExpressionTag', .. }` (i.e. don't wrap in an array) * warn on quoted... — committed to sveltejs/svelte by dummdidumm 2 years ago
Here’s what I think we should do: in dev mode, print a warning if a component prop or element
on[x]attribute is quoted and the value is a non-string. Something like this:(Challenge: indicating where in the source code the offending prop was.)
If we do that, we can fix this weirdo behaviour in Svelte 6.
The warning is in place for components and custom elements now, moving this to 6.0 so we think about the next steps for that major when it comes to that.
This dates back a long time ago (I think Svelte 1?) where people would generally use HTML syntax highlighting when editing Svelte components, so we encouraged people to use
foo="{bar}"rather thanfoo={bar}so that their file would be highlighted correctly. I wouldn’t be against changing this in Svelte 4.It’s not added there because for
onclickandbind:filesthere’s only one possible kind of value, so it doesn’t matter, whereas for attributes stringified or not makes a difference. But, we’ll possible fade these out over time, too (warning, then at some point maybe even error) - depends on how good the other transitions works out.How is that a counter argument? This is just string interpolation in exactly the way I expect it to work. (Same as in element text content.)
Revisiting this, I still don’t think we should change this behavior. It’s arguably an edge case and AFAIK some people still use the prettier plugin’s HTML strict mode due to editor limitations which means quotes are always applied. I’m in favor of kicking this can down the road further.
Either add some types or configure your prettier/linter to format or yell at you.
I’m sorry, forget my comment, didn’t read thoroughly enough. Note to self: this would also need adjustment in prettier-plugin-svelte
Recently a
warningFiltercompiler option was added which could help:Though the preprocessor probably needs to be adjusted if it currently always outputs the quotes 🤔
A counter argument to changing this is that you can do this today:
class="{foo} some string"