//===--- SortJavaScriptImports.cpp - Sort ES6 Imports -----------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// This file implements a sort operation for JavaScript ES6 imports. /// //===----------------------------------------------------------------------===// #include "SortJavaScriptImports.h" #include "TokenAnalyzer.h" #include "TokenAnnotator.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/TokenKinds.h" #include "clang/Format/Format.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Debug.h" #include <algorithm> #include <string> #define DEBUG_TYPE … namespace clang { namespace format { class FormatTokenLexer; // An imported symbol in a JavaScript ES6 import/export, possibly aliased. struct JsImportedSymbol { … }; // An ES6 module reference. // // ES6 implements a module system, where individual modules (~= source files) // can reference other modules, either importing symbols from them, or exporting // symbols from them: // import {foo} from 'foo'; // export {foo}; // export {bar} from 'bar'; // // `export`s with URLs are syntactic sugar for an import of the symbol from the // URL, followed by an export of the symbol, allowing this code to treat both // statements more or less identically, with the exception being that `export`s // are sorted last. // // imports and exports support individual symbols, but also a wildcard syntax: // import * as prefix from 'foo'; // export * from 'bar'; // // This struct represents both exports and imports to build up the information // required for sorting module references. struct JsModuleReference { … }; bool operator<(const JsModuleReference &LHS, const JsModuleReference &RHS) { … } // JavaScriptImportSorter sorts JavaScript ES6 imports and exports. It is // implemented as a TokenAnalyzer because ES6 imports have substantial syntactic // structure, making it messy to sort them using regular expressions. class JavaScriptImportSorter : public TokenAnalyzer { … }; tooling::Replacements sortJavaScriptImports(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName) { … } } // end namespace format } // end namespace clang