[PATCH] D36132: clang-format: [JS] support default imports.

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 01:26:49 PDT 2017


mprobst created this revision.
Herald added a subscriber: klimek.

Formerly, `import {default as X} from y;` would not be recognized as an import.


https://reviews.llvm.org/D36132

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


Index: unittests/Format/SortImportsTestJS.cpp
===================================================================
--- unittests/Format/SortImportsTestJS.cpp
+++ unittests/Format/SortImportsTestJS.cpp
@@ -300,6 +300,14 @@
              "1;");
 }
 
+TEST_F(SortImportsTestJS, SortDefaultImports) {
+  // Reproduces issue where multi-line import was not parsed correctly.
+  verifySort("import {A} from 'a';\n"
+             "import {default as B} from 'b';\n",
+             "import {default as B} from 'b';\n"
+             "import {A} from 'a';\n");
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: lib/Format/SortJavaScriptImports.cpp
===================================================================
--- lib/Format/SortJavaScriptImports.cpp
+++ lib/Format/SortJavaScriptImports.cpp
@@ -413,7 +413,7 @@
       nextToken();
       if (Current->is(tok::r_brace))
         break;
-      if (Current->isNot(tok::identifier))
+      if (Current->isNot(tok::identifier) && Current->isNot(tok::kw_default))
         return false;
 
       JsImportedSymbol Symbol;
@@ -425,7 +425,7 @@
 
       if (Current->is(Keywords.kw_as)) {
         nextToken();
-        if (Current->isNot(tok::identifier))
+        if (Current->isNot(tok::identifier) && Current->isNot(tok::kw_default))
           return false;
         Symbol.Alias = Current->TokenText;
         nextToken();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36132.109059.patch
Type: text/x-patch
Size: 1400 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170801/805d63df/attachment-0001.bin>


More information about the cfe-commits mailing list