Commit 0734fb21 authored by Martin Probst's avatar Martin Probst
Browse files

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
parent d2934179
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -1011,13 +1011,22 @@ void UnwrappedLineParser::parseStructuralElement() {
      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 @@ void UnwrappedLineParser::parseStructuralElement() {
    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 @@ void UnwrappedLineParser::parseStructuralElement() {
      // 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();
+16 −0
Original line number Diff line number Diff line
@@ -358,6 +358,22 @@ TEST_F(FormatTestJS, ReservedWordsMethods) {
      "    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) {