aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/SortJavaScriptImports.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2022-07-03 14:10:23 +0000
committerDimitry Andric <dim@FreeBSD.org>2022-07-03 14:10:23 +0000
commit145449b1e420787bb99721a429341fa6be3adfb6 (patch)
tree1d56ae694a6de602e348dd80165cf881a36600ed /clang/lib/Format/SortJavaScriptImports.cpp
parentecbca9f5fb7d7613d2b94982c4825eb0d33d6842 (diff)
Diffstat (limited to 'clang/lib/Format/SortJavaScriptImports.cpp')
-rw-r--r--clang/lib/Format/SortJavaScriptImports.cpp26
1 files changed, 15 insertions, 11 deletions
diff --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp
index 71326163f45a..c9de4868bf84 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -107,12 +107,13 @@ bool operator<(const JsModuleReference &LHS, const JsModuleReference &RHS) {
if (LHS.Category != RHS.Category)
return LHS.Category < RHS.Category;
if (LHS.Category == JsModuleReference::ReferenceCategory::SIDE_EFFECT ||
- LHS.Category == JsModuleReference::ReferenceCategory::ALIAS)
+ 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())
return LHS.URL.empty() < RHS.URL.empty();
@@ -171,8 +172,9 @@ public:
// in a single group.
if (!Reference.IsExport &&
(Reference.IsExport != References[I + 1].IsExport ||
- Reference.Category != References[I + 1].Category))
+ Reference.Category != References[I + 1].Category)) {
ReferencesText += "\n";
+ }
}
}
llvm::StringRef PreviousText = getSourceText(InsertionPoint);
@@ -187,15 +189,16 @@ public:
// harmless and will be stripped by the subsequent formatting pass.
// FIXME: A better long term fix is to re-calculate Ranges after sorting.
unsigned PreviousSize = PreviousText.size();
- while (ReferencesText.size() < PreviousSize) {
+ while (ReferencesText.size() < PreviousSize)
ReferencesText += " ";
- }
// Separate references from the main code body of the file.
if (FirstNonImportLine && FirstNonImportLine->First->NewlinesBefore < 2 &&
!(FirstNonImportLine->First->is(tok::comment) &&
- FirstNonImportLine->First->TokenText.trim() == "// clang-format on"))
+ FirstNonImportLine->First->TokenText.trim() ==
+ "// clang-format on")) {
ReferencesText += "\n";
+ }
LLVM_DEBUG(llvm::dbgs() << "Replacing imports:\n"
<< PreviousText << "\nwith:\n"
@@ -390,11 +393,12 @@ private:
Current = Current->Next;
}
skipComments();
- if (Start.isInvalid() || References.empty())
+ if (Start.isInvalid() || References.empty()) {
// After the first file level comment, consider line comments to be part
// of the import that immediately follows them by using the previously
// set Start.
Start = Line->First->Tok.getLocation();
+ }
if (!Current) {
// Only comments on this line. Could be the first non-import line.
FirstNonImportLine = Line;
@@ -463,13 +467,14 @@ private:
// URL = TokenText without the quotes.
Reference.URL =
Current->TokenText.substr(1, Current->TokenText.size() - 2);
- if (Reference.URL.startswith(".."))
+ if (Reference.URL.startswith("..")) {
Reference.Category =
JsModuleReference::ReferenceCategory::RELATIVE_PARENT;
- else if (Reference.URL.startswith("."))
+ } else if (Reference.URL.startswith(".")) {
Reference.Category = JsModuleReference::ReferenceCategory::RELATIVE;
- else
+ } else {
Reference.Category = JsModuleReference::ReferenceCategory::ABSOLUTE;
+ }
}
return true;
}
@@ -511,9 +516,8 @@ private:
nextToken();
while (Current->is(tok::identifier)) {
nextToken();
- if (Current->is(tok::semi)) {
+ if (Current->is(tok::semi))
return true;
- }
if (!Current->is(tok::period))
return false;
nextToken();