-
-
Notifications
You must be signed in to change notification settings - Fork 662
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 template literals #1715
Conversation
Turns out there's an edge case here with tagged template literals, where there is a |
how about just throw an error for any invalid escapes for now? |
Hmm, but that proposal on Stage 1 for a long time |
That's the state on the repo, yeah, but it seems to actually be a finished proposal. |
Also still thinking about codegen here. With tagged template literals using a user defined function of the form |
I suggest a tradeoff. Generate separate versions with arity less than 6-8 arguments threshold similar to monomorphisation. But fallback to sigle Actually this approach will be great for any variadic functions (except exported I guess) |
Now compiles untagged template literals with substitutions to a static |
This should be mostly working now. Updated the opening post with the details. |
Could you add also |
From what I can tell so far, class String {
...
static raw(tsa: TemplateStringsArray, ...args: any[]): string {
...
}
} where String.raw`Hello\n${1+2}` // "Hello\\n3" |
yeah. That's definitely should be a builtin. At least for now |
This |
Perhaps one option would be to detect a signature like |
I don't have a good solution for variadic functions in general yet, as it both loses type info so one has to cast, which we cannot do between references and basic values yet, and to implement that would need boxing, which is a bit meh without a stack. |
Count me for very excited :D |
I tested this locally and everything worked! Is there anything else blocking this in its current form? |
Should be good to go I think, except that tagged template literals are currently somewhat unsafe due to the lack of |
It's also still missing a |
Looks great! I think most users will used untagged so the error is good for now. Thanks for the quick work! |
Implements parsing and compilation of (tagged) template literals:
StaticArray<string>
with placeholders between the string parts for the substitution values, that is thenjoin("")
edTemplateStringsArray
including its.raw
property when the function takes aTemplateStringsArray
Array<string>
without a.raw
property if the first argument is of another type to avoid adding unnecessary static data for the.raw
propertyOne difference to JS is that if a tagged template literal contains invalid escape sequences, which is allowed since Template Literals Revision, we are not actually inserting an
undefined
into theTemplateStringsArray
but keep the broken string. A bit questionable as it avoids unnecessarynull
checks at the expense of not doing exactly what the spec says, solely justified by "but we don't haveundefined
". Thoughts?