Skip to content

Commit 6918dca

Browse files
committed
clang-format: [JS] handle default bindings in imports.
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 llvm-svn: 280486
1 parent b480ffb commit 6918dca

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

clang/lib/Format/SortJavaScriptImports.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ class JavaScriptImportSorter : public TokenAnalyzer {
346346

347347
if (!parseModuleBindings(Keywords, Reference))
348348
return false;
349-
nextToken();
350349

351350
if (Current->is(Keywords.kw_from)) {
352351
// imports have a 'from' clause, exports might not.
@@ -389,19 +388,28 @@ class JavaScriptImportSorter : public TokenAnalyzer {
389388
if (Current->isNot(tok::identifier))
390389
return false;
391390
Reference.Prefix = Current->TokenText;
391+
nextToken();
392392
return true;
393393
}
394394

395395
bool parseNamedBindings(const AdditionalKeywords &Keywords,
396396
JsModuleReference &Reference) {
397+
if (Current->is(tok::identifier)) {
398+
nextToken();
399+
if (Current->is(Keywords.kw_from))
400+
return true;
401+
if (Current->isNot(tok::comma))
402+
return false;
403+
nextToken(); // eat comma.
404+
}
397405
if (Current->isNot(tok::l_brace))
398406
return false;
399407

400408
// {sym as alias, sym2 as ...} from '...';
401-
nextToken();
402-
while (true) {
409+
while (Current->isNot(tok::r_brace)) {
410+
nextToken();
403411
if (Current->is(tok::r_brace))
404-
return true;
412+
break;
405413
if (Current->isNot(tok::identifier))
406414
return false;
407415

@@ -422,12 +430,11 @@ class JavaScriptImportSorter : public TokenAnalyzer {
422430
Symbol.Range.setEnd(Current->Tok.getLocation());
423431
Reference.Symbols.push_back(Symbol);
424432

425-
if (Current->is(tok::r_brace))
426-
return true;
427-
if (Current->isNot(tok::comma))
433+
if (!Current->isOneOf(tok::r_brace, tok::comma))
428434
return false;
429-
nextToken();
430435
}
436+
nextToken(); // consume r_brace
437+
return true;
431438
}
432439
};
433440

clang/unittests/Format/SortImportsTestJS.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ TEST_F(SortImportsTestJS, BasicSorting) {
7070
"let x = 1;");
7171
}
7272

73+
TEST_F(SortImportsTestJS, DefaultBinding) {
74+
verifySort("import A from 'a';\n"
75+
"import B from 'b';\n"
76+
"\n"
77+
"let x = 1;",
78+
"import B from 'b';\n"
79+
"import A from 'a';\n"
80+
"let x = 1;");
81+
}
82+
83+
TEST_F(SortImportsTestJS, DefaultAndNamedBinding) {
84+
verifySort("import A, {a} from 'a';\n"
85+
"import B, {b} from 'b';\n"
86+
"\n"
87+
"let x = 1;",
88+
"import B, {b} from 'b';\n"
89+
"import A, {a} from 'a';\n"
90+
"let x = 1;");
91+
}
92+
7393
TEST_F(SortImportsTestJS, WrappedImportStatements) {
7494
verifySort("import {sym1, sym2} from 'a';\n"
7595
"import {sym} from 'b';\n"

0 commit comments

Comments
 (0)