]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Fix line breaks before comments when sorting imports.
authorMartin Probst <martin@probst.io>
Mon, 19 Sep 2016 07:02:34 +0000 (07:02 +0000)
committerMartin Probst <martin@probst.io>
Mon, 19 Sep 2016 07:02:34 +0000 (07:02 +0000)
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

lib/Format/SortJavaScriptImports.cpp
unittests/Format/SortImportsTestJS.cpp

index 42089c522ec8f2201d0db6b4c4a3e40243a64601..93cda49140220938d0467fe2a7fb54a3ba00a03e 100644 (file)
@@ -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({
index 2bb35a2de08780e5bbc362fa8854ca9097e1dbec..7e766e1969e1232d41cd369391135b91695a7bed 100644 (file)
@@ -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) {