close
The Wayback Machine - https://web.archive.org/web/20201030042028/https://github.com/rubys/ruby2js/pull/58
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

Tagged template literals #58

Merged
merged 13 commits into from Oct 9, 2020
Merged

Conversation

@jaredcwhite
Copy link
Collaborator

@jaredcwhite jaredcwhite commented Oct 8, 2020

This is a first pass at a filter which allows a list of tags for prefixing template literal strings. It will convert this:

output = html "some text here"

to this:

let output = html`some text here`

It works with any length of string as well as heredocs. I also provided a customization option for the filter…by default it looks for html and css but you can change it to look for any tags you want using the template_literal_tags option.

I also made sure it doesn't process anything if there's a target for the send, aka self.html or some_obj.html or whatever. Should work around most edge cases.

Probably should be a separate PR, but I also fixed a few bugs with the camelCase filter. When using an underscore prefix, before it would turn _method_name into MethodName rather than _methodName. I also made it support numbers (foo_bar_123 becomes fooBar123) and method calls using suffixes like =, ?, and !.

Let me know what you think!

@jaredcwhite
Copy link
Collaborator Author

@jaredcwhite jaredcwhite commented Oct 8, 2020

Looks like tests prior to 2.3 are failing…is supporting those versions still a requirement? (Ruby 2.5 is now the oldest officially supported version to the best of my knowledge.)

jaredcwhite added 2 commits Oct 8, 2020
@jaredcwhite
Copy link
Collaborator Author

@jaredcwhite jaredcwhite commented Oct 8, 2020

One issue I found: escaping backticks in a JS template literal requires doing \`, but can't seem to get that to output correctly out of the Ruby code. It either swallows the backslash or I get two backslashes. This seems to be an existing issue, prior to this PR.

@jaredcwhite
Copy link
Collaborator Author

@jaredcwhite jaredcwhite commented Oct 8, 2020

OK, that should be fixed now.

@rubys
Copy link
Owner

@rubys rubys commented Oct 8, 2020

Feel free to drop versions of Ruby prior to 2.3.

@rubys
Copy link
Owner

@rubys rubys commented Oct 8, 2020

Consider updating the README?

@jaredcwhite
Copy link
Collaborator Author

@jaredcwhite jaredcwhite commented Oct 8, 2020

Sure, yeah I didn't want to get into docs too much until the approach seemed good to you. Will work on that shortly.

@jaredcwhite jaredcwhite requested a review from rubys Oct 9, 2020
@jaredcwhite
Copy link
Collaborator Author

@jaredcwhite jaredcwhite commented Oct 9, 2020

All right, I added docs to the readme and tests are passing now. Looks good to merge?

@rubys
rubys approved these changes Oct 9, 2020
Copy link
Owner

@rubys rubys left a comment

LGTM.

  • ruby2js.gemspec needs to have required_ruby_version updated before a new gem is pushed.
  • consider having filter/tagged_templates.rb require ruby2js/es2015 - this isn't mandatory, but any JS implementation that supports tagged templates should also support let, class, arrow functions and other goodies added in es2015.
@jaredcwhite jaredcwhite merged commit fd146af into rubys:master Oct 9, 2020
1 check was pending
1 check was pending
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
@rubys
Copy link
Owner

@rubys rubys commented Oct 9, 2020

Now moot, but worth noting that without es2015

html "foo#{bar}" produces html"foo" + bar

@jaredcwhite
Copy link
Collaborator Author

@jaredcwhite jaredcwhite commented Oct 9, 2020

Now moot, but worth noting that without es2015

html "foo#{bar}" produces html"foo" + bar

Yep, good call to add that minimum es2015 requirement for the filter.

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

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.