Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport for ES6 Templates #960
Conversation
| @@ -5932,6 +5939,12 @@ module ts { | |||
| return getUnionType([type1, type2]); | |||
| } | |||
|
|
|||
| function checkTemplateExpression(node: TemplateExpression): void { | |||
| forEach((<TemplateExpression>node).templateSpans, templateSpan => { | |||
This comment has been minimized.
This comment has been minimized.
CyrusNajmabadi
Oct 25, 2014
Contributor
Explain why this is correct. I believe it's because we rewrite this to string addition, and string addition allows the other side to be of any type. So we don't need to check for things like void types, etc. But it would be good to comment this bit.
| } | ||
|
|
||
| function getTemplateLiteralAsStringLiteral(node: LiteralExpression): string { | ||
| return "\"" + escapeString(node.text) + "\""; |
This comment has been minimized.
This comment has been minimized.
| } | ||
|
|
||
| function emitTemplateExpression(node: TemplateExpression): void { | ||
| if (compilerOptions.target >= ScriptTarget.ES6) { |
This comment has been minimized.
This comment has been minimized.
CyrusNajmabadi
Oct 25, 2014
Contributor
Explain that in ES6 we don't need to do anything. But that in ES5 we'll convert things according. Explain the type of conversion you're trying to do.
| } | ||
|
|
||
| var templateNeedsParens = isExpression(node.parent) && | ||
| comparePrecedenceToBinaryPlus(node.parent) !== Comparison.LessThan; |
This comment has been minimized.
This comment has been minimized.
CyrusNajmabadi
Oct 25, 2014
Contributor
explain this bit. (i understand it. but we should make the code clear).
| // All unary operators have a higher precedence apart from yield. | ||
| // Arrow functions and conditionals have a lower precedence, | ||
| // although we convert the former into regular function expressions in ES5 mode, | ||
| // and in ES6 mode this function won't get called anyway. |
This comment has been minimized.
This comment has been minimized.
| * a literal component of a TemplateExpression. | ||
| */ | ||
| function scanTemplateAndSetTokenValue(): SyntaxKind { | ||
| var isStartOfTemplate = text.charCodeAt(pos) === CharacterCodes.backtick; |
This comment has been minimized.
This comment has been minimized.
| pos++; | ||
| var start = pos; | ||
| var contents = "" | ||
| var resultingToken = SyntaxKind.Unknown; |
This comment has been minimized.
This comment has been minimized.
CyrusNajmabadi
Oct 25, 2014
Contributor
why ever set this here? Can 'unknown' actually be returned from here? If so, when?
| case CharacterCodes.singleQuote: | ||
| return "\'"; | ||
| case CharacterCodes.doubleQuote: | ||
| return "\""; |
This comment has been minimized.
This comment has been minimized.
| return "\""; | ||
| case CharacterCodes.x: | ||
| case CharacterCodes.u: | ||
| var ch = scanHexDigits(ch === CharacterCodes.x ? 2 : 4, /*useExactCount*/ true); |
This comment has been minimized.
This comment has been minimized.
| * Unconditionally back up and scan a template expression portion. | ||
| */ | ||
| function reScanTemplateToken(): SyntaxKind { | ||
| Debug.assert("'reScanTemplateToken' should only be called on a '}'"); |
This comment has been minimized.
This comment has been minimized.
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| `b`: 321 | ||
| ~~~~~~~ | ||
| !!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. |
This comment has been minimized.
This comment has been minimized.
| @@ -5985,7 +5998,11 @@ module ts { | |||
| return booleanType; | |||
| case SyntaxKind.NumericLiteral: | |||
| return numberType; | |||
| case SyntaxKind.TemplateExpression: | |||
| checkTemplateExpression(<TemplateExpression>node); | |||
| // fall through | |||
This comment has been minimized.
This comment has been minimized.
DickvdBrink
Oct 25, 2014
Contributor
This comment has been minimized.
This comment has been minimized.
DanielRosenwasser
Oct 27, 2014
Author
Member
Thanks @DickvdBrink - I try to keep up that pattern in general.
Although, I'm turning this into a straight return.
|
"Do we want to accept simple (non-substitution) templates in places we accept string literals (i.e. module names, overloading on string constants, etc.)? On one hand, it would seem more consistent with the rest of our language, but ECMAScript 6 does not accept them in property names." I would say no. I'm not sure we have a reason to be more lenient than ES6. |
|
@JsonFreeman I agree; what about indexing? |
|
I think we can type an index expression with literal, yes. |
This comment has been minimized.
This comment has been minimized.
|
Please put comments explaining what all these things are. |
This comment has been minimized.
This comment has been minimized.
|
If you factor out the meat of this function, you can use it for the "outside" case. You just need to pass in a precedence instead of an expression. |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
StringOrRegularExpressionOrTemplateLiteral |
Support for ES6 Templates
|
@RyanCavanaugh excuse me. please add |
Vadorequest
commented
Nov 19, 2014
|
I just noticed that this ES6 feature is still a proposal, so why implement it already? Not that it isn't great and useful, just wondering why focusing on things that are still |
|
It's actually pretty much nailed down, and it's very unlikely that it will be removed/modified drastically. If anything does change, we'll be very mindful in addressing that. =) |
Vadorequest
commented
Nov 20, 2014
|
Ok, nice then, it's a nice feature btw, looking forward for 1.4. :) |
topherfangio
commented
Dec 12, 2014
|
Hi there, I realize that this is on the Roadmap for 1.4. I'm curious though if we might see it sooner as perhaps an experimental option. My team and I would certainly love to start using this sooner rather than later to clean up our code :-) Cheers! |
|
Hey @topherfangio, glad to hear you're enthusiastic about this feature. If you're dying to try it out, you can clone our repo and use our |
DanielRosenwasser commentedOct 25, 2014
This pull request is meant to add support for ECMAScript 6 templates and address suggestion #13.
Right now this branch supports:
Things that will be done in a separate commit:
Questions worth asking: