diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1011,13 +1011,22 @@ parseAccessSpecifier(); return; case tok::kw_if: + if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + // field/method declaration. + break; parseIfThenElse(); return; case tok::kw_for: case tok::kw_while: + if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + // field/method declaration. + break; parseForOrWhileLoop(); return; case tok::kw_do: + if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + // field/method declaration. + break; parseDoWhile(); return; case tok::kw_switch: @@ -1045,6 +1054,9 @@ return; case tok::kw_try: case tok::kw___try: + if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + // field/method declaration. + break; parseTryCatch(); return; case tok::kw_extern: @@ -1290,6 +1302,12 @@ // element continues. break; case tok::kw_try: + if (Style.Language == FormatStyle::LK_JavaScript && + Line->MustBeDeclaration) { + // field/method declaration. + nextToken(); + break; + } // We arrive here when parsing function-try blocks. if (Style.BraceWrapping.AfterFunction) addUnwrappedLine(); diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -358,6 +358,22 @@ " x();\n" " }\n" "}\n"); + verifyFormat("class KeywordNamedMethods {\n" + " do() {\n" + " }\n" + " for() {\n" + " }\n" + " while() {\n" + " }\n" + " if() {\n" + " }\n" + " else() {\n" + " }\n" + " try() {\n" + " }\n" + " catch() {\n" + " }\n" + "}\n"); } TEST_F(FormatTestJS, ReservedWordsParenthesized) {