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

[CS2] Output interpolated strings as template literals #4365

Merged
merged 4 commits into from
Nov 28, 2016
Merged

[CS2] Output interpolated strings as template literals #4365

merged 4 commits into from
Nov 28, 2016

Conversation

greghuc
Copy link
Contributor

@greghuc greghuc commented Nov 19, 2016

In CS2, output interpolated strings as template literals.
Follow on work from #4352

# Uncomment the following line in CoffeeScript 2, to allow all interpolated
# strings to be output using the ES2015 syntax:
# unwrap: -> this
#TODO: Is this needed / correct?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is correct. This is needed for traverseChildren introspection. If you don't use this, => "foo #{this}" will not have its this bound because that one traverseChildren wouldn't have traversed inside the interpolations.

@greghuc
Copy link
Contributor Author

greghuc commented Nov 19, 2016

@GeoffreyBooth and @lydell I've made the changes to output interpolated strings as template literals.

Am looking for some input, as I'm lacking nodes.coffee knowledge. Specifically, StringWithInterpolations now extends Base, not Parens. Extending Parens is unnecessary, as we don't need to bracket template literals (they have clear backtick begin and end markers).

Am looking for guidance on which StringWithInterpolations fields to overload from Base. I copied Parens, which overloaded children, unwrap and isComplex. I am unclear on whether we need to overload children or isComplex. The tests pass if I remove these fields.

Relevant lines are below.

Thoughts?

exports.StringWithInterpolations = class StringWithInterpolations extends Base
  constructor: (@body) ->

  #TODO: Is this needed / correct?
  children: ['body']

  #NOTE: unwrap returns this to stop ancestor nodes reaching in to grab @body, and using
  #      @body.compileNode. StringWithInterpolations.compileNode is _the_ custom logic to
  #      output interpolated strings as code.
  unwrap: -> this

  #TODO: Is this needed / correct?
  isComplex : -> @body.isComplex()

@GeoffreyBooth
Copy link
Collaborator

@lydell and @jashkenas I’ve been wondering about the purpose of children, unwrap, isComplex, isStatement, jumps, makeReturn and so on since I started working in the CoffeeScript codebase. Perhaps it’s time for a new wiki page explaining how nodes.coffee works? Or more detailed comments?

@lydell
Copy link
Collaborator

lydell commented Nov 27, 2016

Explaining how that stuff works would be awesome. I struggle with it every time I touch nodes.coffee.

@lydell
Copy link
Collaborator

lydell commented Nov 27, 2016

I think this is roughly what some of them do:

  • children tells which properties to recurse into when tree walking.
  • If node.isComplex() is false, it is safe to use node more than once. Otherwise you need to store the value of node in a variable and output that variable several times instead. Kind of like this: 5 is not complex. returnFive() is complex.
  • I guess makeReturn has to do with implicit returns.
  • Not sure what jumps does.
  • Regarding isStatement, I guess it has to do with "everything is an expression". A few things can't be expressions, though, such as break. I think there are some error messages that come from nodes.coffee due statements ending up in expression position.

All of the above might be used for more things as well, but that's just what I (think I) remember from the top of my head.

@GeoffreyBooth
Copy link
Collaborator

Thanks @lydell. Okay @greghuc, I think you got everything right. I updated your comments to use Markdown, since those comments become the annotated source, and I removed the TODOs per @lydell’s notes. I think this is probably good to go. @lydell, did you have any other notes?

@GeoffreyBooth GeoffreyBooth changed the title [wip][CS2] Output interpolated strings as template literals [CS2] Output interpolated strings as template literals Nov 27, 2016
@GeoffreyBooth GeoffreyBooth merged commit 33e6242 into jashkenas:2 Nov 28, 2016
@jashkenas
Copy link
Owner

jashkenas commented Nov 28, 2016

I think this is roughly what some of them do:

children tells which properties to recurse into when tree walking.

The children list is the structure of the AST. The parent pointer, and the pointer to the children are how you can traverse the tree.

If node.isComplex() is false, it is safe to use node more than once. Otherwise you need to store the value of node in a variable and output that variable several times instead. Kind of like this: 5 is not complex. returnFive() is complex.

Exactly. It's all about "do we need to cache this expression?"

I guess makeReturn has to do with implicit returns.

makeReturn is what we use to force an arbitrary node into the returned value from a function. Helpful for implicit returns — when any valid coffeescript statement or expression could need to be returned ... something that doesn't happen in JS. Most of these are simple. If/elses and for-loops are not.

Not sure what jumps does.

Jumps tells you if an expression, or an internal part of an expression has a flow control construct (like break, or continue, or return, or throw) that jumps out of the normal flow of control and can't be used as a value. This is important because things like this make no sense — we have to disallow them:

value = if condition
  100
else
  return 5

Regarding isStatement, I guess it has to do with "everything is an expression". A few things can't be expressions, though, such as break. I think there are some error messages that come from nodes.coffee due statements ending up in expression position.

Exactly. Things that isStatement returns true for are things that can't be used as expressions.


If anyone wants to put any of the above into docs or further comments, feel free.

@GeoffreyBooth
Copy link
Collaborator

@lydell and @jashkenas I merged your comments into https://github.com/jashkenas/coffeescript/wiki/%5BHowTo%5D-How-parsing-works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants