-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Request: Support Svelte JS labels #5367
Comments
I agree that Svelte is an important growing use-case to support. Thanks for raising the issue. Unfortunately, The following doesn't have a meaning yet though: label $
foo 'hi' This matches the syntax style of label $ then foo 'hi' which currently has no meaning, so that seems good. |
One of the primary reasons for adding support for new features is compatibility. Users shouldn’t need to have to choose between CoffeeScript and Svelte, for example; we added support for JSX (and before that, ES6 classes) to enable full compatibility with React. So yes, we should find some way to support this. I don’t know about this particular proposed syntax, though. Since |
Very happy that this is even being discussed. I'm working on an extension to sveltekit and my way around this (which uses backticks) is the most fragile part of my code. |
How about
Alternatively, |
The Svelte syntax is # https://svelte.dev/tutorial/reactive-declarations
:$ doubled = count * 2
# https://svelte.dev/tutorial/reactive-statements
:$ if count >= 10
alert 'count is dangerously high!'
count = 9 |
Ok-ish, because it uses characters not words. Also second example bothers me, look like if-postfix (no then). Otherwise, yeah it works and isn't too difficult to get used to. I would wait a bit more, perhaps something better comes along |
All of the possible words are listed here: Lines 1225 to 1264 in ed6733d
We can’t add any more reserved keywords without breaking changes (which basically can’t happen anymore). These are mostly spoken for, and the few that aren’t (like Anything after |
You're right. Actually, now when you put it that way, |
If we added a general An alternative syntax would be :$: doubled = count * 2
:$: if count >= 10
alert 'count is dangerously high!'
count = 9 |
Why the extra colon? As for |
The first thing you should be asking is whether your intent is to support the concept of labels in general, or if what you really want to support is reactive assignments and/or statements. Most older uses for labels were very bad (think 'goto'). However, at the end of this post, I'll show a use case that I used often in Perl. If it's reactive assignments that you want to support, then here's a syntax that I use in something I'm working on. I have no idea if it would work in CoffeeScript - just something to think about:
This means "whenever a variable in the expression following <== changes, recompute the variable 'doubled'
This means whenever any variable in the enclosed code changes, re-run the code. Personally, I rarely use the 2nd syntax since it's possible to just call a function using the 1st as long as you pass any variables that you want to trigger a re-run to that function. But then you have to define the function somewhere, so a bit less convenient. It seems like supporting this syntax, assuming it's converted directly to Now, the argument for simply supporting labels, which would be simpler to implement anyway. Here is a pattern that I've often used in Perl (this may not be valid Perl, but the intent is obvious):
Now, as written, it will skip any lines like 'END'. But if instead, I wanted to skip the rest of the current file, I would just change 'next LINE' to 'next FILE'. The point is that when I have nested loops, being able to put a label on each loop allows me to specify unambiguously which loop iteration I'm prematurely exiting. I think this is basically the only good use case I've found for labels. Well, except for supporting svelte, where it's used less as a true label, and more as a way of tagging code as being 'reactive'. |
any update ? |
I write a coffeescript label patch for svelte : https://github.com/rmw-lib/coffee-label-patch Make coffeescript support :label syntax (similar to livescript) so that it can be used for svelte. It's too hard to modify the compiler based on lex, ast, and I did this based on string substitution. This is just a crude hack, but it does work. Expect coffee to officially add this syntax. y = 0
:$ x=y*2
:$ if y>2 then x+=y else x-=y
:$
if x > y
x = y/2
else
x = y+9
x += 1
do =>
:out
for i in [1,2,3]
for j in [4,5,6]
console.log i,j
if i > 1
break out
return output : var x, y;
y = 0;
$ : x = y * 2;
$ : y > 2 ? x += y : x -= y;
$ : {
if (x > y) {
x = y / 2;
} else {
x = y + 9;
}
x += 1;
}
(() => {
var i, j, k, l, len, len1, ref, ref1;
out : {
ref = [1, 2, 3];
for (k = 0, len = ref.length; k < len; k++) {
i = ref[k];
ref1 = [4, 5, 6];
for (l = 0, len1 = ref1.length; l < len1; l++) {
j = ref1[l];
console.log(i, j);
if (i > 1) {
break out;
}
}
}
}
})(); it work , i release a preview in @rmw/svelte-preprocess
demo video : https://www.loom.com/share/a45ffe7eeecb4115ad335b7db21f9b04 the code for demo video https://github.com/rmw-lib/svelte-pug-stylus-coffee and also I create a pull request for svelte-preprocess sveltejs/svelte-preprocess#493 |
svelte 5 removes the need for the label hack with runes. |
There was no need for JS labels before, but Svelte found a good use for the syntax. Currently it can only be passed with backticks, but it's very limited and unwieldy.
All other fail
Simplest and very Coffee solution would be to spell out the syntax name, but honestly whatevs, whatever names you have left reserved or can reappropriate such as
case
outside ofswitch
.It's true this label use isn't idiomatic and that it's a solitary kinda-framework example. But, it is actually using the standard syntax, and its unidiomatic use became innovative, because unlike before, it's actually a good use. Past addition of JSX could be considered similar, and a good place for it in documentation (since it is after all a rare good example of Js labels, so it's mostly about Svelte).
In my very humble general opinion here, Coffeescript should adapt here and there because Javascript is a hackable text based language for better and for worse. Otherwise is excessively conservative and necessarily keeps the bad part of Js (Js is what it is), while losing the better part (it's hackable, here for the better). Good guideline for when is enough of offshoots should include standard syntax when it's a good use even if an unconventional one. (So-so unconvencional actually, it's not like Svelte is so unheard of.)
The text was updated successfully, but these errors were encountered: