[clang] 63042d4 - clang-format: [JS] don't sort named imports if off.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 11 03:03:07 PDT 2021


Author: Martin Probst
Date: 2021-06-11T12:02:33+02:00
New Revision: 63042d46bb0c2481a8b7aa1c324405c2720b3603

URL: https://github.com/llvm/llvm-project/commit/63042d46bb0c2481a8b7aa1c324405c2720b3603
DIFF: https://github.com/llvm/llvm-project/commit/63042d46bb0c2481a8b7aa1c324405c2720b3603.diff

LOG: clang-format: [JS] don't sort named imports if off.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp
index 4b88ece02a109..901204c297f9d 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -317,6 +317,11 @@ class JavaScriptImportSorter : public TokenAnalyzer {
 
   // 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;

diff  --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp
index 031dadaaa7a22..4b426375cfea8 100644
--- a/clang/unittests/Format/SortImportsTestJS.cpp
+++ b/clang/unittests/Format/SortImportsTestJS.cpp
@@ -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


        


More information about the cfe-commits mailing list