From 910807d4b9ee588e51e34ccb7804758123a78423 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 12 Jun 2015 04:52:02 +0000 Subject: [PATCH] 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; llvm-svn: 239592 --- clang/lib/Format/UnwrappedLineParser.cpp | 14 ++++++++------ clang/unittests/Format/FormatTestJS.cpp | 3 +++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 6ad4329..7f5df7d 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/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/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 8f7202e..e01637b 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/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" -- 2.7.4