From: Martin Probst Date: Mon, 29 May 2017 08:41:11 +0000 (+0000) Subject: clang-format: [JS] do not clean up duplicated commas. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec038a4a53332cd1b2d9dc1379d6e654a107399a;p=clang clang-format: [JS] do not clean up duplicated commas. Summary: In JavaScript, duplicated commas have semantic meaning. x = [a,,b]; The statement above creates an array with three entries, the middle being undefined. Because clang-format should not change semantics, disable this cleanup in JS. Reviewers: djasper Subscribers: klimek Differential Revision: https://reviews.llvm.org/D33641 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304141 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 4fa0012f51..2ef6516e02 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1910,6 +1910,9 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef Ranges, StringRef FileName) { + // cleanups only apply to C++ (they mostly concern ctor commas etc.) + if (Style.Language != FormatStyle::LK_Cpp) + return tooling::Replacements(); std::unique_ptr Env = Environment::CreateVirtualEnvironment(Code, FileName, Ranges); Cleaner Clean(*Env, Style); diff --git a/unittests/Format/CleanupTest.cpp b/unittests/Format/CleanupTest.cpp index e554a81912..708fdf8968 100644 --- a/unittests/Format/CleanupTest.cpp +++ b/unittests/Format/CleanupTest.cpp @@ -36,11 +36,12 @@ protected: // Returns code after cleanup around \p Offsets. std::string cleanupAroundOffsets(llvm::ArrayRef Offsets, - llvm::StringRef Code) { + llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle()) { std::vector Ranges; for (auto Offset : Offsets) Ranges.push_back(tooling::Range(Offset, 0)); - return cleanup(Code, Ranges); + return cleanup(Code, Ranges, Style); } }; @@ -171,6 +172,14 @@ TEST_F(CleanupTest, ListRedundantComma) { EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code)); } +TEST_F(CleanupTest, NoCleanupsForJavaScript) { + std::string Code = "function f() { var x = [a, b, , c]; }"; + std::string Expected = "function f() { var x = [a, b, , c]; }"; + const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript); + + EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code, Style)); +} + TEST_F(CleanupTest, TrailingCommaInParens) { std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}"; std::string Expected = "int main() { f(1,2,3,f(1,2),4);}";