24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/Support/Debug.h" 30 #define DEBUG_TYPE "format-formatter" 35 class FormatTokenLexer;
73 bool IsExport =
false;
99 if (LHS.
Category == JsModuleReference::ReferenceCategory::SIDE_EFFECT)
105 if (LHS.
URL.empty() != RHS.
URL.empty())
106 return LHS.
URL.empty() < RHS.
URL.empty();
107 if (
int Res = LHS.
URL.compare_lower(RHS.
URL))
124 FileContents(Env.getSourceManager().getBufferData(Env.getFileID())) {}
126 std::pair<tooling::Replacements, unsigned>
131 AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
136 std::tie(References, FirstNonImportLine) =
137 parseModuleReferences(Keywords, AnnotatedLines);
139 if (References.empty())
143 for (
unsigned i = 0, e = References.size(); i != e; ++i)
144 Indices.push_back(i);
145 std::stable_sort(Indices.begin(), Indices.end(),
146 [&](
unsigned LHSI,
unsigned RHSI) {
147 return References[LHSI] < References[RHSI];
149 bool ReferencesInOrder = std::is_sorted(Indices.begin(), Indices.end());
151 std::string ReferencesText;
152 bool SymbolsInOrder =
true;
153 for (
unsigned i = 0, e = Indices.size(); i != e; ++i) {
155 if (appendReference(ReferencesText, Reference))
156 SymbolsInOrder =
false;
159 ReferencesText +=
"\n";
163 (Reference.
IsExport != References[Indices[i + 1]].IsExport ||
164 Reference.
Category != References[Indices[i + 1]].Category))
165 ReferencesText +=
"\n";
169 if (ReferencesInOrder && SymbolsInOrder)
173 InsertionPoint.
setEnd(References[References.size() - 1].Range.getEnd());
182 unsigned PreviousSize = getSourceText(InsertionPoint).size();
183 while (ReferencesText.size() < PreviousSize) {
184 ReferencesText +=
" ";
189 ReferencesText +=
"\n";
191 LLVM_DEBUG(llvm::dbgs() <<
"Replacing imports:\n" 192 << getSourceText(InsertionPoint) <<
"\nwith:\n" 193 << ReferencesText <<
"\n");
213 StringRef FileContents;
215 void skipComments() { Current = skipComments(Current); }
218 while (Tok && Tok->
is(tok::comment))
224 Current = Current->
Next;
226 if (!Current || Current == LineEnd->
Next) {
230 Current = &invalidToken;
251 Symbols.begin(), Symbols.end(),
253 return LHS.
Symbol.compare_lower(RHS.Symbol) < 0;
255 if (Symbols == Reference.
Symbols) {
257 StringRef ReferenceStmt = getSourceText(Reference.
Range);
258 Buffer += ReferenceStmt;
264 Buffer += getSourceText(Reference.
Range.
getBegin(), SymbolsStart);
266 for (
auto I = Symbols.begin(), E = Symbols.end(); I != E; ++I) {
267 if (I != Symbols.begin())
269 Buffer += getSourceText(I->Range);
272 Buffer += getSourceText(SymbolsEnd, Reference.
Range.
getEnd());
279 std::pair<SmallVector<JsModuleReference, 16>,
AnnotatedLine *>
285 bool AnyImportAffected =
false;
286 for (
auto Line : AnnotatedLines) {
287 Current =
Line->First;
288 LineEnd =
Line->Last;
290 if (Start.
isInvalid() || References.empty())
294 Start =
Line->First->Tok.getLocation();
297 FirstNonImportLine =
Line;
302 if (!parseModuleReference(Keywords, Reference)) {
303 if (!FirstNonImportLine)
304 FirstNonImportLine =
Line;
307 FirstNonImportLine =
nullptr;
308 AnyImportAffected = AnyImportAffected ||
Line->Affected;
311 llvm::dbgs() <<
"JsModuleReference: {" 312 <<
"is_export: " << Reference.
IsExport 314 <<
", url: " << Reference.
URL 315 <<
", prefix: " << Reference.
Prefix;
316 for (
size_t i = 0; i < Reference.
Symbols.size(); ++i)
317 llvm::dbgs() <<
", " << Reference.
Symbols[i].Symbol <<
" as " 319 llvm::dbgs() <<
", text: " << getSourceText(Reference.
Range);
320 llvm::dbgs() <<
"}\n";
322 References.push_back(Reference);
326 if (!AnyImportAffected)
328 return std::make_pair(References, FirstNonImportLine);
338 Reference.
IsExport = Current->
is(tok::kw_export);
343 Reference.
Category = JsModuleReference::ReferenceCategory::SIDE_EFFECT;
349 if (!parseModuleBindings(Keywords, Reference))
360 if (Reference.
URL.startswith(
".."))
362 JsModuleReference::ReferenceCategory::RELATIVE_PARENT;
363 else if (Reference.
URL.startswith(
"."))
364 Reference.
Category = JsModuleReference::ReferenceCategory::RELATIVE;
366 Reference.
Category = JsModuleReference::ReferenceCategory::ABSOLUTE;
369 Reference.
Category = JsModuleReference::ReferenceCategory::RELATIVE;
376 if (parseStarBinding(Keywords, Reference))
378 return parseNamedBindings(Keywords, Reference);
384 if (Current->
isNot(tok::star))
390 if (Current->
isNot(tok::identifier))
399 if (Current->
is(tok::identifier)) {
403 if (Current->
isNot(tok::comma))
407 if (Current->
isNot(tok::l_brace))
411 while (Current->
isNot(tok::r_brace)) {
413 if (Current->
is(tok::r_brace))
415 if (!Current->
isOneOf(tok::identifier, tok::kw_default))
425 if (Current->
is(Keywords.
kw_as)) {
427 if (!Current->
isOneOf(tok::identifier, tok::kw_default))
433 Reference.
Symbols.push_back(Symbol);
435 if (!Current->
isOneOf(tok::r_brace, tok::comma))
446 StringRef FileName) {
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
Defines the SourceManager interface.
void setBegin(SourceLocation b)
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
This file implements a token annotator, i.e.
void setKind(tok::TokenKind K)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Defines the Diagnostic-related interfaces.
const AnnotatedLine * Line
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
SourceLocation getEnd() const
static CharSourceRange getCharRange(SourceRange R)
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
Encodes a location in the source.
Dataflow Directional Tag Classes.
This file implements a sorter for JavaScript ES6 imports.
This file declares an abstract TokenAnalyzer, and associated helper classes.
Defines the clang::SourceLocation class and associated facilities.
void setEnd(SourceLocation e)
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
This class handles loading and caching of source files into memory.
SourceLocation getEndLoc() const