Skip to content

Commit 265ed68

Browse files
committed
[clang-format] Fix a JavaScript import order bug
When the imported symbol is exactly "template" the sorting is disabled. "import {template} from x;" is not recognized as an import. Differential Revision: https://reviews.llvm.org/D61663
1 parent 7523b89 commit 265ed68

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

clang/lib/Format/SortJavaScriptImports.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,12 @@ class JavaScriptImportSorter : public TokenAnalyzer {
548548
nextToken();
549549
if (Current->is(tok::r_brace))
550550
break;
551-
bool isTypeOnly =
552-
Current->is(Keywords.kw_type) && Current->Next &&
553-
Current->Next->isOneOf(tok::identifier, tok::kw_default);
554-
if (!isTypeOnly && !Current->isOneOf(tok::identifier, tok::kw_default))
551+
auto IsIdentifier = [](const auto *Tok) {
552+
return Tok->isOneOf(tok::identifier, tok::kw_default, tok::kw_template);
553+
};
554+
bool isTypeOnly = Current->is(Keywords.kw_type) && Current->Next &&
555+
IsIdentifier(Current->Next);
556+
if (!isTypeOnly && !IsIdentifier(Current))
555557
return false;
556558

557559
JsImportedSymbol Symbol;
@@ -565,7 +567,7 @@ class JavaScriptImportSorter : public TokenAnalyzer {
565567

566568
if (Current->is(Keywords.kw_as)) {
567569
nextToken();
568-
if (!Current->isOneOf(tok::identifier, tok::kw_default))
570+
if (!IsIdentifier(Current))
569571
return false;
570572
Symbol.Alias = Current->TokenText;
571573
nextToken();

clang/unittests/Format/SortImportsTestJS.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,14 @@ TEST_F(SortImportsTestJS, ImportExportType) {
514514
"export {Y};\n");
515515
}
516516

517+
TEST_F(SortImportsTestJS, TemplateKeyword) {
518+
// Reproduces issue where importing "template" disables imports sorting.
519+
verifySort("import {template} from './a';\n"
520+
"import {b} from './b';\n",
521+
"import {b} from './b';\n"
522+
"import {template} from './a';");
523+
}
524+
517525
} // end namespace
518526
} // end namespace format
519527
} // end namespace clang

0 commit comments

Comments
 (0)