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();


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();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to