Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/SortJavaScriptImports.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SortJavaScriptImports.cpp - Sort ES6 Imports -----------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// \file
10
/// This file implements a sort operation for JavaScript ES6 imports.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "SortJavaScriptImports.h"
15
#include "TokenAnalyzer.h"
16
#include "TokenAnnotator.h"
17
#include "clang/Basic/Diagnostic.h"
18
#include "clang/Basic/DiagnosticOptions.h"
19
#include "clang/Basic/LLVM.h"
20
#include "clang/Basic/SourceLocation.h"
21
#include "clang/Basic/SourceManager.h"
22
#include "clang/Basic/TokenKinds.h"
23
#include "clang/Format/Format.h"
24
#include "llvm/ADT/STLExtras.h"
25
#include "llvm/ADT/SmallVector.h"
26
#include "llvm/Support/Debug.h"
27
#include <algorithm>
28
#include <string>
29
30
#define DEBUG_TYPE "format-formatter"
31
32
namespace clang {
33
namespace format {
34
35
class FormatTokenLexer;
36
37
using clang::format::FormatStyle;
38
39
// An imported symbol in a JavaScript ES6 import/export, possibly aliased.
40
struct JsImportedSymbol {
41
  StringRef Symbol;
42
  StringRef Alias;
43
  SourceRange Range;
44
45
73
  bool operator==(const JsImportedSymbol &RHS) const {
46
    // Ignore Range for comparison, it is only used to stitch code together,
47
    // but imports at different code locations are still conceptually the same.
48
73
    return Symbol == RHS.Symbol && 
Alias == RHS.Alias68
;
49
73
  }
50
};
51
52
// An ES6 module reference.
53
//
54
// ES6 implements a module system, where individual modules (~= source files)
55
// can reference other modules, either importing symbols from them, or exporting
56
// symbols from them:
57
//   import {foo} from 'foo';
58
//   export {foo};
59
//   export {bar} from 'bar';
60
//
61
// `export`s with URLs are syntactic sugar for an import of the symbol from the
62
// URL, followed by an export of the symbol, allowing this code to treat both
63
// statements more or less identically, with the exception being that `export`s
64
// are sorted last.
65
//
66
// imports and exports support individual symbols, but also a wildcard syntax:
67
//   import * as prefix from 'foo';
68
//   export * from 'bar';
69
//
70
// This struct represents both exports and imports to build up the information
71
// required for sorting module references.
72
struct JsModuleReference {
73
  bool FormattingOff = false;
74
  bool IsExport = false;
75
  // Module references are sorted into these categories, in order.
76
  enum ReferenceCategory {
77
    SIDE_EFFECT,     // "import 'something';"
78
    ABSOLUTE,        // from 'something'
79
    RELATIVE_PARENT, // from '../*'
80
    RELATIVE,        // from './*'
81
    ALIAS,           // import X = A.B;
82
  };
83
  ReferenceCategory Category = ReferenceCategory::SIDE_EFFECT;
84
  // The URL imported, e.g. `import .. from 'url';`. Empty for `export {a, b};`.
85
  StringRef URL;
86
  // Prefix from "import * as prefix". Empty for symbol imports and `export *`.
87
  // Implies an empty names list.
88
  StringRef Prefix;
89
  // Default import from "import DefaultName from '...';".
90
  StringRef DefaultImport;
91
  // Symbols from `import {SymbolA, SymbolB, ...} from ...;`.
92
  SmallVector<JsImportedSymbol, 1> Symbols;
93
  // Whether some symbols were merged into this one. Controls if the module
94
  // reference needs re-formatting.
95
  bool SymbolsMerged = false;
96
  // The source location just after { and just before } in the import.
97
  // Extracted eagerly to allow modification of Symbols later on.
98
  SourceLocation SymbolsStart, SymbolsEnd;
99
  // Textual position of the import/export, including preceding and trailing
100
  // comments.
101
  SourceRange Range;
102
};
103
104
68
bool operator<(const JsModuleReference &LHS, const JsModuleReference &RHS) {
105
68
  if (LHS.IsExport != RHS.IsExport)
106
7
    return LHS.IsExport < RHS.IsExport;
107
61
  if (LHS.Category != RHS.Category)
108
17
    return LHS.Category < RHS.Category;
109
44
  if (LHS.Category == JsModuleReference::ReferenceCategory::SIDE_EFFECT ||
110
44
      
LHS.Category == JsModuleReference::ReferenceCategory::ALIAS43
) {
111
    // Side effect imports and aliases might be ordering sensitive. Consider
112
    // them equal so that they maintain their relative order in the stable sort
113
    // below. This retains transitivity because LHS.Category == RHS.Category
114
    // here.
115
2
    return false;
116
2
  }
117
  // Empty URLs sort *last* (for export {...};).
118
42
  if (LHS.URL.empty() != RHS.URL.empty())
119
1
    return LHS.URL.empty() < RHS.URL.empty();
120
41
  if (int Res = LHS.URL.compare_insensitive(RHS.URL))
121
33
    return Res < 0;
122
  // '*' imports (with prefix) sort before {a, b, ...} imports.
123
8
  if (LHS.Prefix.empty() != RHS.Prefix.empty())
124
2
    return LHS.Prefix.empty() < RHS.Prefix.empty();
125
6
  if (LHS.Prefix != RHS.Prefix)
126
0
    return LHS.Prefix > RHS.Prefix;
127
6
  return false;
128
6
}
129
130
// JavaScriptImportSorter sorts JavaScript ES6 imports and exports. It is
131
// implemented as a TokenAnalyzer because ES6 imports have substantial syntactic
132
// structure, making it messy to sort them using regular expressions.
133
class JavaScriptImportSorter : public TokenAnalyzer {
134
public:
135
  JavaScriptImportSorter(const Environment &Env, const FormatStyle &Style)
136
      : TokenAnalyzer(Env, Style),
137
44
        FileContents(Env.getSourceManager().getBufferData(Env.getFileID())) {
138
    // FormatToken.Tok starts out in an uninitialized state.
139
44
    invalidToken.Tok.startToken();
140
44
  }
141
142
  std::pair<tooling::Replacements, unsigned>
143
  analyze(TokenAnnotator &Annotator,
144
          SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
145
44
          FormatTokenLexer &Tokens) override {
146
44
    tooling::Replacements Result;
147
44
    AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
148
149
44
    const AdditionalKeywords &Keywords = Tokens.getKeywords();
150
44
    SmallVector<JsModuleReference, 16> References;
151
44
    AnnotatedLine *FirstNonImportLine;
152
44
    std::tie(References, FirstNonImportLine) =
153
44
        parseModuleReferences(Keywords, AnnotatedLines);
154
155
44
    if (References.empty())
156
5
      return {Result, 0};
157
158
    // The text range of all parsed imports, to be replaced later.
159
39
    SourceRange InsertionPoint = References[0].Range;
160
39
    InsertionPoint.setEnd(References[References.size() - 1].Range.getEnd());
161
162
39
    References = sortModuleReferences(References);
163
164
39
    std::string ReferencesText;
165
130
    for (unsigned I = 0, E = References.size(); I != E; 
++I91
) {
166
91
      JsModuleReference Reference = References[I];
167
91
      appendReference(ReferencesText, Reference);
168
91
      if (I + 1 < E) {
169
        // Insert breaks between imports and exports.
170
52
        ReferencesText += "\n";
171
        // Separate imports groups with two line breaks, but keep all exports
172
        // in a single group.
173
52
        if (!Reference.IsExport &&
174
52
            
(49
Reference.IsExport != References[I + 1].IsExport49
||
175
49
             
Reference.Category != References[I + 1].Category45
)) {
176
11
          ReferencesText += "\n";
177
11
        }
178
52
      }
179
91
    }
180
39
    llvm::StringRef PreviousText = getSourceText(InsertionPoint);
181
39
    if (ReferencesText == PreviousText)
182
6
      return {Result, 0};
183
184
    // The loop above might collapse previously existing line breaks between
185
    // import blocks, and thus shrink the file. SortIncludes must not shrink
186
    // overall source length as there is currently no re-calculation of ranges
187
    // after applying source sorting.
188
    // This loop just backfills trailing spaces after the imports, which are
189
    // harmless and will be stripped by the subsequent formatting pass.
190
    // FIXME: A better long term fix is to re-calculate Ranges after sorting.
191
33
    unsigned PreviousSize = PreviousText.size();
192
148
    while (ReferencesText.size() < PreviousSize)
193
115
      ReferencesText += " ";
194
195
    // Separate references from the main code body of the file.
196
33
    if (FirstNonImportLine && FirstNonImportLine->First->NewlinesBefore < 2 &&
197
33
        
!(25
FirstNonImportLine->First->is(tok::comment)25
&&
198
25
          FirstNonImportLine->First->TokenText.trim() ==
199
25
              "// clang-format on")) {
200
25
      ReferencesText += "\n";
201
25
    }
202
203
33
    LLVM_DEBUG(llvm::dbgs() << "Replacing imports:\n"
204
33
                            << PreviousText << "\nwith:\n"
205
33
                            << ReferencesText << "\n");
206
33
    auto Err = Result.add(tooling::Replacement(
207
33
        Env.getSourceManager(), CharSourceRange::getCharRange(InsertionPoint),
208
33
        ReferencesText));
209
    // FIXME: better error handling. For now, just print error message and skip
210
    // the replacement for the release version.
211
33
    if (Err) {
212
0
      llvm::errs() << llvm::toString(std::move(Err)) << "\n";
213
0
      assert(false);
214
0
    }
215
216
0
    return {Result, 0};
217
39
  }
218
219
private:
220
  FormatToken *Current;
221
  FormatToken *LineEnd;
222
223
  FormatToken invalidToken;
224
225
  StringRef FileContents;
226
227
680
  void skipComments() { Current = skipComments(Current); }
228
229
680
  FormatToken *skipComments(FormatToken *Tok) {
230
684
    while (Tok && 
Tok->is(tok::comment)666
)
231
4
      Tok = Tok->Next;
232
680
    return Tok;
233
680
  }
234
235
519
  void nextToken() {
236
519
    Current = Current->Next;
237
519
    skipComments();
238
519
    if (!Current || Current == LineEnd->Next) {
239
      // Set the current token to an invalid token, so that further parsing on
240
      // this line fails.
241
0
      Current = &invalidToken;
242
0
    }
243
519
  }
244
245
137
  StringRef getSourceText(SourceRange Range) {
246
137
    return getSourceText(Range.getBegin(), Range.getEnd());
247
137
  }
248
249
162
  StringRef getSourceText(SourceLocation Begin, SourceLocation End) {
250
162
    const SourceManager &SM = Env.getSourceManager();
251
162
    return FileContents.substr(SM.getFileOffset(Begin),
252
162
                               SM.getFileOffset(End) - SM.getFileOffset(Begin));
253
162
  }
254
255
  // Sorts the given module references.
256
  // Imports can have formatting disabled (FormattingOff), so the code below
257
  // skips runs of "no-formatting" module references, and sorts/merges the
258
  // references that have formatting enabled in individual chunks.
259
  SmallVector<JsModuleReference, 16>
260
39
  sortModuleReferences(const SmallVector<JsModuleReference, 16> &References) {
261
    // Sort module references.
262
    // Imports can have formatting disabled (FormattingOff), so the code below
263
    // skips runs of "no-formatting" module references, and sorts other
264
    // references per group.
265
39
    const auto *Start = References.begin();
266
39
    SmallVector<JsModuleReference, 16> ReferencesSorted;
267
79
    while (Start != References.end()) {
268
45
      while (Start != References.end() && 
Start->FormattingOff43
) {
269
        // Skip over all imports w/ disabled formatting.
270
5
        ReferencesSorted.push_back(*Start);
271
5
        ++Start;
272
5
      }
273
40
      SmallVector<JsModuleReference, 16> SortChunk;
274
131
      while (Start != References.end() && 
!Start->FormattingOff92
) {
275
        // Skip over all imports w/ disabled formatting.
276
91
        SortChunk.push_back(*Start);
277
91
        ++Start;
278
91
      }
279
40
      llvm::stable_sort(SortChunk);
280
40
      mergeModuleReferences(SortChunk);
281
40
      ReferencesSorted.insert(ReferencesSorted.end(), SortChunk.begin(),
282
40
                              SortChunk.end());
283
40
    }
284
39
    return ReferencesSorted;
285
39
  }
286
287
  // Merge module references.
288
  // After sorting, find all references that import named symbols from the
289
  // same URL and merge their names. E.g.
290
  //   import {X} from 'a';
291
  //   import {Y} from 'a';
292
  // should be rewritten to:
293
  //   import {X, Y} from 'a';
294
  // Note: this modifies the passed in ``References`` vector (by removing no
295
  // longer needed references).
296
40
  void mergeModuleReferences(SmallVector<JsModuleReference, 16> &References) {
297
40
    if (References.empty())
298
2
      return;
299
38
    JsModuleReference *PreviousReference = References.begin();
300
38
    auto *Reference = std::next(References.begin());
301
91
    while (Reference != References.end()) {
302
      // Skip:
303
      //   import 'foo';
304
      //   import * as foo from 'foo'; on either previous or this.
305
      //   import Default from 'foo'; on either previous or this.
306
      //   mismatching
307
53
      if (Reference->Category == JsModuleReference::SIDE_EFFECT ||
308
53
          
PreviousReference->Category == JsModuleReference::SIDE_EFFECT52
||
309
53
          
Reference->IsExport != PreviousReference->IsExport50
||
310
53
          
!PreviousReference->Prefix.empty()46
||
!Reference->Prefix.empty()44
||
311
53
          
!PreviousReference->DefaultImport.empty()43
||
312
53
          
!Reference->DefaultImport.empty()39
||
Reference->Symbols.empty()38
||
313
53
          
PreviousReference->URL != Reference->URL38
) {
314
48
        PreviousReference = Reference;
315
48
        ++Reference;
316
48
        continue;
317
48
      }
318
      // Merge symbols from identical imports.
319
5
      PreviousReference->Symbols.append(Reference->Symbols);
320
5
      PreviousReference->SymbolsMerged = true;
321
      // Remove the merged import.
322
5
      Reference = References.erase(Reference);
323
5
    }
324
38
  }
325
326
  // Appends ``Reference`` to ``Buffer``.
327
91
  void appendReference(std::string &Buffer, JsModuleReference &Reference) {
328
91
    if (Reference.FormattingOff) {
329
5
      Buffer +=
330
5
          getSourceText(Reference.Range.getBegin(), Reference.Range.getEnd());
331
5
      return;
332
5
    }
333
    // Sort the individual symbols within the import.
334
    // E.g. `import {b, a} from 'x';` -> `import {a, b} from 'x';`
335
86
    SmallVector<JsImportedSymbol, 1> Symbols = Reference.Symbols;
336
86
    llvm::stable_sort(
337
86
        Symbols, [&](const JsImportedSymbol &LHS, const JsImportedSymbol &RHS) {
338
18
          return LHS.Symbol.compare_insensitive(RHS.Symbol) < 0;
339
18
        });
340
86
    if (!Reference.SymbolsMerged && 
Symbols == Reference.Symbols81
) {
341
      // Symbols didn't change, just emit the entire module reference.
342
76
      StringRef ReferenceStmt = getSourceText(Reference.Range);
343
76
      Buffer += ReferenceStmt;
344
76
      return;
345
76
    }
346
    // Stitch together the module reference start...
347
10
    Buffer += getSourceText(Reference.Range.getBegin(), Reference.SymbolsStart);
348
    // ... then the references in order ...
349
10
    if (!Symbols.empty()) {
350
10
      Buffer += getSourceText(Symbols.front().Range);
351
12
      for (const JsImportedSymbol &Symbol : llvm::drop_begin(Symbols)) {
352
12
        Buffer += ",";
353
12
        Buffer += getSourceText(Symbol.Range);
354
12
      }
355
10
    }
356
    // ... followed by the module reference end.
357
10
    Buffer += getSourceText(Reference.SymbolsEnd, Reference.Range.getEnd());
358
10
  }
359
360
  // Parses module references in the given lines. Returns the module references,
361
  // and a pointer to the first "main code" line if that is adjacent to the
362
  // affected lines of module references, nullptr otherwise.
363
  std::pair<SmallVector<JsModuleReference, 16>, AnnotatedLine *>
364
  parseModuleReferences(const AdditionalKeywords &Keywords,
365
44
                        SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
366
44
    SmallVector<JsModuleReference, 16> References;
367
44
    SourceLocation Start;
368
44
    AnnotatedLine *FirstNonImportLine = nullptr;
369
44
    bool AnyImportAffected = false;
370
44
    bool FormattingOff = false;
371
161
    for (auto *Line : AnnotatedLines) {
372
161
      assert(Line->First);
373
0
      Current = Line->First;
374
161
      LineEnd = Line->Last;
375
      // clang-format comments toggle formatting on/off.
376
      // This is tracked in FormattingOff here and on JsModuleReference.
377
181
      while (Current && 
Current->is(tok::comment)163
) {
378
20
        StringRef CommentText = Current->TokenText.trim();
379
20
        if (CommentText == "// clang-format off") {
380
7
          FormattingOff = true;
381
13
        } else if (CommentText == "// clang-format on") {
382
7
          FormattingOff = false;
383
          // Special case: consider a trailing "clang-format on" line to be part
384
          // of the module reference, so that it gets moved around together with
385
          // it (as opposed to the next module reference, which might get sorted
386
          // around).
387
7
          if (!References.empty()) {
388
3
            References.back().Range.setEnd(Current->Tok.getEndLoc());
389
3
            Start = Current->Tok.getEndLoc().getLocWithOffset(1);
390
3
          }
391
7
        }
392
        // Handle all clang-format comments on a line, e.g. for an empty block.
393
20
        Current = Current->Next;
394
20
      }
395
161
      skipComments();
396
161
      if (Start.isInvalid() || 
References.empty()21
) {
397
        // After the first file level comment, consider line comments to be part
398
        // of the import that immediately follows them by using the previously
399
        // set Start.
400
149
        Start = Line->First->Tok.getLocation();
401
149
      }
402
161
      if (!Current) {
403
        // Only comments on this line. Could be the first non-import line.
404
18
        FirstNonImportLine = Line;
405
18
        continue;
406
18
      }
407
143
      JsModuleReference Reference;
408
143
      Reference.FormattingOff = FormattingOff;
409
143
      Reference.Range.setBegin(Start);
410
      // References w/o a URL, e.g. export {A}, groups with RELATIVE.
411
143
      Reference.Category = JsModuleReference::ReferenceCategory::RELATIVE;
412
143
      if (!parseModuleReference(Keywords, Reference)) {
413
44
        if (!FirstNonImportLine)
414
37
          FirstNonImportLine = Line; // if no comment before.
415
44
        break;
416
44
      }
417
99
      FirstNonImportLine = nullptr;
418
99
      AnyImportAffected = AnyImportAffected || 
Line->Affected42
;
419
99
      Reference.Range.setEnd(LineEnd->Tok.getEndLoc());
420
99
      LLVM_DEBUG({
421
99
        llvm::dbgs() << "JsModuleReference: {"
422
99
                     << "formatting_off: " << Reference.FormattingOff
423
99
                     << ", is_export: " << Reference.IsExport
424
99
                     << ", cat: " << Reference.Category
425
99
                     << ", url: " << Reference.URL
426
99
                     << ", prefix: " << Reference.Prefix;
427
99
        for (const JsImportedSymbol &Symbol : Reference.Symbols)
428
99
          llvm::dbgs() << ", " << Symbol.Symbol << " as " << Symbol.Alias;
429
99
        llvm::dbgs() << ", text: " << getSourceText(Reference.Range);
430
99
        llvm::dbgs() << "}\n";
431
99
      });
432
99
      References.push_back(Reference);
433
99
      Start = SourceLocation();
434
99
    }
435
    // Sort imports if any import line was affected.
436
44
    if (!AnyImportAffected)
437
5
      References.clear();
438
44
    return std::make_pair(References, FirstNonImportLine);
439
44
  }
440
441
  // Parses a JavaScript/ECMAScript 6 module reference.
442
  // See http://www.ecma-international.org/ecma-262/6.0/#sec-scripts-and-modules
443
  // for grammar EBNF (production ModuleItem).
444
  bool parseModuleReference(const AdditionalKeywords &Keywords,
445
143
                            JsModuleReference &Reference) {
446
143
    if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
447
44
      return false;
448
99
    Reference.IsExport = Current->is(tok::kw_export);
449
450
99
    nextToken();
451
99
    if (Current->isStringLiteral() && 
!Reference.IsExport3
) {
452
      // "import 'side-effect';"
453
3
      Reference.Category = JsModuleReference::ReferenceCategory::SIDE_EFFECT;
454
3
      Reference.URL =
455
3
          Current->TokenText.substr(1, Current->TokenText.size() - 2);
456
3
      return true;
457
3
    }
458
459
96
    if (!parseModuleBindings(Keywords, Reference))
460
0
      return false;
461
462
96
    if (Current->is(Keywords.kw_from)) {
463
      // imports have a 'from' clause, exports might not.
464
92
      nextToken();
465
92
      if (!Current->isStringLiteral())
466
0
        return false;
467
      // URL = TokenText without the quotes.
468
92
      Reference.URL =
469
92
          Current->TokenText.substr(1, Current->TokenText.size() - 2);
470
92
      if (Reference.URL.startswith("..")) {
471
3
        Reference.Category =
472
3
            JsModuleReference::ReferenceCategory::RELATIVE_PARENT;
473
89
      } else if (Reference.URL.startswith(".")) {
474
20
        Reference.Category = JsModuleReference::ReferenceCategory::RELATIVE;
475
69
      } else {
476
69
        Reference.Category = JsModuleReference::ReferenceCategory::ABSOLUTE;
477
69
      }
478
92
    }
479
96
    return true;
480
96
  }
481
482
  bool parseModuleBindings(const AdditionalKeywords &Keywords,
483
96
                           JsModuleReference &Reference) {
484
96
    if (parseStarBinding(Keywords, Reference))
485
3
      return true;
486
93
    return parseNamedBindings(Keywords, Reference);
487
96
  }
488
489
  bool parseStarBinding(const AdditionalKeywords &Keywords,
490
96
                        JsModuleReference &Reference) {
491
    // * as prefix from '...';
492
96
    if (Current->isNot(tok::star))
493
93
      return false;
494
3
    nextToken();
495
3
    if (Current->isNot(Keywords.kw_as))
496
0
      return false;
497
3
    nextToken();
498
3
    if (Current->isNot(tok::identifier))
499
0
      return false;
500
3
    Reference.Prefix = Current->TokenText;
501
3
    nextToken();
502
3
    return true;
503
3
  }
504
505
  bool parseNamedBindings(const AdditionalKeywords &Keywords,
506
93
                          JsModuleReference &Reference) {
507
    // eat a potential "import X, " prefix.
508
93
    if (Current->is(tok::identifier)) {
509
7
      Reference.DefaultImport = Current->TokenText;
510
7
      nextToken();
511
7
      if (Current->is(Keywords.kw_from))
512
3
        return true;
513
      // import X = A.B.C;
514
4
      if (Current->is(tok::equal)) {
515
2
        Reference.Category = JsModuleReference::ReferenceCategory::ALIAS;
516
2
        nextToken();
517
5
        while (Current->is(tok::identifier)) {
518
5
          nextToken();
519
5
          if (Current->is(tok::semi))
520
2
            return true;
521
3
          if (!Current->is(tok::period))
522
0
            return false;
523
3
          nextToken();
524
3
        }
525
2
      }
526
2
      if (Current->isNot(tok::comma))
527
0
        return false;
528
2
      nextToken(); // eat comma.
529
2
    }
530
88
    if (Current->isNot(tok::l_brace))
531
0
      return false;
532
533
    // {sym as alias, sym2 as ...} from '...';
534
88
    Reference.SymbolsStart = Current->Tok.getEndLoc();
535
187
    while (Current->isNot(tok::r_brace)) {
536
101
      nextToken();
537
101
      if (Current->is(tok::r_brace))
538
2
        break;
539
99
      if (!Current->isOneOf(tok::identifier, tok::kw_default))
540
0
        return false;
541
542
99
      JsImportedSymbol Symbol;
543
99
      Symbol.Symbol = Current->TokenText;
544
      // Make sure to include any preceding comments.
545
99
      Symbol.Range.setBegin(
546
99
          Current->getPreviousNonComment()->Next->WhitespaceRange.getBegin());
547
99
      nextToken();
548
549
99
      if (Current->is(Keywords.kw_as)) {
550
6
        nextToken();
551
6
        if (!Current->isOneOf(tok::identifier, tok::kw_default))
552
0
          return false;
553
6
        Symbol.Alias = Current->TokenText;
554
6
        nextToken();
555
6
      }
556
99
      Symbol.Range.setEnd(Current->Tok.getLocation());
557
99
      Reference.Symbols.push_back(Symbol);
558
559
99
      if (!Current->isOneOf(tok::r_brace, tok::comma))
560
0
        return false;
561
99
    }
562
88
    Reference.SymbolsEnd = Current->Tok.getLocation();
563
    // For named imports with a trailing comma ("import {X,}"), consider the
564
    // comma to be the end of the import list, so that it doesn't get removed.
565
88
    if (Current->Previous->is(tok::comma))
566
1
      Reference.SymbolsEnd = Current->Previous->Tok.getLocation();
567
88
    nextToken(); // consume r_brace
568
88
    return true;
569
88
  }
570
};
571
572
tooling::Replacements sortJavaScriptImports(const FormatStyle &Style,
573
                                            StringRef Code,
574
                                            ArrayRef<tooling::Range> Ranges,
575
44
                                            StringRef FileName) {
576
  // FIXME: Cursor support.
577
44
  auto Env = Environment::make(Code, FileName, Ranges);
578
44
  if (!Env)
579
0
    return {};
580
44
  return JavaScriptImportSorter(*Env, Style).process().first;
581
44
}
582
583
} // end namespace format
584
} // end namespace clang