-
-
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
Template syntax for each breaks with reserved words as single-item variables #934
Comments
This is a tricky, err... case. The problem is that acorn (which parses JS on Svelte's behalf) won't parse It's not something particular to Svelte — this would be equally impossible: cases.forEach(case => {
console.log(case.title);
}); We might just have to accept that it isn't possible to have reserved words in template expressions, sadly! Unless anyone has any bright ideas? |
Seems fine as is, a regular JS annoyance. Maybe if you wanted to be extra nice then in dev mode it could give a warning if any template identifier is a reversed keyword? |
I must have a fundamental misunderstanding of how svelte compiles templates, because I assumed that it would be trivial for it to rename those values inside of a template. When using template tags like svelte does I tend not to think of it as "just javascript" any more, so I definitely fell into the same trap that @Morklympious did and assumed that would be fine at first. |
You can't just do a string replace (because you might have something like I think @aubergene is onto the right idea — Svelte should just throw an error at compile-time that says 'you can't use |
Yeah, I get why acorn doesn't like that name. I assumed that svelte used a custom parser for the special bits of template syntax (since It's not a big deal, ultimately, and I agree that even just slightly tweaked error messaging would certainly solve most of the problem. |
After some more thinking I get the issue, I wasn't thinking about later uses of Sorry for the flopping around, had to learn some more svelte before the truth dawned on me. |
I think @aubergene's "Throw an error" is an acceptable solution, if only to stop people from falling down the rabbit hole of "code, what are you DOING?!". |
This is implemented in 1.43: |
* Render full posts on RSS feed * fix tab
I ran into an issue where an
{{#each}}
in my templating code wasn't outputting values I was passing through an array.My code:
The shape of my data:
Looks like since
case
is a reserved word in javascript, I can't use it in my templating. 😢Same goes for pretty much any other reserved word, You can remove the last letter in any of the REPL examples below and see it actually render properly.
REPL repro with "case"
REPL repro with "class"
REPL repro with "default"
The text was updated successfully, but these errors were encountered: