-
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
New escape javascript (`) token #1504
Comments
God, that sounds disgusting. edit: So, what's the use case here? Large chunks of javascript embedded in coffeescript? Who is doing that? |
No one is writing large chunks of javascript embedded in coffeescript, but people are writing/using tools like Sprockets that need to be able to add javascript to coffeescript files and have it compile correctly. |
Can you give some example code that would use these backtick heredocs?
I can only assume these would be used for something like that. As in, a horrible abuse of a feature that's already highly discouraged as it is. edit: Hopefully this doesn't derail the conversion, but I don't understand a tool like Sprockets. It "turns your messy javascript into clean modules", but I already write clean (for some definitions of clean) modules, what are these "messy javascript" files with paperclips and thumbtacks? And then, amazingly, it produces a single file. The same tool has been around for 40 years now: |
I think you're missing the point.. Yes, the example you posted is an awful abuse of the feature. That example should not be used even if there is only one backtick for escaping javascript. Hopefully this will clear it up: You start with something like this in your coffeescript file:
Something like Sprockets reads the require goes in and bundles the assets into a build.js that looks like this:
Make sense? Edit: Okay. This is not a discussion of Sprockets. The whole point of this issue is to point out that the single backtick does not always work the way it's intended. Use cases and outside discussion should be irrelevant here. Currently, the backtick does not work if you try to escape a file like backbone or jquery or even a two line javascript file that has a comment that includes a backtick. |
So what you're saying is
edit: By the way, I'm purposely simplifying your proposal to better understand the most basic reason for its necessity. Don't take it personally. At this point, it looks like this is yet another macro proposal. edit 2: It looks like we don't properly support backslash-escaping backticks:
|
Usefulness aside, I think it would be great to support "here-embedded-js" with triple backticks. It makes them just a bit more consistent with all the other quoting operators in the language. |
Note that ``` can only appear in literals in JS, and ``` works fine in both string and regex:
Comments do suffer, but aren't essential. |
I guess the whole point is that it breaks when there are any backticks in the javascript. You can have perfectly valid javascript that contains a backtick in a literal string or in regex and, as you said, it will break. No this isn't a huge deal, but as @jashkenas mentioned, it makes it more consistent with the language and it fixes these minor issues with a less conflicting operator. |
Yes, therefore you need to escape them with backslash when embedding. This is true for whatever escape method we use. With the proposed triple backtick literal, you'd still have to take care of `````s in your embedded JS. |
Yah true, why not just have Coffeescript escape the backticks? Maybe that's what you were thinking. Then one backtick would work just fine. Then it just becomes a debate over the style of the syntax, which I don't know if that would be worth our time for an issue like this :-) |
@satyr and @matthewmueller: see my edit #2 above from a few hours ago. Yes, you can use backslashes to escape backticks, and yes, they're broken. |
Hi there. Can we get the triple backtick delimiter added? I write CodeKit and I cannot tell you how many people have asked me to let them combine Javascript and CoffeeScript files together. The most common case is that they want to stick jQuery at the beginning and then write some CoffeeScript against that library. If you add the ``` delimiter, as suggested here, providing this capability in build tools would become trivial. Thank you. |
Just chiming in to bump this for 2 reasons:
|
Also, GitHub markdown uses backticks to escape code, and it lets you surround text with as many ```s as you need so to allow backticks inside the strings:
|
Escapable backticks plus here-backticks would be nice to have. |
+1 |
+1 |
Would changing this line from: @token 'JS', (script = match[0])[1...-1], 0, script.length to @token 'JS', (script = match[0])[1...-1].replace(/\\`/g,'`'), 0, script.length be enough for escaping backticks? |
This is a really important feature, considering how the lack of convenient JavaScript escaping prevents the use of CoffeeScript with, for example, Relay (a complement to React). I'm currently being forced away of CoffeeScript, and into the extremely verbose realm of ES6, by this single little feature. After 2+ years with CoffeeScript and a huge codebase, it's not so easy :( An example of what should be possible to embed in CoffeeScript: var fragments = {
viewer: () => Relay.QL`
fragment on Viewer {
projects { edges { node {
id, name
}}}
}
`,
}; @michaelficarra, @jashkenas: Any chance this 2-year issue might get some love? ;) It sort of future-proofs CoffeeScript by allowing new features of JavaScript to be usable (if mandatory, as is the case for Relay) |
@guigrpa what you could do meanwhile this gets figured out is to keep the queries in a separate JS file and require them from your components. I know the query is supposed to stay inside the component itself but at least it works for now... |
@nickdima That's a good workaround, thanks! |
@nickdima What about the fix for escaping backticks you suggested above (Oct 2, 2015)? Did it indeed solve the bug mentioned by @michaelficarra? |
It doesn't escape escaped escapes (...), but it should work for simple cases, I guess. |
Just to clarify: is there any official strategy for handling backticks (especially those in ES6) or is CoffeeScript kinda saying, "Meh, screw it."? Just trying to plan and can't keep straight the various issues referencing this problem. It's been 4 years, so I'm guessing the CoffeeScript team just doesn't care?
|
CS already has the template string feature using double quotes. You don't want to emit an ES6 template string. Just code it to work that way in CS to begin with. |
No, I think it's about |
@langri-sha Not impossible, just inconvenient.
You can test tag functions by calling with an array of all string parts and then each interpolation as separate arguments. |
@lydell ahh, thank you for taking the time to explain! It's very obvious once you clearly point it out, but my initial attempt was to escape JS and backslash-escape the backticks therein. It's a harsh blow, not understanding how template strings can be built from other primitives and CS not meeting your expectations at the same time.
|
Hello again all. I've found that writing the backtick in one of the supported escape sequences is a quick way to make friends with the compiler. The hexadecimal escape sequence for the backtick is
Now, this only gets you half way to writing template strings, since JS sources do not support escape sequences and you will get a
|
@langri-sha Any idea how this would work in the case of Relay queries mentioned by @guigrpa ? |
@huan-ji Sad to say this, but this was just one of the issues that finally triggered my conversion to ES6. I miss CoffeeScript's terse syntax, though. |
I'm building an asset bundler that can merge both coffeescript and javascript together. It works fine placing coffeescript inside javascript -- compile it and stick it in the js file.
The problem I'm running into is when I try to include javascript into coffeescript. My approach was to bring in the javascript, escape with `, and then compile the coffeescript.
The problem is many libraries use ` in comments -- including backbone. One thought is to use ``` to escape javascript like comment blocks in coffeescript.
Please let me know your thoughts on this.
After some discussion: The whole point of this issue is to point out that the single backtick does not always work the way it's intended. Use cases and outside discussion should be irrelevant here. Currently, the backtick does not work if you try to escape a file like backbone or jquery or even a two line javascript file that has a comment that includes a backtick.
Thanks,
Matt
The text was updated successfully, but these errors were encountered: