clang-format: [JS] don't sort named imports if off.
authorMartin Probst <martin@probst.io>
Fri, 11 Jun 2021 09:43:00 +0000 (11:43 +0200)
committerMartin Probst <martin@probst.io>
Fri, 11 Jun 2021 10:02:33 +0000 (12:02 +0200)
The previous implementation would accidentally still sort the individual
named imports, even if the module reference was in a clang-format off
block.

Differential Revision: https://reviews.llvm.org/D104101

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

index 4b88ece..901204c 100644 (file)
@@ -317,6 +317,11 @@ private:
 
   // Appends ``Reference`` to ``Buffer``.
   void appendReference(std::string &Buffer, JsModuleReference &Reference) {
+    if (Reference.FormattingOff) {
+      Buffer +=
+          getSourceText(Reference.Range.getBegin(), Reference.Range.getEnd());
+      return;
+    }
     // Sort the individual symbols within the import.
     // E.g. `import {b, a} from 'x';` -> `import {a, b} from 'x';`
     SmallVector<JsImportedSymbol, 1> Symbols = Reference.Symbols;
index 031dada..4b42637 100644 (file)
@@ -431,6 +431,17 @@ TEST_F(SortImportsTestJS, RespectsClangFormatOff) {
              "// clang-format off\n");
 }
 
+TEST_F(SortImportsTestJS, RespectsClangFormatOffInNamedImports) {
+  verifySort("// clang-format off\n"
+             "import {B, A} from './b';\n"
+             "// clang-format on\n"
+             "const x = 1;",
+             "// clang-format off\n"
+             "import {B, A} from './b';\n"
+             "// clang-format on\n"
+             "const x =   1;");
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang