From: Daniel Jasper Date: Fri, 12 Jun 2015 04:52:02 +0000 (+0000) Subject: clang-format: [JS] fix incorrectly collapsed lines after export X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6281b76cbda247396d26ebaaa135655e2ec0e965;p=clang clang-format: [JS] fix incorrectly collapsed lines after export statement. When an exported function would follow a class declaration, it would not be recognized as a stand-alone function. That would then collapse the following line with the current one, e.g. class C {} export function f() {} var x; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239592 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 6ad43294ca..7f5df7dfba 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -785,9 +785,15 @@ void UnwrappedLineParser::parseStructuralElement() { case tok::kw_struct: case tok::kw_union: case tok::kw_class: + // parseRecord falls through and does not yet add an unwrapped line as a + // record declaration or definition can start a structural element. parseRecord(); - // A record declaration or definition is always the start of a structural - // element. + // This does not apply for Java and JavaScript. + if (Style.Language == FormatStyle::LK_Java || + Style.Language == FormatStyle::LK_JavaScript) { + addUnwrappedLine(); + return; + } break; case tok::period: nextToken(); @@ -1626,10 +1632,6 @@ void UnwrappedLineParser::parseRecord() { // We fall through to parsing a structural element afterwards, so // class A {} n, m; // will end up in one unwrapped line. - // This does not apply for Java and JavaScript. - if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) - addUnwrappedLine(); } void UnwrappedLineParser::parseObjCProtocolList() { diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 8f7202eeb2..e01637bfad 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -726,6 +726,9 @@ TEST_F(FormatTestJS, Modules) { verifyFormat("export default class X { y: number }"); verifyFormat("export default function() {\n return 1;\n}"); verifyFormat("export var x = 12;"); + verifyFormat("class C {}\n" + "export function f() {}\n" + "var v;"); verifyFormat("export var x: number = 12;"); verifyFormat("export const y = {\n" " a: 1,\n"