17#define DEBUG_TYPE "format-formatter"
22class FormatTokenLexer;
106 if (LHS.
URL.empty() != RHS.
URL.empty())
107 return LHS.
URL.empty() < RHS.
URL.empty();
108 if (
int Res = LHS.
URL.compare_insensitive(RHS.
URL))
125 FileContents(
Env.getSourceManager().getBufferData(
Env.getFileID())) {
130 std::pair<tooling::Replacements, unsigned>
140 std::tie(References, FirstNonImportLine) =
141 parseModuleReferences(Keywords, AnnotatedLines);
143 if (References.empty())
148 InsertionPoint.
setEnd(References[References.size() - 1].Range.getEnd());
150 References = sortModuleReferences(References);
152 std::string ReferencesText;
153 for (
unsigned I = 0, E = References.size(); I != E; ++I) {
155 appendReference(ReferencesText, Reference);
158 ReferencesText +=
"\n";
161 if (!Reference.IsExport &&
162 (Reference.IsExport != References[I + 1].IsExport ||
163 Reference.Category != References[I + 1].Category)) {
164 ReferencesText +=
"\n";
168 llvm::StringRef PreviousText = getSourceText(InsertionPoint);
169 if (ReferencesText == PreviousText)
179 unsigned PreviousSize = PreviousText.size();
180 while (ReferencesText.size() < PreviousSize)
181 ReferencesText +=
" ";
185 !(FirstNonImportLine->
First->
is(tok::comment) &&
187 ReferencesText +=
"\n";
190 LLVM_DEBUG(llvm::dbgs() <<
"Replacing imports:\n"
191 << PreviousText <<
"\nwith:\n"
192 << ReferencesText <<
"\n");
199 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
212 StringRef FileContents;
214 void skipComments() { Current = skipComments(Current); }
216 FormatToken *skipComments(FormatToken *Tok) {
217 while (Tok && Tok->is(tok::comment))
223 Current = Current->Next;
225 if (!Current || Current == LineEnd->
Next) {
228 Current = &invalidToken;
232 StringRef getSourceText(SourceRange Range) {
233 return getSourceText(
Range.getBegin(),
Range.getEnd());
236 StringRef getSourceText(SourceLocation
Begin, SourceLocation End) {
238 return FileContents.substr(
SM.getFileOffset(
Begin),
239 SM.getFileOffset(End) -
SM.getFileOffset(
Begin));
246 SmallVector<JsModuleReference, 16>
247 sortModuleReferences(
const SmallVector<JsModuleReference, 16> &References) {
252 const auto *Start = References.begin();
253 SmallVector<JsModuleReference, 16> ReferencesSorted;
254 while (Start != References.end()) {
255 while (Start != References.end() && Start->FormattingOff) {
257 ReferencesSorted.push_back(*Start);
260 SmallVector<JsModuleReference, 16> SortChunk;
261 while (Start != References.end() && !Start->FormattingOff) {
263 SortChunk.push_back(*Start);
266 llvm::stable_sort(SortChunk);
267 mergeModuleReferences(SortChunk);
268 ReferencesSorted.insert(ReferencesSorted.end(), SortChunk.begin(),
271 return ReferencesSorted;
283 void mergeModuleReferences(SmallVector<JsModuleReference, 16> &References) {
284 if (References.empty())
286 JsModuleReference *PreviousReference = References.begin();
287 auto *
Reference = std::next(References.begin());
288 while (Reference != References.end()) {
296 Reference->IsExport != PreviousReference->IsExport ||
297 Reference->IsTypeOnly != PreviousReference->IsTypeOnly ||
298 !PreviousReference->Prefix.empty() || !
Reference->Prefix.empty() ||
299 !PreviousReference->DefaultImport.empty() ||
301 PreviousReference->URL !=
Reference->URL) {
307 PreviousReference->Symbols.append(
Reference->Symbols);
308 PreviousReference->SymbolsMerged =
true;
315 void appendReference(std::string &Buffer, JsModuleReference &Reference) {
323 SmallVector<JsImportedSymbol, 1> Symbols =
Reference.Symbols;
325 Symbols, [&](
const JsImportedSymbol &LHS,
const JsImportedSymbol &RHS) {
326 return LHS.Symbol.compare_insensitive(RHS.Symbol) < 0;
330 StringRef ReferenceStmt = getSourceText(
Reference.Range);
331 Buffer += ReferenceStmt;
337 if (!Symbols.empty()) {
338 Buffer += getSourceText(Symbols.front().Range);
339 for (
const JsImportedSymbol &Symbol : llvm::drop_begin(Symbols)) {
341 Buffer += getSourceText(Symbol.Range);
351 std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine *>
352 parseModuleReferences(
const AdditionalKeywords &Keywords,
353 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
354 SmallVector<JsModuleReference, 16> References;
355 SourceLocation Start;
356 AnnotatedLine *FirstNonImportLine =
nullptr;
357 bool AnyImportAffected =
false;
358 bool FormattingOff =
false;
359 for (
auto *
Line : AnnotatedLines) {
361 Current =
Line->First;
362 LineEnd =
Line->Last;
365 while (Current && Current->is(tok::comment)) {
366 StringRef CommentText = Current->TokenText.trim();
368 FormattingOff =
true;
370 FormattingOff =
false;
375 if (!References.empty()) {
376 References.back().Range.setEnd(Current->Tok.getEndLoc());
377 Start = Current->Tok.getEndLoc().getLocWithOffset(1);
381 Current = Current->Next;
384 if (Start.isInvalid() || References.empty()) {
388 Start =
Line->First->Tok.getLocation();
392 FirstNonImportLine =
Line;
400 if (!parseModuleReference(Keywords, Reference)) {
401 if (!FirstNonImportLine)
402 FirstNonImportLine =
Line;
405 FirstNonImportLine =
nullptr;
406 AnyImportAffected = AnyImportAffected ||
Line->Affected;
409 llvm::dbgs() <<
"JsModuleReference: {"
410 <<
"formatting_off: " <<
Reference.FormattingOff
415 for (
const JsImportedSymbol &Symbol :
Reference.Symbols)
416 llvm::dbgs() <<
", " << Symbol.Symbol <<
" as " << Symbol.Alias;
417 llvm::dbgs() <<
", text: " << getSourceText(
Reference.Range);
418 llvm::dbgs() <<
"}\n";
420 References.push_back(Reference);
421 Start = SourceLocation();
424 if (!AnyImportAffected)
426 return std::make_pair(References, FirstNonImportLine);
432 bool parseModuleReference(
const AdditionalKeywords &Keywords,
433 JsModuleReference &Reference) {
434 if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
436 Reference.IsExport = Current->is(tok::kw_export);
439 if (Current->isStringLiteral() && !
Reference.IsExport) {
443 Current->TokenText.substr(1, Current->TokenText.size() - 2);
447 if (!parseModuleBindings(Keywords, Reference))
450 if (Current->is(Keywords.kw_from)) {
453 if (!Current->isStringLiteral())
457 Current->TokenText.substr(1, Current->TokenText.size() - 2);
461 }
else if (
Reference.URL.starts_with(
".")) {
470 bool parseModuleBindings(
const AdditionalKeywords &Keywords,
471 JsModuleReference &Reference) {
472 if (parseStarBinding(Keywords, Reference))
474 return parseNamedBindings(Keywords, Reference);
477 bool parseStarBinding(
const AdditionalKeywords &Keywords,
478 JsModuleReference &Reference) {
480 if (Current->is(Keywords.kw_type) && Current->Next &&
481 Current->Next->is(tok::star)) {
485 if (Current->isNot(tok::star))
488 if (Current->isNot(Keywords.kw_as))
491 if (Current->isNot(tok::identifier))
498 bool parseNamedBindings(
const AdditionalKeywords &Keywords,
499 JsModuleReference &Reference) {
500 if (Current->is(Keywords.kw_type) && Current->Next &&
501 Current->Next->isOneOf(tok::identifier, tok::l_brace)) {
507 if (!
Reference.IsExport && Current->is(tok::identifier)) {
508 Reference.DefaultImport = Current->TokenText;
510 if (Current->is(Keywords.kw_from))
513 if (Current->is(tok::equal)) {
516 while (Current->is(tok::identifier)) {
518 if (Current->is(tok::semi))
520 if (Current->isNot(tok::period))
525 if (Current->isNot(tok::comma))
529 if (Current->isNot(tok::l_brace))
533 Reference.SymbolsStart = Current->Tok.getEndLoc();
534 while (Current->isNot(tok::r_brace)) {
536 if (Current->is(tok::r_brace))
538 auto IsIdentifier = [](
const auto *Tok) {
539 return Tok->isOneOf(tok::identifier, tok::kw_default, tok::kw_template);
541 bool isTypeOnly = Current->is(Keywords.kw_type) && Current->Next &&
542 IsIdentifier(Current->Next);
543 if (!isTypeOnly && !IsIdentifier(Current))
546 JsImportedSymbol Symbol;
548 Symbol.Range.setBegin(
549 Current->getPreviousNonComment()->Next->WhitespaceRange.getBegin());
552 Symbol.Symbol = Current->TokenText;
555 if (Current->is(Keywords.kw_as)) {
557 if (!IsIdentifier(Current))
559 Symbol.Alias = Current->TokenText;
562 Symbol.Range.setEnd(Current->Tok.getLocation());
565 if (!Current->isOneOf(tok::r_brace, tok::comma))
568 Reference.SymbolsEnd = Current->Tok.getLocation();
571 if (Current->Previous->is(tok::comma))
572 Reference.SymbolsEnd = Current->Previous->Tok.getLocation();
This file implements a sorter for JavaScript ES6 imports.
This file declares an abstract TokenAnalyzer, and associated helper classes.
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
A trivial tuple used to represent a source range.
void setEnd(SourceLocation e)
SourceLocation getEndLoc() const
void startToken()
Reset all flags to cleared.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.