[clang] 265ed68 - [clang-format] Fix a JavaScript import order bug

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 25 17:07:00 PDT 2023


Author: Owen Pan
Date: 2023-10-25T17:06:53-07:00
New Revision: 265ed6819409a9d76f112a601d48b971904921c8

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

LOG: [clang-format] Fix a JavaScript import order bug

When the imported symbol is exactly "template" the sorting is disabled.
"import {template} from x;" is not recognized as an import.

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

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 9afe85aeed437ef..8c6722e915344fb 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -548,10 +548,12 @@ class JavaScriptImportSorter : public TokenAnalyzer {
       nextToken();
       if (Current->is(tok::r_brace))
         break;
-      bool isTypeOnly =
-          Current->is(Keywords.kw_type) && Current->Next &&
-          Current->Next->isOneOf(tok::identifier, tok::kw_default);
-      if (!isTypeOnly && !Current->isOneOf(tok::identifier, tok::kw_default))
+      auto IsIdentifier = [](const auto *Tok) {
+        return Tok->isOneOf(tok::identifier, tok::kw_default, tok::kw_template);
+      };
+      bool isTypeOnly = Current->is(Keywords.kw_type) && Current->Next &&
+                        IsIdentifier(Current->Next);
+      if (!isTypeOnly && !IsIdentifier(Current))
         return false;
 
       JsImportedSymbol Symbol;
@@ -565,7 +567,7 @@ class JavaScriptImportSorter : public TokenAnalyzer {
 
       if (Current->is(Keywords.kw_as)) {
         nextToken();
-        if (!Current->isOneOf(tok::identifier, tok::kw_default))
+        if (!IsIdentifier(Current))
           return false;
         Symbol.Alias = Current->TokenText;
         nextToken();

diff  --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp
index 2778d6efcdf9a3f..c724fcc073f59e2 100644
--- a/clang/unittests/Format/SortImportsTestJS.cpp
+++ b/clang/unittests/Format/SortImportsTestJS.cpp
@@ -514,6 +514,14 @@ TEST_F(SortImportsTestJS, ImportExportType) {
              "export {Y};\n");
 }
 
+TEST_F(SortImportsTestJS, TemplateKeyword) {
+  // Reproduces issue where importing "template" disables imports sorting.
+  verifySort("import {template} from './a';\n"
+             "import {b} from './b';\n",
+             "import {b} from './b';\n"
+             "import {template} from './a';");
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang


        


More information about the cfe-commits mailing list