From: Daniel Jasper Date: Thu, 22 May 2014 09:10:04 +0000 (+0000) Subject: clang-format: [JS] Understand line breaks in concatenated strings. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=25c29908626548cee8a40bd153bde2b4f98490d3;p=clang clang-format: [JS] Understand line breaks in concatenated strings. Before: var literal = 'hello ' + 'world'; After: var literal = 'hello ' + 'world'; There is no reason to concatenated two string literals with a '+' unless the line break is intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209413 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index f3d655ace5..ce847d6427 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1622,6 +1622,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, BeforeClosingBrace->isOneOf(tok::comma, tok::comment)) return true; + if (Style.Language == FormatStyle::LK_JavaScript) { + // FIXME: This might apply to other languages and token kinds. + if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && + Left.Previous->is(tok::char_constant)) + return true; + } + return false; } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 33bfe06e5f..ecf4e69999 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -164,6 +164,11 @@ TEST_F(FormatTestJS, TryCatch) { "}"); } +TEST_F(FormatTestJS, StringLiteralConcatenation) { + verifyFormat("var literal = 'hello ' +\n" + " 'world';"); +} + TEST_F(FormatTestJS, RegexLiteralClassification) { // Regex literals. verifyFormat("var regex = /abc/;");