[clang] 3d4a603 - clang-format: [JS] do not merge imports and exports.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 20 04:08:25 PDT 2021


Author: Martin Probst
Date: 2021-04-20T13:08:18+02:00
New Revision: 3d4a6037ff462bc3e41e4924c603f0e3dfc2c06a

URL: https://github.com/llvm/llvm-project/commit/3d4a6037ff462bc3e41e4924c603f0e3dfc2c06a
DIFF: https://github.com/llvm/llvm-project/commit/3d4a6037ff462bc3e41e4924c603f0e3dfc2c06a.diff

LOG: clang-format: [JS] do not merge imports and exports.

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

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 b7df1a5f1b539..a6a706f15f281 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -271,6 +271,7 @@ class JavaScriptImportSorter : public TokenAnalyzer {
       //   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() ||

diff  --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp
index a0bd76877b9e6..784238ef6ce7c 100644
--- a/clang/unittests/Format/SortImportsTestJS.cpp
+++ b/clang/unittests/Format/SortImportsTestJS.cpp
@@ -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


        


More information about the cfe-commits mailing list