Proposal: Symbol.templateTag

Alexander Jones alex at
Thu Nov 17 01:02:55 UTC 2016

Hi es-discuss!

Template tags are a great feature with many novel applications, IMO
approaching macro-level expressiveness. But due to their nature in this
context, it's helpful if tags are named quite tersely. Unfortunately, if
you have existing API, this can be challenging.

Let's say I have a DSL (domain-specific language), and I *may*, unsafely,
generate typed snippets of code containing that DSL like so:

function dsl(code: string) {
    // `code` MUST be well-formed

const someStatement = dsl("val foo = " + dslEncode(foo));

Let's say I'd like to add support in my library for ES6 clients such that
they can do:

const someStatement = dsl`val foo = ${foo}`;

where the `dsl` template tag would be implemented such that `dslEncode` is
used on each interpolated expression.

Unfortunately, the `dsl` function's arguments are already defined and can't
be changed. I'd need a new name, like `dsl.tag`, which would end up being a
fair bit more verbose.

Would it be possible, at this point, to introduce a new behaviour into ES
such that instead of merely calling the tag object, the implementation
first checks for a `Symbol.templateTag` property on the tag object, and if
it exists, it is invoked? I'd guess this can't Break The Web because no-one
can realistically be using an existing function as a template tag if it was
not already designed for it.

const someStatement = dsl`val foo = ${foo}`;
// desugars to, approximately
const someStatement = (dsl[Symbol.templateTag] || dsl)(["val foo =", ""],

To be honest, I am kind of surprised it wasn't already implemented like
this, but maybe there were performance concerns with the branching.
Interested in your thoughts.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list