clang-format: [JS] Properly set scopes inside template strings.

Before:
  var f = `aaaaaaaaaaaaa:${aaaaaaa
              .aaaaa} aaaaaaaa
           aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;

After:
  var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa
           aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;

llvm-svn: 293622
This commit is contained in:
Daniel Jasper 2017-01-31 13:03:07 +00:00
parent dbaacc7566
commit 24de6fbfdb
3 changed files with 14 additions and 1 deletions

View File

@ -337,11 +337,15 @@ struct FormatToken {
/// \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);
}

View File

@ -1454,7 +1454,9 @@ public:
// 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();
}

View File

@ -1392,6 +1392,13 @@ TEST_F(FormatTestJS, TemplateStrings) {
// 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) {