diff options
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 25 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 9 |
2 files changed, 22 insertions, 12 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index ae79ea5d8a6..eda7ef36434 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -360,16 +360,21 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { switch (Tok->Tok.getKind()) { case tok::l_brace: - if (Style.Language == FormatStyle::LK_JavaScript && PrevTok && - PrevTok->is(tok::colon)) - // A colon indicates this code is in a type, or a braced list following - // a label in an object literal ({a: {b: 1}}). - // The code below could be confused by semicolons between the individual - // members in a type member list, which would normally trigger BK_Block. - // In both cases, this must be parsed as an inline braced init. - Tok->BlockKind = BK_BracedInit; - else + if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) { + if (PrevTok->is(tok::colon)) + // A colon indicates this code is in a type, or a braced list + // following a label in an object literal ({a: {b: 1}}). The code + // below could be confused by semicolons between the individual + // members in a type member list, which would normally trigger + // BK_Block. In both cases, this must be parsed as an inline braced + // init. + Tok->BlockKind = BK_BracedInit; + else if (PrevTok->is(tok::r_paren)) + // `) { }` can only occur in function or method declarations in JS. + Tok->BlockKind = BK_Block; + } else { Tok->BlockKind = BK_Unknown; + } LBraceStack.push_back(Tok); break; case tok::r_brace: @@ -391,6 +396,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { // BlockKind later if we parse a braced list (where all blocks // inside are by default braced lists), or when we explicitly detect // blocks (for example while parsing lambdas). + // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a + // braced list in JS. ProbablyBracedList = (Style.Language == FormatStyle::LK_JavaScript && NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index d8f887f9c78..92a113111b6 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -474,9 +474,8 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) { "(function f() {\n" " var x = 1;\n" "}());\n"); - // Known issue: this should wrap after {}, but calculateBraceTypes - // misclassifies the first braces as a BK_BracedInit. - verifyFormat("function aFunction(){} {\n" + verifyFormat("function aFunction() {}\n" + "{\n" " let x = 1;\n" " console.log(x);\n" "}\n"); @@ -1233,6 +1232,10 @@ TEST_F(FormatTestJS, ClassDeclarations) { verifyFormat("class C {\n x: string = 12;\n}"); verifyFormat("class C {\n x(): string => 12;\n}"); verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); + verifyFormat("class C {\n" + " foo() {}\n" + " [bar]() {}\n" + "}\n"); verifyFormat("class C {\n private x: string = 12;\n}"); verifyFormat("class C {\n private static x: string = 12;\n}"); verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}"); |