]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] handle default bindings in imports.
authorMartin Probst <martin@probst.io>
Fri, 2 Sep 2016 14:06:32 +0000 (14:06 +0000)
committerMartin Probst <martin@probst.io>
Fri, 2 Sep 2016 14:06:32 +0000 (14:06 +0000)
Summary:
Default imports appear outside of named bindings in curly braces:

  import A from 'a';
  import A, {symbol} from 'a';

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@280486 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 72da52bf16bde7e4874d9a8254b6614177281666..2a90e9ab760464f1254ec763dc773bf634226175 100644 (file)
@@ -346,7 +346,6 @@ private:
 
     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 @@ private:
     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 @@ private:
       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 e43844c49cb14df662096633e69d2a582a0c365d..2bb35a2de08780e5bbc362fa8854ca9097e1dbec 100644 (file)
@@ -70,6 +70,26 @@ TEST_F(SortImportsTestJS, BasicSorting) {
              "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"