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

Implement tagged template string #103

Closed
teohhanhui opened this issue Sep 15, 2015 · 8 comments
Closed

Implement tagged template string #103

teohhanhui opened this issue Sep 15, 2015 · 8 comments

Comments

@teohhanhui
Copy link

As per your gist from 2012: https://gist.github.com/slevithan/4222600

@slevithan
Copy link
Owner

IMO this is not a priority until more browsers support tagged template strings natively.

Some changes in thinking and details since I wrote that gist:

  • Not sure what the name of the tag should be, but probably not r. Maybe XRegExp.make or XRegExp.create.
  • You could do XRegExp.make(flags)... to provide flags. Not just XRegExp.make(?flags)... (especially since the latter wouldn't support flags g or y due to this, explained here).
  • It might make sense to support regex and or string interpolation alongside the otherwise raw strings.
    • Perhaps interpolated strings should have their metacharacters escaped, and interpolated regexes should have their backreferences safely rewritten (like the handling of XRegExp.union).
    • Interpolated strings and regexes should probably be treated as atomic tokens (i.e. immediately following quantifiers should apply to the entire token).
    • Probably best to just ignore flags for interpolated regexes, rather than attempting to partially support them.
  • Would want to implicitly enable free-spacing as described in the gist, but also maybe implicitly enable "explicit capture" (flag n) and ES6's unicode flag (u).
    • Probably don't want to offer multiple tags with any variation of implicit flags (the gist describes r vs rx) to avoid confusion and complexity.
  • Would want to add $<n> and $<name> as alternative backreference syntax for ${n} and ${name} in replacement text, so they can be used in ES6 template strings. This would be backward compatible, since $< is currently an error in XRegExp replacement text.

@teohhanhui
Copy link
Author

Do tagged template strings not work properly with a transpiler like Babel?

@slevithan
Copy link
Owner

I don't know whether Babel supports creation of arbitrary new tag functions. Would be happy to hear/see more info about this. Of course, you can and probably should use Babel today to get raw multiline strings for more convenient use with XRegExp. No changes to XRegExp are needed for that.

If the XRegExp.make tag were able to work in non-ES6 browsers (via transpilers), then it would probably be better to not implicitly enable the ES6 u flag since I don't think that can be accurately transpiled to ES5.

@rauschma
Copy link

@slevithan A tagged template literal is just another way of making a function call: the first parameter is an Array with the static parts of the template literal, the remaining parameters are the dynamic ${} parts of the template literal.

Thus, you can even write tag functions in ES5.

More info: http://exploringjs.com/es6/ch_template-literals.html#_implementing-tag-functions

@Kagami
Copy link

Kagami commented Jan 9, 2016

It's perfectly possible to use XRegExp(String.raw^test\raw\slashes$)) btw.

@treyhunner
Copy link

Just chiming in to add that I'm most interested in seeing a special tag in XRegExp so that interpolated strings can be escaped. A RegExp.escape proposal was rejected by TC39 last year and it was suggested that a template tag with escaping on interpolation would be more safe than escaping combined with plain string concatenation. Further discussion of this is at tc39/proposal-regex-escaping#37.

Until XRegExp supports escaping via a tag, I'll continue my search for a tag function that escapes interpolated strings for use in regular expressions.

@josephfrazier
Copy link
Collaborator

josephfrazier commented Feb 15, 2017

Over at OctoLinker/OctoLinker#282, I've made some progress on an XRegExp.build-backed implementation of a tag function that interpolates regular expressions and strings as described above, and it seems to work pretty well so far. @slevithan, would you be interested in a pull request that adds it to the build addon?

Note that template literals are now supported natively in most browsers except IE, but Babel can transpile them down to regular function calls (as mentioned above), so they can work in IE as well.

@slevithan
Copy link
Owner

would you be interested in a pull request that adds it to the build addon?

Yes, interested.

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

No branches or pull requests

6 participants