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

Jan Kühle via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 15 05:58:06 PDT 2023


jankuehle created this revision.
jankuehle added projects: clang, clang-format.
Herald added a project: All.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
jankuehle requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150563

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


Index: clang/unittests/Format/SortImportsTestJS.cpp
===================================================================
--- clang/unittests/Format/SortImportsTestJS.cpp
+++ clang/unittests/Format/SortImportsTestJS.cpp
@@ -503,6 +503,15 @@
   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
Index: clang/lib/Format/SortJavaScriptImports.cpp
===================================================================
--- clang/lib/Format/SortJavaScriptImports.cpp
+++ clang/lib/Format/SortJavaScriptImports.cpp
@@ -517,7 +517,7 @@
     }
 
     // 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))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150563.522148.patch
Type: text/x-patch
Size: 1234 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230515/516f9326/attachment-0001.bin>


More information about the cfe-commits mailing list