mprobst created this revision.
Herald added a subscriber: klimek.
JavaScript supports asynchronous loop iteration in async functions:
for async (const x of y) ...
https://reviews.llvm.org/D33193
Files:
lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -548,6 +548,15 @@
" // Comment.\n"
" return async.then();\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) {
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -1635,6 +1635,8 @@
assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
"'for', 'while' or foreach macro expected");
nextToken();
+ if (FormatTok->is(Keywords.kw_async))
+ nextToken();
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
if (FormatTok->Tok.is(tok::l_brace)) {
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -580,7 +580,11 @@
Tok->Previous->is(tok::period))
break;
Contexts.back().ColonIsForRangeExpr = true;
+ // for async ( ...
+ if (CurrentToken->is(Keywords.kw_async))
+ next();
next();
+
if (!parseParens())
return false;
break;
@@ -2249,6 +2253,10 @@
} else if (Style.Language == FormatStyle::LK_JavaScript) {
if (Left.is(TT_JsFatArrow))
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) &&
Right.MatchingParen) {
const FormatToken *Next = Right.MatchingParen->getNextNonComment();
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -548,6 +548,15 @@
" // Comment.\n"
" return async.then();\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) {
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -1635,6 +1635,8 @@
assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
"'for', 'while' or foreach macro expected");
nextToken();
+ if (FormatTok->is(Keywords.kw_async))
+ nextToken();
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
if (FormatTok->Tok.is(tok::l_brace)) {
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -580,7 +580,11 @@
Tok->Previous->is(tok::period))
break;
Contexts.back().ColonIsForRangeExpr = true;
+ // for async ( ...
+ if (CurrentToken->is(Keywords.kw_async))
+ next();
next();
+
if (!parseParens())
return false;
break;
@@ -2249,6 +2253,10 @@
} else if (Style.Language == FormatStyle::LK_JavaScript) {
if (Left.is(TT_JsFatArrow))
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) &&
Right.MatchingParen) {
const FormatToken *Next = Right.MatchingParen->getNextNonComment();
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits