aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/SortJavaScriptImports.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib/Format/SortJavaScriptImports.cpp')
-rw-r--r--clang/lib/Format/SortJavaScriptImports.cpp49
1 files changed, 34 insertions, 15 deletions
diff --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp
index 77dc0d683e5f..e4107525a7ff 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -78,6 +78,7 @@ struct JsModuleReference {
ABSOLUTE, // from 'something'
RELATIVE_PARENT, // from '../*'
RELATIVE, // from './*'
+ ALIAS, // import X = A.B;
};
ReferenceCategory Category = ReferenceCategory::SIDE_EFFECT;
// The URL imported, e.g. `import .. from 'url';`. Empty for `export {a, b};`.
@@ -105,10 +106,12 @@ bool operator<(const JsModuleReference &LHS, const JsModuleReference &RHS) {
return LHS.IsExport < RHS.IsExport;
if (LHS.Category != RHS.Category)
return LHS.Category < RHS.Category;
- if (LHS.Category == JsModuleReference::ReferenceCategory::SIDE_EFFECT)
- // Side effect imports might be ordering sensitive. Consider them equal so
- // that they maintain their relative order in the stable sort below.
- // This retains transitivity because LHS.Category == RHS.Category here.
+ if (LHS.Category == JsModuleReference::ReferenceCategory::SIDE_EFFECT ||
+ LHS.Category == JsModuleReference::ReferenceCategory::ALIAS)
+ // Side effect imports and aliases might be ordering sensitive. Consider
+ // them equal so that they maintain their relative order in the stable sort
+ // below. This retains transitivity because LHS.Category == RHS.Category
+ // here.
return false;
// Empty URLs sort *last* (for export {...};).
if (LHS.URL.empty() != RHS.URL.empty())
@@ -260,13 +263,13 @@ private:
while (Start != References.end() && Start->FormattingOff) {
// Skip over all imports w/ disabled formatting.
ReferencesSorted.push_back(*Start);
- Start++;
+ ++Start;
}
SmallVector<JsModuleReference, 16> SortChunk;
while (Start != References.end() && !Start->FormattingOff) {
// Skip over all imports w/ disabled formatting.
SortChunk.push_back(*Start);
- Start++;
+ ++Start;
}
llvm::stable_sort(SortChunk);
mergeModuleReferences(SortChunk);
@@ -338,10 +341,12 @@ private:
// Stitch together the module reference start...
Buffer += getSourceText(Reference.Range.getBegin(), Reference.SymbolsStart);
// ... then the references in order ...
- for (auto I = Symbols.begin(), E = Symbols.end(); I != E; ++I) {
- if (I != Symbols.begin())
+ if (!Symbols.empty()) {
+ Buffer += getSourceText(Symbols.front().Range);
+ for (const JsImportedSymbol &Symbol : llvm::drop_begin(Symbols)) {
Buffer += ",";
- Buffer += getSourceText(I->Range);
+ Buffer += getSourceText(Symbol.Range);
+ }
}
// ... followed by the module reference end.
Buffer += getSourceText(Reference.SymbolsEnd, Reference.Range.getEnd());
@@ -359,6 +364,7 @@ private:
bool AnyImportAffected = false;
bool FormattingOff = false;
for (auto *Line : AnnotatedLines) {
+ assert(Line->First);
Current = Line->First;
LineEnd = Line->Last;
// clang-format comments toggle formatting on/off.
@@ -395,6 +401,8 @@ private:
JsModuleReference Reference;
Reference.FormattingOff = FormattingOff;
Reference.Range.setBegin(Start);
+ // References w/o a URL, e.g. export {A}, groups with RELATIVE.
+ Reference.Category = JsModuleReference::ReferenceCategory::RELATIVE;
if (!parseModuleReference(Keywords, Reference)) {
if (!FirstNonImportLine)
FirstNonImportLine = Line; // if no comment before.
@@ -410,9 +418,8 @@ private:
<< ", cat: " << Reference.Category
<< ", url: " << Reference.URL
<< ", prefix: " << Reference.Prefix;
- for (size_t I = 0; I < Reference.Symbols.size(); ++I)
- llvm::dbgs() << ", " << Reference.Symbols[I].Symbol << " as "
- << Reference.Symbols[I].Alias;
+ for (const JsImportedSymbol &Symbol : Reference.Symbols)
+ llvm::dbgs() << ", " << Symbol.Symbol << " as " << Symbol.Alias;
llvm::dbgs() << ", text: " << getSourceText(Reference.Range);
llvm::dbgs() << "}\n";
});
@@ -461,9 +468,6 @@ private:
Reference.Category = JsModuleReference::ReferenceCategory::RELATIVE;
else
Reference.Category = JsModuleReference::ReferenceCategory::ABSOLUTE;
- } else {
- // w/o URL groups with "empty".
- Reference.Category = JsModuleReference::ReferenceCategory::RELATIVE;
}
return true;
}
@@ -499,6 +503,21 @@ private:
nextToken();
if (Current->is(Keywords.kw_from))
return true;
+ // import X = A.B.C;
+ if (Current->is(tok::equal)) {
+ Reference.Category = JsModuleReference::ReferenceCategory::ALIAS;
+ nextToken();
+ while (Current->is(tok::identifier)) {
+ nextToken();
+ if (Current->is(tok::semi)) {
+ nextToken();
+ return true;
+ }
+ if (!Current->is(tok::period))
+ return false;
+ nextToken();
+ }
+ }
if (Current->isNot(tok::comma))
return false;
nextToken(); // eat comma.