[clang] e1f34b7 - clang-format: [JS] terminate import sorting on `export type X = Y`

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Mon May 15 06:38:07 PDT 2023


Author: Jan Kuhle
Date: 2023-05-15T15:37:18+02:00
New Revision: e1f34b735b669c5978bfdead874ee59d33f99eb0

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

LOG: clang-format: [JS] terminate import sorting on `export type X = Y`

Contributed by @jankuehle!

https://reviews.llvm.org/D150116 introduced a bug. `export type X = Y` was considered an export declaration and took part in import sorting. This is not correct. With this change `export type X = Y` properly terminates import sorting.

Reviewed By: krasimir

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

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 af2d3400c970d..e24fe051a26ce 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -517,7 +517,7 @@ class JavaScriptImportSorter : public TokenAnalyzer {
     }
 
     // eat a potential "import X, " prefix.
-    if (Current->is(tok::identifier)) {
+    if (!Reference.IsExport && Current->is(tok::identifier)) {
       Reference.DefaultImport = Current->TokenText;
       nextToken();
       if (Current->is(Keywords.kw_from))

diff  --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp
index 9d779cd988d0c..2778d6efcdf9a 100644
--- a/clang/unittests/Format/SortImportsTestJS.cpp
+++ b/clang/unittests/Format/SortImportsTestJS.cpp
@@ -503,6 +503,15 @@ TEST_F(SortImportsTestJS, ImportExportType) {
   verifySort("export {A, type B} from 'foo';\n",
              "export {A} from 'foo';\n"
              "export   {type B} from 'foo';");
+
+  // `export type X = Y;` should terminate import sorting. The following export
+  // statements should therefore not merge.
+  verifySort("export type A = B;\n"
+             "export {X};\n"
+             "export {Y};\n",
+             "export type A = B;\n"
+             "export {X};\n"
+             "export {Y};\n");
 }
 
 } // end namespace


        


More information about the cfe-commits mailing list