embroider-build / content-tag

A rust program that uses a fork of SWC to parse and transform Javascript containing the content-tag proposal
MIT License
8 stars 7 forks source link

BUG: backticks are not escaped #24

Closed NullVoxPopuli closed 1 year ago

NullVoxPopuli commented 1 year ago

Found this issue when trying to integrate content-tag in to ember-template-imports.

ef4 commented 1 year ago

I assumed that getting this right was the responsibility of swc's printer, but clearly it is not doing that.

We can do the escaping ourselves if we need to but I'll also investigate whether this should be considered an swc bug or whether there's some existing swc api we're supposed to be using.

chancancode commented 1 year ago

fwiw I was a little suspicious of us using an “atom” to store the template tag context, I wonder if swc didn’t expect/meant for the atom to contain things that could require certain kind of escaping?

ef4 commented 1 year ago

I added a test for the second case which is unique to template literals: dollar sign.

While fixing this I took a more careful look at what we're doing with escaping and discovered another problem that has been present all along in both ember-template-imports and in content-tag: https://github.com/embroider-build/content-tag/issues/26

ef4 commented 1 year ago

fwiw I was a little suspicious of us using an “atom” to store the template tag context, I wonder if swc didn’t expect/meant for the atom to contain things that could require certain kind of escaping?

Atom is their "locally-interned string" type. It doesn't by itself imply anything about what's in the string. In this case, we're sticking the atom into TplElement { raw }, and it makes sense that the raw representation is supposed to be just the raw characters that go into the source. Their printer is trusting that it won't be invalid because their parser never puts invalid strings in there.

So I think it's fine for us to do our own escaping. It's up to us to interpret the body of the content-tag and decide how to re-represent it as a template literal.