Skip to content

Commit 5f4b3bd

Browse files
authored
fix: use type-only imports whenever possible (#460)
This PR introduces two fundamental ESLint rules: ```text @typescript-eslint/no-import-type-side-effects @typescript-eslint/consistent-type-imports ``` Which help avoiding situations where `tsc` keeps empty imports. Fixes #459
1 parent bcbe601 commit 5f4b3bd

File tree

10 files changed

+19
-15
lines changed

10 files changed

+19
-15
lines changed

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default [
1313
rules: {
1414
"@typescript-eslint/no-empty-object-type": ["error", { allowInterfaces: "with-single-extends" }],
1515
"@typescript-eslint/no-namespace": ["error", { allowDeclarations: true }],
16+
"@typescript-eslint/no-import-type-side-effects": "error",
17+
"@typescript-eslint/consistent-type-imports": ["error", { fixStyle: "inline-type-imports" }],
1618
"no-empty": ["error", { allowEmptyCatch: true }],
1719
"prefer-const": ["error", { destructuring: "all" }],
1820
},

src/plugins/nx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// See nx-transformer-plugin.ts
22
// https://github.com/nrwl/nx/blob/229f71ef1758ee625869aaa6fa6355dc3284fa5b/packages/js/src/utils/typescript/types.ts#L19-L32
33
// https://github.com/nrwl/nx/blob/master/packages/js/src/utils/typescript/load-ts-transformers.ts
4-
import ts from "typescript";
4+
import type ts from "typescript";
55

66
import transformer from "../transformer.ts";
77

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Minimatch } from "minimatch";
2-
import { type PluginConfig } from "ts-patch";
3-
import ts, { type CompilerOptions, type EmitHost, type Pattern, type SourceFile } from "typescript";
1+
import type { Minimatch } from "minimatch";
2+
import type { PluginConfig } from "ts-patch";
3+
import type ts from "typescript";
4+
import type { CompilerOptions, EmitHost, Pattern, SourceFile } from "typescript";
45

56
/* ****************************************************************************************************************** */
67
// region: TS Types

src/utils/resolve-module-name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from "node:path";
22

33
import { removeFileExtension, removeSuffix, type ResolvedModuleFull, type SourceFile } from "typescript";
44

5-
import { type VisitorContext } from "../types.ts";
5+
import type { VisitorContext } from "../types.ts";
66
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils.ts";
77
import { getRelativePath } from "./get-relative-path.ts";
88
import { getOutputDirForSourceFile } from "./ts-helpers.ts";

src/utils/resolve-path-update-node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ts from "typescript";
1+
import type ts from "typescript";
22

3-
import { type VisitorContext } from "../types.ts";
3+
import type { VisitorContext } from "../types.ts";
44
import { isURL, maybeAddRelativeLocalPrefix } from "./general-utils.ts";
55
import { resolveModuleName } from "./resolve-module-name.ts";
66
import { isModulePathsMatch } from "./ts-helpers.ts";

src/utils/ts-helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import path from "node:path";
22

33
import type { REGISTER_INSTANCE } from "ts-node";
4-
import ts, { type GetCanonicalFileName, type SourceFile } from "typescript";
4+
import type ts from "typescript";
5+
import type { GetCanonicalFileName, SourceFile } from "typescript";
56

6-
import { type VisitorContext } from "../types.ts";
7+
import type { VisitorContext } from "../types.ts";
78

89
/* ****************************************************************************************************************** */
910
// region: TS Helpers

src/visitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ts from "typescript";
1+
import type ts from "typescript";
22

3-
import { type VisitorContext } from "./types.ts";
3+
import type { VisitorContext } from "./types.ts";
44
import { elideImportOrExportDeclaration, resolvePathAndUpdateNode } from "./utils/index.ts";
55

66
const isAsyncImport = ({ tsInstance }: VisitorContext, node: ts.Node): node is ts.CallExpression =>

test/tests/transformer/general.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as path from "node:path";
33
import { before, describe, test } from "node:test";
44

5-
import { projectsPaths, ts, tsModules } from "../../config.ts";
5+
import { projectsPaths, type ts, tsModules } from "../../config.ts";
66
import { createTsProgram, getEmitResultFromProgram, type EmittedFiles } from "../../utils/index.ts";
77

88
/* ****************************************************************************************************************** *

test/tests/transformer/specific.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import assert from "node:assert";
33
import * as path from "node:path";
44
import { before, describe, test } from "node:test";
55

6-
import TS from "typescript";
6+
import type TS from "typescript";
77

8-
import { type TsTransformPathsConfig } from "typescript-transform-paths";
8+
import type { TsTransformPathsConfig } from "typescript-transform-paths";
99

1010
import { projectsPaths, ts, tsModules } from "../../config.ts";
1111
import {

test/utils/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "node:fs";
22

3-
import ts from "typescript";
3+
import type ts from "typescript";
44
import { default as tstpTransform, type TsTransformPathsConfig } from "typescript-transform-paths";
55

66
import * as config from "../config.ts";

0 commit comments

Comments
 (0)