This revision was automatically updated to reflect the committed changes.
Closed by commit rL280486: clang-format: [JS] handle default bindings in 
imports. (authored by mprobst).

Changed prior to commit:
  https://reviews.llvm.org/D23973?vs=69514&id=70159#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D23973

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

Index: cfe/trunk/lib/Format/SortJavaScriptImports.cpp
===================================================================
--- cfe/trunk/lib/Format/SortJavaScriptImports.cpp
+++ cfe/trunk/lib/Format/SortJavaScriptImports.cpp
@@ -346,7 +346,6 @@
 
     if (!parseModuleBindings(Keywords, Reference))
       return false;
-    nextToken();
 
     if (Current->is(Keywords.kw_from)) {
       // imports have a 'from' clause, exports might not.
@@ -389,19 +388,28 @@
     if (Current->isNot(tok::identifier))
       return false;
     Reference.Prefix = Current->TokenText;
+    nextToken();
     return true;
   }
 
   bool parseNamedBindings(const AdditionalKeywords &Keywords,
                           JsModuleReference &Reference) {
+    if (Current->is(tok::identifier)) {
+      nextToken();
+      if (Current->is(Keywords.kw_from))
+        return true;
+      if (Current->isNot(tok::comma))
+        return false;
+      nextToken(); // eat comma.
+    }
     if (Current->isNot(tok::l_brace))
       return false;
 
     // {sym as alias, sym2 as ...} from '...';
-    nextToken();
-    while (true) {
+    while (Current->isNot(tok::r_brace)) {
+      nextToken();
       if (Current->is(tok::r_brace))
-        return true;
+        break;
       if (Current->isNot(tok::identifier))
         return false;
 
@@ -422,12 +430,11 @@
       Symbol.Range.setEnd(Current->Tok.getLocation());
       Reference.Symbols.push_back(Symbol);
 
-      if (Current->is(tok::r_brace))
-        return true;
-      if (Current->isNot(tok::comma))
+      if (!Current->isOneOf(tok::r_brace, tok::comma))
         return false;
-      nextToken();
     }
+    nextToken(); // consume r_brace
+    return true;
   }
 };
 
Index: cfe/trunk/unittests/Format/SortImportsTestJS.cpp
===================================================================
--- cfe/trunk/unittests/Format/SortImportsTestJS.cpp
+++ cfe/trunk/unittests/Format/SortImportsTestJS.cpp
@@ -70,6 +70,26 @@
              "let x = 1;");
 }
 
+TEST_F(SortImportsTestJS, DefaultBinding) {
+  verifySort("import A from 'a';\n"
+             "import B from 'b';\n"
+             "\n"
+             "let x = 1;",
+             "import B from 'b';\n"
+             "import A from 'a';\n"
+             "let x = 1;");
+}
+
+TEST_F(SortImportsTestJS, DefaultAndNamedBinding) {
+  verifySort("import A, {a} from 'a';\n"
+             "import B, {b} from 'b';\n"
+             "\n"
+             "let x = 1;",
+             "import B, {b} from 'b';\n"
+             "import A, {a} from 'a';\n"
+             "let x = 1;");
+}
+
 TEST_F(SortImportsTestJS, WrappedImportStatements) {
   verifySort("import {sym1, sym2} from 'a';\n"
              "import {sym} from 'b';\n"


Index: cfe/trunk/lib/Format/SortJavaScriptImports.cpp
===================================================================
--- cfe/trunk/lib/Format/SortJavaScriptImports.cpp
+++ cfe/trunk/lib/Format/SortJavaScriptImports.cpp
@@ -346,7 +346,6 @@
 
     if (!parseModuleBindings(Keywords, Reference))
       return false;
-    nextToken();
 
     if (Current->is(Keywords.kw_from)) {
       // imports have a 'from' clause, exports might not.
@@ -389,19 +388,28 @@
     if (Current->isNot(tok::identifier))
       return false;
     Reference.Prefix = Current->TokenText;
+    nextToken();
     return true;
   }
 
   bool parseNamedBindings(const AdditionalKeywords &Keywords,
                           JsModuleReference &Reference) {
+    if (Current->is(tok::identifier)) {
+      nextToken();
+      if (Current->is(Keywords.kw_from))
+        return true;
+      if (Current->isNot(tok::comma))
+        return false;
+      nextToken(); // eat comma.
+    }
     if (Current->isNot(tok::l_brace))
       return false;
 
     // {sym as alias, sym2 as ...} from '...';
-    nextToken();
-    while (true) {
+    while (Current->isNot(tok::r_brace)) {
+      nextToken();
       if (Current->is(tok::r_brace))
-        return true;
+        break;
       if (Current->isNot(tok::identifier))
         return false;
 
@@ -422,12 +430,11 @@
       Symbol.Range.setEnd(Current->Tok.getLocation());
       Reference.Symbols.push_back(Symbol);
 
-      if (Current->is(tok::r_brace))
-        return true;
-      if (Current->isNot(tok::comma))
+      if (!Current->isOneOf(tok::r_brace, tok::comma))
         return false;
-      nextToken();
     }
+    nextToken(); // consume r_brace
+    return true;
   }
 };
 
Index: cfe/trunk/unittests/Format/SortImportsTestJS.cpp
===================================================================
--- cfe/trunk/unittests/Format/SortImportsTestJS.cpp
+++ cfe/trunk/unittests/Format/SortImportsTestJS.cpp
@@ -70,6 +70,26 @@
              "let x = 1;");
 }
 
+TEST_F(SortImportsTestJS, DefaultBinding) {
+  verifySort("import A from 'a';\n"
+             "import B from 'b';\n"
+             "\n"
+             "let x = 1;",
+             "import B from 'b';\n"
+             "import A from 'a';\n"
+             "let x = 1;");
+}
+
+TEST_F(SortImportsTestJS, DefaultAndNamedBinding) {
+  verifySort("import A, {a} from 'a';\n"
+             "import B, {b} from 'b';\n"
+             "\n"
+             "let x = 1;",
+             "import B, {b} from 'b';\n"
+             "import A, {a} from 'a';\n"
+             "let x = 1;");
+}
+
 TEST_F(SortImportsTestJS, WrappedImportStatements) {
   verifySort("import {sym1, sym2} from 'a';\n"
              "import {sym} from 'b';\n"
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to