From 1a8a32b666f11930b88d5658ff887253e6b16b1f Mon Sep 17 00:00:00 2001 From: solufa Date: Fri, 4 Aug 2023 14:27:50 +0900 Subject: [PATCH] refactor: organize imports --- __tests__/index.spec.ts | 4 ++-- package-lock.json | 21 +++++++++++++++++++++ package.json | 2 ++ src/buildTemplate.ts | 4 ++-- src/buildV3.ts | 19 +++++++------------ src/builderUtils/converters.ts | 4 ++-- src/builderUtils/parameters2Props.ts | 8 ++++---- src/builderUtils/requestBodies2Props.ts | 6 +++--- src/builderUtils/resolvers.ts | 4 ++-- src/builderUtils/responses2Props.ts | 6 +++--- src/builderUtils/schemas2Props.ts | 6 +++--- src/getConfig.ts | 5 +++-- src/index.ts | 4 ++-- src/resolveExternalRefs.ts | 8 ++++---- src/writeRouteFile.ts | 3 ++- 15 files changed, 62 insertions(+), 42 deletions(-) diff --git a/__tests__/index.spec.ts b/__tests__/index.spec.ts index a4fc0889..7355aab3 100644 --- a/__tests__/index.spec.ts +++ b/__tests__/index.spec.ts @@ -1,7 +1,7 @@ import fs from 'fs'; -import { ConfigFile } from '../src/getConfig'; -import build from '../src'; import path from 'path'; +import build from '../src'; +import type { ConfigFile } from '../src/getConfig'; function readDirRecursive(dirPath: string): string[] { return fs diff --git a/package-lock.json b/package-lock.json index 16169a1c..1eb95154 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,7 @@ "jest": "^29.6.2", "minimist": "^1.2.8", "prettier": "^2.8.8", + "prettier-plugin-organize-imports": "^3.2.3", "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "typescript": "^5.1.6" @@ -5788,6 +5789,26 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/prettier-plugin-organize-imports": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/prettier-plugin-organize-imports/-/prettier-plugin-organize-imports-3.2.3.tgz", + "integrity": "sha512-KFvk8C/zGyvUaE3RvxN2MhCLwzV6OBbFSkwZ2OamCrs9ZY4i5L77jQ/w4UmUr+lqX8qbaqVq6bZZkApn+IgJSg==", + "dev": true, + "peerDependencies": { + "@volar/vue-language-plugin-pug": "^1.0.4", + "@volar/vue-typescript": "^1.0.4", + "prettier": ">=2.0", + "typescript": ">=2.9" + }, + "peerDependenciesMeta": { + "@volar/vue-language-plugin-pug": { + "optional": true + }, + "@volar/vue-typescript": { + "optional": true + } + } + }, "node_modules/pretty-format": { "version": "29.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.6.2.tgz", diff --git a/package.json b/package.json index d2dfb2d6..e130cd73 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ }, "root": true, "rules": { + "@typescript-eslint/consistent-type-imports": "error", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/ban-ts-comment": "off", @@ -105,6 +106,7 @@ "jest": "^29.6.2", "minimist": "^1.2.8", "prettier": "^2.8.8", + "prettier-plugin-organize-imports": "^3.2.3", "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "typescript": "^5.1.6" diff --git a/src/buildTemplate.ts b/src/buildTemplate.ts index 5a68243a..7e708103 100644 --- a/src/buildTemplate.ts +++ b/src/buildTemplate.ts @@ -1,8 +1,8 @@ import SwaggerParser from '@apidevtools/swagger-parser'; -import { OpenAPI, OpenAPIV3 } from 'openapi-types'; +import type { OpenAPI, OpenAPIV3 } from 'openapi-types'; import buildV3 from './buildV3'; +import type { Config } from './getConfig'; import resolveExternalRefs from './resolveExternalRefs'; -import { Config } from './getConfig'; const isV3 = (openapi: OpenAPI.Document): openapi is OpenAPIV3.Document => 'openapi' in openapi; diff --git a/src/buildV3.ts b/src/buildV3.ts index 71359aa4..a109809c 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -1,24 +1,19 @@ -import { OpenAPIV3 } from 'openapi-types'; +import type { OpenAPIV3 } from 'openapi-types'; import { - isRefObject, $ref2Type, + BINARY_TYPE, getPropertyName, + isRefObject, schema2value, - BINARY_TYPE, } from './builderUtils/converters'; -import { - props2String, - Prop, - PropValue, - value2String, - description2Doc, -} from './builderUtils/props2String'; -import { resolveParamsRef, resolveResRef, resolveReqRef } from './builderUtils/resolvers'; import getDirName from './builderUtils/getDirName'; -import schemas2Props from './builderUtils/schemas2Props'; import parameters2Props from './builderUtils/parameters2Props'; +import type { Prop, PropValue } from './builderUtils/props2String'; +import { description2Doc, props2String, value2String } from './builderUtils/props2String'; import requestBodies2Props from './builderUtils/requestBodies2Props'; +import { resolveParamsRef, resolveReqRef, resolveResRef } from './builderUtils/resolvers'; import responses2Props from './builderUtils/responses2Props'; +import schemas2Props from './builderUtils/schemas2Props'; const methodNames = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'] as const; diff --git a/src/builderUtils/converters.ts b/src/builderUtils/converters.ts index a1c5fdb4..0852d0fd 100644 --- a/src/builderUtils/converters.ts +++ b/src/builderUtils/converters.ts @@ -1,5 +1,5 @@ -import { OpenAPIV3 } from 'openapi-types'; -import { Prop, PropValue } from './props2String'; +import type { OpenAPIV3 } from 'openapi-types'; +import type { Prop, PropValue } from './props2String'; export const defKey2defName = (key: string) => `${key[0].replace(/^([^a-zA-Z$_])$/, '$$$1').toUpperCase()}${key diff --git a/src/builderUtils/parameters2Props.ts b/src/builderUtils/parameters2Props.ts index cadc44cf..be728966 100644 --- a/src/builderUtils/parameters2Props.ts +++ b/src/builderUtils/parameters2Props.ts @@ -1,12 +1,12 @@ -import { OpenAPIV3 } from 'openapi-types'; +import type { OpenAPIV3 } from 'openapi-types'; import { - isRefObject, - defKey2defName, $ref2Type, + defKey2defName, getPropertyName, + isRefObject, schema2value, } from './converters'; -import { Prop } from './props2String'; +import type { Prop } from './props2String'; import { resolveParamsRef } from './resolvers'; export type Parameter = { name: string; prop: string | Prop }; diff --git a/src/builderUtils/requestBodies2Props.ts b/src/builderUtils/requestBodies2Props.ts index 16956418..b8ade09b 100644 --- a/src/builderUtils/requestBodies2Props.ts +++ b/src/builderUtils/requestBodies2Props.ts @@ -1,6 +1,6 @@ -import { OpenAPIV3 } from 'openapi-types'; -import { isRefObject, defKey2defName, $ref2Type, schema2value } from './converters'; -import { PropValue } from './props2String'; +import type { OpenAPIV3 } from 'openapi-types'; +import { $ref2Type, defKey2defName, isRefObject, schema2value } from './converters'; +import type { PropValue } from './props2String'; export type RequestBody = { name: string; value: string | PropValue }; diff --git a/src/builderUtils/resolvers.ts b/src/builderUtils/resolvers.ts index c07561c2..49c327f4 100644 --- a/src/builderUtils/resolvers.ts +++ b/src/builderUtils/resolvers.ts @@ -1,5 +1,5 @@ -import { OpenAPIV3 } from 'openapi-types'; -import { isRefObject, $ref2TypeName } from './converters'; +import type { OpenAPIV3 } from 'openapi-types'; +import { $ref2TypeName, isRefObject } from './converters'; export const resolveParamsRef = ( openapi: OpenAPIV3.Document, diff --git a/src/builderUtils/responses2Props.ts b/src/builderUtils/responses2Props.ts index 8c451a15..e9449b07 100644 --- a/src/builderUtils/responses2Props.ts +++ b/src/builderUtils/responses2Props.ts @@ -1,6 +1,6 @@ -import { OpenAPIV3 } from 'openapi-types'; -import { isRefObject, defKey2defName, $ref2Type, schema2value } from './converters'; -import { PropValue } from './props2String'; +import type { OpenAPIV3 } from 'openapi-types'; +import { $ref2Type, defKey2defName, isRefObject, schema2value } from './converters'; +import type { PropValue } from './props2String'; export type Response = { name: string; value: string | PropValue }; diff --git a/src/builderUtils/schemas2Props.ts b/src/builderUtils/schemas2Props.ts index 9a23eaac..c666e2fc 100644 --- a/src/builderUtils/schemas2Props.ts +++ b/src/builderUtils/schemas2Props.ts @@ -1,6 +1,6 @@ -import { OpenAPIV3 } from 'openapi-types'; -import { isRefObject, defKey2defName, schema2value } from './converters'; -import { PropValue } from './props2String'; +import type { OpenAPIV3 } from 'openapi-types'; +import { defKey2defName, isRefObject, schema2value } from './converters'; +import type { PropValue } from './props2String'; import { resolveSchemasRef } from './resolvers'; export type Schema = { name: string; value: PropValue }; diff --git a/src/getConfig.ts b/src/getConfig.ts index 899fffa6..43b532fe 100644 --- a/src/getConfig.ts +++ b/src/getConfig.ts @@ -1,5 +1,6 @@ -import { OpenAPI } from 'openapi-types'; -import { AspidaConfig, getConfigs } from 'aspida/dist/cjs/commands'; +import type { AspidaConfig } from 'aspida/dist/cjs/commands'; +import { getConfigs } from 'aspida/dist/cjs/commands'; +import type { OpenAPI } from 'openapi-types'; export type Config = Pick & { input: string | OpenAPI.Document; diff --git a/src/index.ts b/src/index.ts index 3306dfba..9e4045b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import fs from 'fs'; import fse from 'fs-extra'; -import getConfig from './getConfig'; import buildTemplate from './buildTemplate'; -import writeRouteFile from './writeRouteFile'; +import getConfig from './getConfig'; import outputFilePath from './outputFilePath'; +import writeRouteFile from './writeRouteFile'; /** * @param {string} outputdir 出力するディレクトリー diff --git a/src/resolveExternalRefs.ts b/src/resolveExternalRefs.ts index f839398c..2da4808a 100644 --- a/src/resolveExternalRefs.ts +++ b/src/resolveExternalRefs.ts @@ -1,9 +1,9 @@ -import https from 'https'; -import http from 'http'; import fs from 'fs'; -import path from 'path'; +import http from 'http'; +import https from 'https'; import yaml from 'js-yaml'; -import { OpenAPIV3 } from 'openapi-types'; +import type { OpenAPIV3 } from 'openapi-types'; +import path from 'path'; const getText = (url: string) => new Promise(resolve => { diff --git a/src/writeRouteFile.ts b/src/writeRouteFile.ts index 277cd7c8..12c1b6d3 100644 --- a/src/writeRouteFile.ts +++ b/src/writeRouteFile.ts @@ -1,5 +1,6 @@ +import type { AspidaConfig } from 'aspida/dist/cjs/commands'; +import { build } from 'aspida/dist/cjs/commands'; import fs from 'fs'; -import { build, AspidaConfig } from 'aspida/dist/cjs/commands'; export default ({ config,