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 @@ -1015,9 +1015,15 @@ 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: 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 @@ -343,6 +343,14 @@ " x: 'x'\n" "};", "const Axis = {for: 'for', x: 'x'};"); + verifyFormat("class KeywordNamedMethods {\n" + " do() {\n" + " }\n" + " for() {\n" + " }\n" + " while() {\n" + " }\n" + "}\n"); } TEST_F(FormatTestJS, ReservedWordsMethods) {