clang-format: [JS] do not merge imports and exports.
authorMartin Probst <martin@probst.io>
Mon, 19 Apr 2021 11:31:06 +0000 (13:31 +0200)
committerMartin Probst <martin@probst.io>
Tue, 20 Apr 2021 11:08:18 +0000 (13:08 +0200)
Previously, clang-format would erroneously merge import and export
statements. These need to be kept separate, as the semantics differ.

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

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

index b7df1a5..a6a706f 100644 (file)
@@ -271,6 +271,7 @@ private:
       //   import Default from 'foo'; on either previous or this.
       //   mismatching
       if (Reference->Category == JsModuleReference::SIDE_EFFECT ||
+          Reference->IsExport != PreviousReference->IsExport ||
           !PreviousReference->Prefix.empty() || !Reference->Prefix.empty() ||
           !PreviousReference->DefaultImport.empty() ||
           !Reference->DefaultImport.empty() || Reference->Symbols.empty() ||
index a0bd768..784238e 100644 (file)
@@ -355,6 +355,15 @@ TEST_F(SortImportsTestJS, MergeImports) {
              "import {/* x */ X} from 'a';\n"
              "\n"
              "X + Y + Z;\n");
+
+  // do not merge imports and exports
+  verifySort("import {A} from 'foo';\n"
+             "export {B} from 'foo';",
+             "import {A} from 'foo';\n"
+             "export   {B} from 'foo';");
+  // do merge exports
+  verifySort("export {A, B} from 'foo';\n", "export {A} from 'foo';\n"
+                                            "export   {B} from 'foo';");
 }
 
 } // end namespace