Skip to content

Commit

Permalink
refactor: sort all top-level imports (#337)
Browse files Browse the repository at this point in the history
- basically, general format is:
  ```ts
  import x from "external-dep"

  import y from "./internal-dep"
  ```
  - so external deps, new line, then internal/local deps
  - with some further sorting within there, like trying to keep Node
    built-ins (e.g. `path`) at the top half of externals, then core deps
    like `typescript`, then any other external deps
    - and similar for internal deps -- core internals at the top half of
      internals, then any other internal deps
    - just to keep things consistent between files -- makes the top
      easier to read through when it's similar between files
    - also makes it easier for contributors to understand where to put
      imports, as there's a sorting already there

- this is how I generally sort my imports and how I wrote most of the
  unit test suite's imports as well

- there is automation for this that we should probably add once TSLint
  is replaced here; some previous art:
  - https://github.com/trivago/prettier-plugin-sort-imports
  - https://github.com/lydell/eslint-plugin-simple-import-sort/
  - Older:
    - https://github.com/renke/import-sort/tree/master/packages/import-sort-style-module
    - https://github.com/mcdougal/js-isort
    - inspired by Python's `isort` ofc
  • Loading branch information
agilgur5 authored Jun 1, 2022
1 parent ccd6815 commit b08f272
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/check-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";

import { tsModule } from "./tsproxy";

export function checkTsConfig(parsedConfig: tsTypes.ParsedCommandLine): void
{
const module = parsedConfig.options.module!;
Expand Down
1 change: 1 addition & 0 deletions src/diagnostics-format-host.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "path";
import * as tsTypes from "typescript";

import { tsModule } from "./tsproxy";

export class FormatHost implements tsTypes.FormatDiagnosticsHost
Expand Down
5 changes: 3 additions & 2 deletions src/get-options-overrides.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as path from "path";
import * as tsTypes from "typescript";
import { createFilter as createRollupFilter, normalizePath as normalize } from "@rollup/pluginutils";

import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import { IOptions } from "./ioptions";
import * as path from "path";
import { IContext } from "./context";

export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot }: IOptions, preParsedTsconfig?: tsTypes.ParsedCommandLine): tsTypes.CompilerOptions
Expand Down
3 changes: 2 additions & 1 deletion src/host.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import { normalizePath as normalize } from "@rollup/pluginutils";

import { tsModule } from "./tsproxy";
import { TransformerFactoryCreator } from "./ioptions";

export class LanguageServiceHost implements tsTypes.LanguageServiceHost
Expand Down
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { relative, dirname, normalize as pathNormalize, resolve as pathResolve } from "path";
import * as tsTypes from "typescript";
import { PluginImpl, PluginContext, InputOptions, OutputOptions, TransformResult, SourceMap, Plugin } from "rollup";
import { normalizePath as normalize } from "@rollup/pluginutils";
import * as _ from "lodash";
import { blue, red, yellow, green } from "colors/safe";
import * as resolve from "resolve";
import findCacheDir from "find-cache-dir";

import { RollupContext } from "./rollupcontext";
import { ConsoleContext, VerbosityLevel } from "./context";
import { LanguageServiceHost } from "./host";
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences } from "./tscache";
import { tsModule, setTypescriptModule } from "./tsproxy";
import * as tsTypes from "typescript";
import * as resolve from "resolve";
import * as _ from "lodash";
import { IOptions } from "./ioptions";
import { parseTsConfig } from "./parse-tsconfig";
import { printDiagnostics } from "./print-diagnostics";
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
import { blue, red, yellow, green } from "colors/safe";
import { relative, dirname, normalize as pathNormalize, resolve as pathResolve } from "path";
import { normalizePath as normalize } from "@rollup/pluginutils";
import findCacheDir from "find-cache-dir";

import { PluginImpl, PluginContext, InputOptions, OutputOptions, TransformResult, SourceMap, Plugin } from "rollup";

import { createFilter } from "./get-options-overrides";

type RPT2Options = Partial<IOptions>;
Expand Down
3 changes: 2 additions & 1 deletion src/ioptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";

import { tsModule } from "./tsproxy";

export interface ICustomTransformer
{
before?: tsTypes.TransformerFactory<tsTypes.SourceFile>;
Expand Down
5 changes: 3 additions & 2 deletions src/parse-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { dirname } from "path";
import * as _ from "lodash";

import { tsModule } from "./tsproxy";
import { IContext } from "./context";
import { dirname } from "path";
import { printDiagnostics } from "./print-diagnostics";
import { convertDiagnostic } from "./tscache";
import { getOptionsOverrides } from "./get-options-overrides";
import { IOptions } from "./ioptions";
import * as _ from "lodash";
import { checkTsConfig } from "./check-tsconfig";

export function parseTsConfig(context: IContext, pluginOptions: IOptions)
Expand Down
3 changes: 2 additions & 1 deletion src/print-diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { tsModule } from "./tsproxy";
import { red, white, yellow } from "colors/safe";

import { tsModule } from "./tsproxy";
import { IContext } from "./context";
import { IDiagnostics } from "./tscache";

Expand Down
5 changes: 3 additions & 2 deletions src/rollingcache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ICache } from "./icache";
import { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } from "fs-extra";
import { existsSync, readdirSync, renameSync } from "fs";
import { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } from "fs-extra";
import * as _ from "lodash";

import { ICache } from "./icache";

/**
* Saves data in new cache folder or reads it from old one.
* Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed.
Expand Down
3 changes: 2 additions & 1 deletion src/rollupcontext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IContext, VerbosityLevel } from "./context";
import * as _ from "lodash";
import { PluginContext } from "rollup";

import { IContext, VerbosityLevel } from "./context";

export class RollupContext implements IContext
{
private hasContext: boolean = true;
Expand Down
11 changes: 6 additions & 5 deletions src/tscache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IContext } from "./context";
import * as tsTypes from "typescript";
import { emptyDirSync, pathExistsSync, readdirSync, removeSync, statSync } from "fs-extra";
import * as _ from "lodash";
import { Graph, alg } from "graphlib";
import hash from "object-hash";
import { blue, yellow, green } from "colors/safe";

import { IContext } from "./context";
import { RollingCache } from "./rollingcache";
import { ICache } from "./icache";
import * as _ from "lodash";
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import { blue, yellow, green } from "colors/safe";
import { emptyDirSync, pathExistsSync, readdirSync, removeSync, statSync } from "fs-extra";
import { formatHost } from "./diagnostics-format-host";
import { NoCache } from "./nocache";

Expand Down

0 comments on commit b08f272

Please sign in to comment.