/// \brief Returns whether \p Tok is ([{ or a template opening <.
bool opensScope() const {
+ if (is(TT_TemplateString) && TokenText.endswith("${"))
+ return true;
return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
TT_TemplateOpener);
}
/// \brief Returns whether \p Tok is )]} or a template closing >.
bool closesScope() const {
+ if (is(TT_TemplateString) && TokenText.startswith("}"))
+ return true;
return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
TT_TemplateCloser);
}
// Consume scopes: (), [], <> and {}
if (Current->opensScope()) {
- while (Current && !Current->closesScope()) {
+ // In fragment of a JavaScript template string can look like '}..${' and
+ // thus close a scope and open a new one at the same time.
+ while (Current && (!Current->closesScope() || Current->opensScope())) {
next();
parse();
}
// The token stream can contain two string_literals in sequence, but that
// doesn't mean that they are implicitly concatenated in JavaScript.
verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
+
+ // Ensure that scopes are appropriately set around evaluated expressions in
+ // template strings.
+ verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
+ " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
+ "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n"
+ " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;");
}
TEST_F(FormatTestJS, TemplateStringASI) {