Author: mprobst Date: Mon Sep 19 02:02:34 2016 New Revision: 281888 URL: http://llvm.org/viewvc/llvm-project?rev=281888&view=rev Log: clang-format: [JS] Fix line breaks before comments when sorting imports.
Summary: Previously, clang-format would always insert an additional line break after the import block if the main body started with a comment, due to loosing track of the first non-import line. Reviewers: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D24708 Modified: cfe/trunk/lib/Format/SortJavaScriptImports.cpp cfe/trunk/unittests/Format/SortImportsTestJS.cpp Modified: cfe/trunk/lib/Format/SortJavaScriptImports.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/SortJavaScriptImports.cpp?rev=281888&r1=281887&r2=281888&view=diff ============================================================================== --- cfe/trunk/lib/Format/SortJavaScriptImports.cpp (original) +++ cfe/trunk/lib/Format/SortJavaScriptImports.cpp Mon Sep 19 02:02:34 2016 @@ -293,14 +293,19 @@ private: // of the import that immediately follows them by using the previously // set Start. Start = Line->First->Tok.getLocation(); - if (!Current) - continue; // Only comments on this line. + if (!Current) { + // Only comments on this line. Could be the first non-import line. + FirstNonImportLine = Line; + continue; + } JsModuleReference Reference; Reference.Range.setBegin(Start); if (!parseModuleReference(Keywords, Reference)) { - FirstNonImportLine = Line; + if (!FirstNonImportLine) + FirstNonImportLine = Line; // if no comment before. break; } + FirstNonImportLine = nullptr; AnyImportAffected = AnyImportAffected || Line->Affected; Reference.Range.setEnd(LineEnd->Tok.getEndLoc()); DEBUG({ Modified: cfe/trunk/unittests/Format/SortImportsTestJS.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortImportsTestJS.cpp?rev=281888&r1=281887&r2=281888&view=diff ============================================================================== --- cfe/trunk/unittests/Format/SortImportsTestJS.cpp (original) +++ cfe/trunk/unittests/Format/SortImportsTestJS.cpp Mon Sep 19 02:02:34 2016 @@ -121,6 +121,16 @@ TEST_F(SortImportsTestJS, Comments) { "import {sym} from 'b'; // from //foo:bar\n" "// A very important import follows.\n" "import {sym} from 'a'; /* more comments */\n"); + verifySort("import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "\n" + "/** Comment on variable. */\n" + "const x = 1;\n", + "import {sym} from 'b';\n" + "import {sym} from 'a';\n" + "\n" + "/** Comment on variable. */\n" + "const x = 1;\n"); } TEST_F(SortImportsTestJS, SortStar) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits