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

Coffeescript bug: single quoted string with tagged template literal #311

Closed
greghuc opened this issue Jan 31, 2023 · 5 comments · Fixed by #322
Closed

Coffeescript bug: single quoted string with tagged template literal #311

greghuc opened this issue Jan 31, 2023 · 5 comments · Fixed by #322
Assignees
Labels
bug Something isn't working coffeescript CoffeeScript compatibility related

Comments

@greghuc
Copy link

greghuc commented Jan 31, 2023

Coffeescript bug (parse error)

"civet coffeeCompat"

upperCaseExpr = (textParts, expressions...) ->
  textParts.reduce (text, textPart, i) ->
    text + expressions[i - 1].toUpperCase() + textPart

greet = (_name, _adjective) ->
  upperCaseExpr'A string with no variables'

console.log greet("greg", "awesome")

gives

"civet coffeeCompat"

upperCaseExpr = (textParts, expressions...) ->
  textParts.reduce (text, textPart, i) ->
    text + expressions[i - 1].toUpperCase() + textPart

greet = (_name, _adjective) ->
  upperCaseExpr'A string with no variables'
               ^ ParseError

Playground links

Coffeescript link
Civet link

@greghuc
Copy link
Author

greghuc commented Jan 31, 2023

Use of tripped quoted string works fine:

"civet coffeeCompat"

upperCaseExpr = (textParts, expressions...) ->
  textParts.reduce (text, textPart, i) ->
    text + expressions[i - 1].toUpperCase() + textPart

greet = (_name, _adjective) ->
  upperCaseExpr"""A string with no variables"""

console.log greet("greg", "awesome")

I believe the underlying issue is that Coffeescript is converting both the single-quoted string and triple-quoted string to a template literal. Civet seems to convert single-quoted to a double-quoted string, and triple-quoted to a template literal.

And a quick check with the Chrome dev tools console shows that the Javascript tagged template literal function only wants a template literal, not a single or double quoted string (grabbing example from MDN):

function tag(strings) {
  console.log(strings.raw[0]);
}

tag`string text line 1 \n string text line 2`;
=> string text line 1 \n string text line 2

tag"string text line 1 \n string text line 2";
=> Uncaught SyntaxError: Unexpected string
tag'string text line 1 \n string text line 2';
=> Uncaught SyntaxError: Unexpected string

@edemaine edemaine added bug Something isn't working coffeescript CoffeeScript compatibility related labels Jan 31, 2023
@edemaine
Copy link
Collaborator

Indeed, 'strings' are currently not able to become template literals, while '''strings''' and """strings""" always are and "strings" are in coffeeCompat mode.

We should be able to fix this, but it will require some more tweaking to separation between string literals and template literals previously considered in #294.

Meanwhile, please use "strings" or '''strings'''.

@edemaine edemaine changed the title Coffeescript big: single quoted string with tagged template literal Coffeescript bug: single quoted string with tagged template literal Jan 31, 2023
@STRd6
Copy link
Contributor

STRd6 commented Feb 2, 2023

@edemaine Do you see any downside to allowing all types of strings to be template literals as a default?

@edemaine
Copy link
Collaborator

edemaine commented Feb 2, 2023

No, I don't think so, which is why CoffeeScript made this decision. So I was thinking of just adding StringLiteral to the tagged template literal rule, and converting the string to back ticks. (Actually we may need to do this already with CoffeeScript double-quote strings that have no expansions.) Feel free to do this, or I can.

@STRd6
Copy link
Contributor

STRd6 commented Feb 2, 2023

You can. I'm looking into ||> right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working coffeescript CoffeeScript compatibility related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants