Skip to content

Commit 0734fb2

Browse files
committed
clang-format: [JS] Handle more keyword-named methods.
Summary: Including `do`, `for`, and `while`, `if`, `else`, `try`, `catch`, in addition to the previously handled fields. The unit test explicitly uses methods, but this code path handles both fields and methods. Reviewers: krasimir Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72827
1 parent d293417 commit 0734fb2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,13 +1011,22 @@ void UnwrappedLineParser::parseStructuralElement() {
10111011
parseAccessSpecifier();
10121012
return;
10131013
case tok::kw_if:
1014+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1015+
// field/method declaration.
1016+
break;
10141017
parseIfThenElse();
10151018
return;
10161019
case tok::kw_for:
10171020
case tok::kw_while:
1021+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1022+
// field/method declaration.
1023+
break;
10181024
parseForOrWhileLoop();
10191025
return;
10201026
case tok::kw_do:
1027+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1028+
// field/method declaration.
1029+
break;
10211030
parseDoWhile();
10221031
return;
10231032
case tok::kw_switch:
@@ -1045,6 +1054,9 @@ void UnwrappedLineParser::parseStructuralElement() {
10451054
return;
10461055
case tok::kw_try:
10471056
case tok::kw___try:
1057+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1058+
// field/method declaration.
1059+
break;
10481060
parseTryCatch();
10491061
return;
10501062
case tok::kw_extern:
@@ -1290,6 +1302,12 @@ void UnwrappedLineParser::parseStructuralElement() {
12901302
// element continues.
12911303
break;
12921304
case tok::kw_try:
1305+
if (Style.Language == FormatStyle::LK_JavaScript &&
1306+
Line->MustBeDeclaration) {
1307+
// field/method declaration.
1308+
nextToken();
1309+
break;
1310+
}
12931311
// We arrive here when parsing function-try blocks.
12941312
if (Style.BraceWrapping.AfterFunction)
12951313
addUnwrappedLine();

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ TEST_F(FormatTestJS, ReservedWordsMethods) {
358358
" x();\n"
359359
" }\n"
360360
"}\n");
361+
verifyFormat("class KeywordNamedMethods {\n"
362+
" do() {\n"
363+
" }\n"
364+
" for() {\n"
365+
" }\n"
366+
" while() {\n"
367+
" }\n"
368+
" if() {\n"
369+
" }\n"
370+
" else() {\n"
371+
" }\n"
372+
" try() {\n"
373+
" }\n"
374+
" catch() {\n"
375+
" }\n"
376+
"}\n");
361377
}
362378

363379
TEST_F(FormatTestJS, ReservedWordsParenthesized) {

0 commit comments

Comments
 (0)