Skip to content

Commit

Permalink
refactor: eslint uses import type where possible
Browse files Browse the repository at this point in the history
This commit adds an eslint rule
`@typescript-eslint/consistent-type-imports` that warns (and
automatically fixes) when `import` is used instead of `import type` for
importing types.

This has minor performance benefits at build time and maybe at runtime,
but can also communicate the intent of the import more clearly.

For a nice developer experience, it's recommended to enable autofixing
fixable eslint issues so that the developer never has to worry about
this - it's done completely automatically when the file is saved.
Autoformatting with prettier is also recommended.

- https://typescript-eslint.io/blog/consistent-type-imports-and-exports-why-and-how/
- https://typescript-eslint.io/rules/consistent-type-imports/
  • Loading branch information
mikavilpas committed Sep 12, 2024
1 parent 0f7dbe7 commit 1e49b28
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 22 deletions.
7 changes: 7 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default tseslint.config(
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/consistent-type-imports': 'warn',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
Expand All @@ -43,6 +44,12 @@ export default tseslint.config(
'typescript-sort-keys/string-enum': 'warn',
},
},
{
files: ['packages/openapi-ts/test/e2e/assets/main-angular-module.ts'],
rules: {
'@typescript-eslint/consistent-type-imports': 'off',
},
},
configPrettier,
{
ignores: [
Expand Down
3 changes: 2 additions & 1 deletion packages/client-axios/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosError } from 'axios';
import type { AxiosError } from 'axios';
import axios from 'axios';

import type { Client, Config, RequestOptions } from './types';
import { createConfig, getUrl, mergeConfigs, mergeHeaders } from './utils';
Expand Down
8 changes: 2 additions & 6 deletions packages/openapi-ts/src/compiler/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import {
toParameterDeclarations,
toTypeParameters,
} from './types';
import {
addLeadingComments,
Comments,
createIdentifier,
isType,
} from './utils';
import type { Comments } from './utils';
import { addLeadingComments, createIdentifier, isType } from './utils';

/**
* Create a class constructor declaration.
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type PathLike, rmSync, writeFileSync } from 'node:fs';
import path from 'node:path';

import ts from 'typescript';
import type ts from 'typescript';

import { ensureDirSync } from '../generate/utils';
import * as classes from './classes';
Expand Down
9 changes: 4 additions & 5 deletions packages/openapi-ts/src/generate/services.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
import type {
ClassElement,
type Comments,
compiler,
Comments,
FunctionParameter,
type Node,
TypeScriptFile,
Node,
} from '../compiler';
import { compiler, TypeScriptFile } from '../compiler';
import type { FunctionTypeParameter, ObjectValue } from '../compiler/types';
import { isOperationParameterRequired } from '../openApi';
import type {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/generate/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnumDeclaration } from 'typescript';
import type { EnumDeclaration } from 'typescript';

import {
type Comments,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/common/parser/getRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OpenApiReference as OpenApiReferenceV2 } from '../../v2/interfaces/OpenApiReference';
import type { OpenApiReference as OpenApiReferenceV3 } from '../../v3/interfaces/OpenApiReference';
import { OpenApi } from '../interfaces/OpenApi';
import type { OpenApi } from '../interfaces/OpenApi';

const ESCAPED_REF_SLASH = /~1/g;
const ESCAPED_REF_TILDE = /~0/g;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ts from 'typescript';

import { compiler, type Property } from '../../../compiler';
import type { ImportExportItem } from '../../../compiler/module';
import { ImportExportItemObject } from '../../../compiler/utils';
import type { ImportExportItemObject } from '../../../compiler/utils';
import {
clientModulePath,
clientOptionsTypeName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosError } from 'axios';
import type { AxiosError } from 'axios';
import axios from 'axios';

import type { Client, Config, RequestOptions } from './types';
import { createConfig, getUrl, mergeConfigs, mergeHeaders } from './utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosError } from 'axios';
import type { AxiosError } from 'axios';
import axios from 'axios';

import type { Client, Config, RequestOptions } from './types';
import { createConfig, getUrl, mergeConfigs, mergeHeaders } from './utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ export const createClient = (config: Config = {}): Client => {
request,
setConfig,
trace: (options) => request({ ...options, method: 'TRACE' }),
} as Client;
};
};
3 changes: 2 additions & 1 deletion packages/openapi-ts/test/e2e/scripts/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import puppeteer, { Browser, EvaluateFunc, Page } from 'puppeteer'
import type { Browser, EvaluateFunc, Page } from 'puppeteer';
import puppeteer from 'puppeteer'

let _browser: Browser
let _page: Page
Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-ts/test/e2e/scripts/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Server } from 'node:http'
import type { Server } from 'node:http'
import path from 'node:path'

import express, { Express } from 'express'
import type { Express } from 'express';
import express from 'express'

let _app: Express
let _server: Server
Expand Down

0 comments on commit 1e49b28

Please sign in to comment.