From: Martin Probst Date: Mon, 19 Sep 2016 07:02:34 +0000 (+0000) Subject: clang-format: [JS] Fix line breaks before comments when sorting imports. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=812eb3cb09fe20a516bd43cab24a90ee238dc505;p=clang 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281888 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/SortJavaScriptImports.cpp b/lib/Format/SortJavaScriptImports.cpp index 42089c522e..93cda49140 100644 --- a/lib/Format/SortJavaScriptImports.cpp +++ b/lib/Format/SortJavaScriptImports.cpp @@ -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({ diff --git a/unittests/Format/SortImportsTestJS.cpp b/unittests/Format/SortImportsTestJS.cpp index 2bb35a2de0..7e766e1969 100644 --- a/unittests/Format/SortImportsTestJS.cpp +++ b/unittests/Format/SortImportsTestJS.cpp @@ -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) {