From: Daniel Jasper Date: Mon, 1 Jun 2015 09:56:32 +0000 (+0000) Subject: clang-format: [JS] Making arrow function wrapping more consistent. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=32d47d738191399a87ffd6cc945f60586241a8af;p=clang clang-format: [JS] Making arrow function wrapping more consistent. Before: someFunction(() => { doSomething(); // break }) .doSomethingElse( // break ); After: someFunction(() => { doSomething(); // break }) .doSomethingElse( // break ); This is still bad, but at least it is consistent with what we do for other function literals. Added corresponding tests. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@238736 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 4e8f5af263..39b2793fee 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -481,11 +481,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, bool NestedBlockSpecialCase = Current.is(tok::r_brace) && State.Stack.size() > 1 && State.Stack[State.Stack.size() - 2].NestedBlockInlined; - if (!NestedBlockSpecialCase) { - for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { + if (!NestedBlockSpecialCase) + for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) State.Stack[i].BreakBeforeParameter = true; - } - } if (PreviousNonComment && !PreviousNonComment->isOneOf(tok::comma, tok::semi) && @@ -689,11 +687,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, // }, a, b, c); if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) && State.Stack.size() > 1) { - if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) { - for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { + if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) + for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) State.Stack[i].NoLineBreak = true; - } - } State.Stack[State.Stack.size() - 2].NestedBlockInlined = false; } if (Previous && (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) || diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index ad40d99ad8..501865e9ad 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -749,8 +749,8 @@ private: // recovered from an error (e.g. failure to find the matching >). if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro, TT_FunctionLBrace, TT_ImplicitStringLiteral, - TT_InlineASMBrace, TT_RegexLiteral, - TT_TrailingReturnArrow)) + TT_InlineASMBrace, TT_JsFatArrow, + TT_RegexLiteral, TT_TrailingReturnArrow)) CurrentToken->Type = TT_Unknown; CurrentToken->Role.reset(); CurrentToken->MatchingParen = nullptr; diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 29b8aa1ff1..20c10b634b 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -337,6 +337,14 @@ TEST_F(FormatTestJS, FunctionLiterals) { " doSomething();\n" " doSomething();\n" " }, this));"); + + // FIXME: This is bad, we should be wrapping before "function() {". + verifyFormat("someFunction(function() {\n" + " doSomething(); // break\n" + "})\n" + " .doSomethingElse(\n" + " // break\n" + " );"); } TEST_F(FormatTestJS, InliningFunctionLiterals) { @@ -455,7 +463,14 @@ TEST_F(FormatTestJS, ArrowFunctions) { " return a;\n" "};"); verifyFormat("var x = (a) => a;"); - verifyFormat("var x = (a) => a;"); + + // FIXME: This is bad, we should be wrapping before "() => {". + verifyFormat("someFunction(() => {\n" + " doSomething(); // break\n" + "})\n" + " .doSomethingElse(\n" + " // break\n" + " );"); } TEST_F(FormatTestJS, ReturnStatements) {