clang-format: [JS] for async loops.

Summary:
JavaScript supports asynchronous loop iteration in async functions:

    for async (const x of y) ...

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D33193

llvm-svn: 303106
This commit is contained in:
Martin Probst 2017-05-15 19:33:20 +00:00
parent 878715f978
commit bd49e321d3
3 changed files with 23 additions and 3 deletions

View File

@ -576,9 +576,12 @@ private:
} }
break; break;
case tok::kw_for: case tok::kw_for:
if (Style.Language == FormatStyle::LK_JavaScript && Tok->Previous && if (Style.Language == FormatStyle::LK_JavaScript)
Tok->Previous->is(tok::period)) if (Tok->Previous && Tok->Previous->is(tok::period))
break; break;
// JS' for async ( ...
if (CurrentToken->is(Keywords.kw_async))
next();
Contexts.back().ColonIsForRangeExpr = true; Contexts.back().ColonIsForRangeExpr = true;
next(); next();
if (!parseParens()) if (!parseParens())
@ -2249,6 +2252,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
} else if (Style.Language == FormatStyle::LK_JavaScript) { } else if (Style.Language == FormatStyle::LK_JavaScript) {
if (Left.is(TT_JsFatArrow)) if (Left.is(TT_JsFatArrow))
return true; return true;
// for async ( ...
if (Right.is(tok::l_paren) && Left.is(Keywords.kw_async) &&
Left.Previous && Left.Previous->is(tok::kw_for))
return true;
if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) && if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
Right.MatchingParen) { Right.MatchingParen) {
const FormatToken *Next = Right.MatchingParen->getNextNonComment(); const FormatToken *Next = Right.MatchingParen->getNextNonComment();

View File

@ -1635,6 +1635,10 @@ void UnwrappedLineParser::parseForOrWhileLoop() {
assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
"'for', 'while' or foreach macro expected"); "'for', 'while' or foreach macro expected");
nextToken(); nextToken();
// JS' for async ( ...
if (Style.Language == FormatStyle::LK_JavaScript &&
FormatTok->is(Keywords.kw_async))
nextToken();
if (FormatTok->Tok.is(tok::l_paren)) if (FormatTok->Tok.is(tok::l_paren))
parseParens(); parseParens();
if (FormatTok->Tok.is(tok::l_brace)) { if (FormatTok->Tok.is(tok::l_brace)) {

View File

@ -548,6 +548,15 @@ TEST_F(FormatTestJS, AsyncFunctions) {
" // Comment.\n" " // Comment.\n"
" return async.then();\n" " return async.then();\n"
"}\n"); "}\n");
verifyFormat("for async (const x of y) {\n"
" console.log(x);\n"
"}\n");
verifyFormat("function asyncLoop() {\n"
" for async (const x of y) {\n"
" console.log(x);\n"
" }\n"
"}\n");
} }
TEST_F(FormatTestJS, FunctionParametersTrailingComma) { TEST_F(FormatTestJS, FunctionParametersTrailingComma) {