-
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
CS1 tagged template literals (and CS2 interpolated strings as template literals) #4352
CS1 tagged template literals (and CS2 interpolated strings as template literals) #4352
Conversation
interpolation
I was thinking that it would be really nice to do this as two commits, if not two PRs:
|
@connec the current plan is to add tagged template literal support, as that's needed for ES6 compatibility. This feature could be published as CS 1.9.x, as it's an opt-in feature. We'd then compile However, your suggestion of doing |
a"""#{b}""" | ||
^^^^^^^^^^ | ||
''' | ||
# TODO: Tagged template literals impl: disabling these tests, as now valid 'tagged template literals' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than commenting these out, I would move these tests into test/tagged_template_literals.coffee
and update them accordingly. This is a pretty thorough review of all the ways tagged template literals can be written, from a''
to a"""b"""
and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than moving and changing these tests, I would change all the a
s to 1
s. For example, "a''"
→ "1''"
. This is about ensuring that unexpected string errors work correctly, and is still a valid thing to test after tagged template literals are added.
|
||
func = (text) -> "I saw: #{text}" | ||
|
||
eq 'I saw: a single line string', func'a single line string' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don’t forget the test
line, e.g. test "basic tagged template literal", ->
use number not identifier prefix. Identifer prefixes are now valid as tagged template literals
strings and tag function.
block string | ||
""" | ||
|
||
test "tagged template literals: string prefix must be function", -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put these in test/error_messages.coffee?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lydell since you're around right now, are you up for a webchat about tagged template literals? I can do Skype on my github username, or Google Hangouts on ghuczynski at gmail.com
I'm working on tagged template literals today Sunday, and I'd appreciate talking to someone with more CS compiler knowledge than me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m sorry, I won’t have the time.
eq 'text: [single-line block string] expressions: []', func"""single-line block string""" | ||
|
||
eq 'text: [multi-line single quotes] expressions: []', func'multi-line | ||
single quotes' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid this kind of crazy indentation :)
Pull tagged template definition up to Invocation level in grammar, enabling chained invocation calls. We can view a tagged template is a special form of function call.
Tagged template literals status update. So I've got tagged template literals working for pure, non-interpolated Strings (code will need tidy up before checkin). Interpolated strings are next. This will require StringWithInterpolations in nodes.coffee to be custom-output as a ES6 template literal. E.g:
I'm still working out how to do this in nodes.coffee, so any input is appreciated. StringWithInterpolations is a very general class, since it's a Parens wrapping a body. Rough rules for outputting StringWithInterpolations as a ES6 template literal:
|
I assume you’re not intending to support |
@GeoffreyBooth only Using your example, here's the desired Coffeescript to Javascript compilation:
At present, Coffeescript outputs For tagged template literals, we need the Coffeescript to output instead as |
I've had a quick look at what would need to be done to compile interpolated strings to template literals, and it's not as easy as I'd hoped since most of the work is done in the lexer. I'd probably attempt to avoid making changes to the grammar and lexer (as far as template literals are concerned, ofc tagging will need some grammar changes). One approach could be to flesh out "a #{b} c"
# StringWithInterpolations
# strings: [
# StringLiteral 'a '
# StringLiteral ' c'
# ]
# interpolations: [
# IdentifierLiteral 'b'
# ] It should be possible to build that structure from the |
…d-template-literals
…yBooth/coffeescript into tagged-template-literals
into Call constructor. StringWithInterpolations will be output as template literal, so already in correct form for outputting tagged template literal.
Still needs comprehensive testing.
…d-template-literals # Conflicts: # lib/coffee-script/nodes.js # lib/coffee-script/parser.js
Having thought about We should then deal with |
…pt 2 all-ES2015 syntax output
I commented out |
This looks fine to me from a quick glance. As an aside — for this sort of thing, it feels like you guys are stealing a little bit of your own thunder by maintaining the CS1 compatibility. If you just released the even-more-streamlined version for CS2 only, I bet folks would get more excited about CS2. In short: It's awfully nice of you. |
@jashkenas Yeah, but the code is right here for anyone to adapt as a 1.x PR. It was written originally as a 2-only PR, but it was made 1.x-friendly by commenting out about three lines of code, so it was inevitable that people would figure it out 😄 The 2 branch has two big features that should hopefully motivate people to upgrade: classes that can extend ES classes, and async/await. Those two alone should be enough for a big chunk of people. |
@greghuc Congratulations! Would you like to follow this up with a PR adding documentation for this? And another PR for |
@GeoffreyBooth thanks! Re the PRs, I'll aim to fit them in over the next week. Quick question: where's the right place to be doing the follow-on PRs?
|
@greghuc for documentation yes, the current 1.x docs. They’re in the For interpolated strings, branch off of |
@greghuc I've done plenty of refactoring regarding strings with interpolations – all the way from the lexer to nodes.coffee. Feel free to ping me in PRs and I'll try to help out. |
Tested implementation of tagged template literals, as defined at: coffeescript6/discuss#28 (comment)
For CS2, we also get interpolated strings outputting as ES6 template literals. This fell out of the tagged template literal work.