From 085d8bd34204c09acd469859cd1076bf146bcf90 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 6 Oct 2024 02:13:52 +0900 Subject: [PATCH] remove @typescript-eslint/no-unsafe-call --- .eslintrc.js | 1 - packages/cli/src/cli.ts | 9 +++++++++ packages/cli/src/module/generate-routes.ts | 7 ++++--- packages/cli/src/swagger/specGenerator2.ts | 4 ++-- packages/cli/src/swagger/specGenerator3.ts | 4 ++-- .../express/expressTemplateService.ts | 3 ++- .../templates/templateService.ts | 3 ++- tests/esm/fixtures/express/server.ts | 2 +- .../serverlessRouteGenerator.ts | 2 +- tests/fixtures/custom/server.ts | 2 +- .../express-dynamic-controllers/server.ts | 2 +- tests/fixtures/express-openapi3/server.ts | 2 +- .../server.ts | 2 +- tests/fixtures/express-router/server.ts | 2 +- tests/fixtures/express/server.ts | 2 +- tests/integration/koa-multer-options.spec.ts | 4 ++-- .../definitionsGeneration/definitions.spec.ts | 2 +- .../definitionsGeneration/metadata.spec.ts | 2 +- .../swagger/pathGeneration/getRoutes.spec.ts | 2 +- tests/unit/utilities/verifyParameter.ts | 20 +++++++++++++++++-- 20 files changed, 52 insertions(+), 25 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 453f8a4ac..21cbec0ca 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -31,7 +31,6 @@ module.exports = { ], '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-unsafe-assignment': 'off', - '@typescript-eslint/no-unsafe-call': 'off', '@typescript-eslint/no-unsafe-argument': 'off', '@typescript-eslint/no-unsafe-member-access': 'off', '@typescript-eslint/no-unsafe-return': 'off', diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 3b8df0a4f..807483df9 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -397,3 +397,12 @@ export async function generateSpecAndRoutes(args: SwaggerArgs, metadata?: Tsoa.M throw err; } } + +export type RouteGeneratorModule = { + default: new ( + metadata: Tsoa.Metadata, + routesConfig: Config, + ) => { + GenerateCustomRoutes: () => Promise; + }; +}; diff --git a/packages/cli/src/module/generate-routes.ts b/packages/cli/src/module/generate-routes.ts index 594b620a4..f35f56e78 100644 --- a/packages/cli/src/module/generate-routes.ts +++ b/packages/cli/src/module/generate-routes.ts @@ -1,5 +1,5 @@ import * as ts from 'typescript'; -import { ExtendedRoutesConfig } from '../cli'; +import { ExtendedRoutesConfig, RouteGeneratorModule } from '../cli'; import { MetadataGenerator } from '../metadataGeneration/metadataGenerator'; import { Tsoa } from '@tsoa/runtime'; import { DefaultRouteGenerator } from '../routeGeneration/defaultRouteGenerator'; @@ -46,12 +46,13 @@ async function getRouteGenerator(metadata: if (typeof routeGenerator === 'string') { try { // try as a module import - const module = await import(routeGenerator); + const module = (await import(routeGenerator)) as RouteGeneratorModule; + return new module.default(metadata, routesConfig); } catch (_err) { // try to find a relative import path const relativePath = path.relative(__dirname, routeGenerator); - const module = await import(relativePath); + const module = (await import(relativePath)) as RouteGeneratorModule; return new module.default(metadata, routesConfig); } } else { diff --git a/packages/cli/src/swagger/specGenerator2.ts b/packages/cli/src/swagger/specGenerator2.ts index 34a833d57..5f620a4f8 100644 --- a/packages/cli/src/swagger/specGenerator2.ts +++ b/packages/cli/src/swagger/specGenerator2.ts @@ -68,13 +68,13 @@ export class SpecGenerator2 extends SpecGenerator { if (this.config.spec) { this.config.specMerging = this.config.specMerging || 'immediate'; - const mergeFuncs: { [key: string]: any } = { + const mergeFuncs: { [key: string]: (spec: UnspecifiedObject, merge: UnspecifiedObject) => UnspecifiedObject } = { immediate: Object.assign, recursive: mergeAnything, deepmerge: (spec: UnspecifiedObject, merge: UnspecifiedObject): UnspecifiedObject => deepMerge(spec, merge), }; - spec = mergeFuncs[this.config.specMerging](spec, this.config.spec); + spec = mergeFuncs[this.config.specMerging](spec as unknown as UnspecifiedObject, this.config.spec as unknown as UnspecifiedObject) as unknown as Swagger.Spec2; } if (this.config.schemes) { spec.schemes = this.config.schemes; diff --git a/packages/cli/src/swagger/specGenerator3.ts b/packages/cli/src/swagger/specGenerator3.ts index cc17ee02d..bfcb215d1 100644 --- a/packages/cli/src/swagger/specGenerator3.ts +++ b/packages/cli/src/swagger/specGenerator3.ts @@ -39,13 +39,13 @@ export class SpecGenerator3 extends SpecGenerator { if (this.config.spec) { this.config.specMerging = this.config.specMerging || 'immediate'; - const mergeFuncs: { [key: string]: any } = { + const mergeFuncs: { [key: string]: (spec: UnspecifiedObject, merge: UnspecifiedObject) => UnspecifiedObject } = { immediate: Object.assign, recursive: mergeAnything, deepmerge: (spec: UnspecifiedObject, merge: UnspecifiedObject): UnspecifiedObject => deepMerge(spec, merge), }; - spec = mergeFuncs[this.config.specMerging](spec, this.config.spec); + spec = mergeFuncs[this.config.specMerging](spec as unknown as UnspecifiedObject, this.config.spec as UnspecifiedObject) as unknown as Swagger.Spec3; } return spec; diff --git a/packages/runtime/src/routeGeneration/templates/express/expressTemplateService.ts b/packages/runtime/src/routeGeneration/templates/express/expressTemplateService.ts index a3dc4a6c3..b3b8de937 100644 --- a/packages/runtime/src/routeGeneration/templates/express/expressTemplateService.ts +++ b/packages/runtime/src/routeGeneration/templates/express/expressTemplateService.ts @@ -5,6 +5,7 @@ import { FieldErrors } from '../../templateHelpers'; import { TsoaRoute } from '../../tsoa-route'; import { ValidateError } from '../../templateHelpers'; import { TemplateService } from '../templateService'; +import { Readable } from 'node:stream'; type ExpressApiHandlerParameters = { methodName: string; @@ -115,7 +116,7 @@ export class ExpressTemplateService extends TemplateService Promise).apply(controller, validatedArgs); } } diff --git a/tests/esm/fixtures/express/server.ts b/tests/esm/fixtures/express/server.ts index 67cb30b66..0ac723711 100644 --- a/tests/esm/fixtures/express/server.ts +++ b/tests/esm/fixtures/express/server.ts @@ -13,7 +13,7 @@ app.use(bodyParser.json() as RequestHandler); app.use((req, res, next) => { methodOverride()(req, res, next); }); -app.use((req: any, res: any, next: any) => { +app.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/fixtures/custom/custom-route-generator/serverlessRouteGenerator.ts b/tests/fixtures/custom/custom-route-generator/serverlessRouteGenerator.ts index 7fa59a99e..ff79941ad 100644 --- a/tests/fixtures/custom/custom-route-generator/serverlessRouteGenerator.ts +++ b/tests/fixtures/custom/custom-route-generator/serverlessRouteGenerator.ts @@ -77,7 +77,7 @@ export default class ServerlessRouteGenerator extends AbstractRouteGenerator { controller.actions = controller.actions.map(action => { return { diff --git a/tests/fixtures/custom/server.ts b/tests/fixtures/custom/server.ts index 9542c528e..e3d7b4b83 100644 --- a/tests/fixtures/custom/server.ts +++ b/tests/fixtures/custom/server.ts @@ -23,7 +23,7 @@ app.use(bodyParser.json()); app.use((req, res, next) => { methodOverride()(req, res, next); }); -app.use((req: any, res: any, next: any) => { +app.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/fixtures/express-dynamic-controllers/server.ts b/tests/fixtures/express-dynamic-controllers/server.ts index 2f153d02e..564783b24 100644 --- a/tests/fixtures/express-dynamic-controllers/server.ts +++ b/tests/fixtures/express-dynamic-controllers/server.ts @@ -10,7 +10,7 @@ app.use(bodyParser.json()); app.use((req, res, next) => { methodOverride()(req, res, next); }); -app.use((req: any, res: any, next: any) => { +app.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/fixtures/express-openapi3/server.ts b/tests/fixtures/express-openapi3/server.ts index 3f1fc363e..4b6fed9a8 100644 --- a/tests/fixtures/express-openapi3/server.ts +++ b/tests/fixtures/express-openapi3/server.ts @@ -26,7 +26,7 @@ app.use(bodyParser.json()); app.use((req, res, next) => { methodOverride()(req, res, next); }); -app.use((req: any, res: any, next: any) => { +app.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/fixtures/express-router-with-custom-multer/server.ts b/tests/fixtures/express-router-with-custom-multer/server.ts index ec46d06c2..96569dd91 100644 --- a/tests/fixtures/express-router-with-custom-multer/server.ts +++ b/tests/fixtures/express-router-with-custom-multer/server.ts @@ -13,7 +13,7 @@ router.use(bodyParser.json()); router.use((req, res, next) => { methodOverride()(req, res, next); }); -router.use((req: any, res: any, next: any) => { +router.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/fixtures/express-router/server.ts b/tests/fixtures/express-router/server.ts index 9fed34529..9543b2eda 100644 --- a/tests/fixtures/express-router/server.ts +++ b/tests/fixtures/express-router/server.ts @@ -13,7 +13,7 @@ router.use(bodyParser.json()); router.use((req, res, next) => { methodOverride()(req, res, next); }); -router.use((req: any, res: any, next: any) => { +router.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/fixtures/express/server.ts b/tests/fixtures/express/server.ts index a15d2ffa5..a465a3430 100644 --- a/tests/fixtures/express/server.ts +++ b/tests/fixtures/express/server.ts @@ -34,7 +34,7 @@ app.use(bodyParser.json()); app.use((req, res, next) => { methodOverride()(req, res, next); }); -app.use((req: any, res: any, next: any) => { +app.use((req: any, res: any, next: express.NextFunction) => { req.stringValue = 'fancyStringForContext'; next(); }); diff --git a/tests/integration/koa-multer-options.spec.ts b/tests/integration/koa-multer-options.spec.ts index 47556e5b5..b30cb5224 100644 --- a/tests/integration/koa-multer-options.spec.ts +++ b/tests/integration/koa-multer-options.spec.ts @@ -19,7 +19,7 @@ describe('Koa Server (with multerOpts)', () => { expect(res.body.originalname).to.equal('package.json'); expect(res.body.encoding).to.be.not.undefined; expect(res.body.mimetype).to.equal('application/json'); - expect(res.body.path).to.satisfy(value => value.startsWith(os.tmpdir())); + expect(res.body.path).to.satisfy((value: string) => value.startsWith(os.tmpdir())); }); }); @@ -31,7 +31,7 @@ describe('Koa Server (with multerOpts)', () => { expect(res.body.fieldname).to.equal('someFile'); expect(res.body.originalname).to.equal('lessThan8mb'); expect(res.body.encoding).to.be.not.undefined; - expect(res.body.path).to.satisfy(value => value.startsWith(os.tmpdir())); + expect(res.body.path).to.satisfy((value: string) => value.startsWith(os.tmpdir())); unlinkSync('./lessThan8mb'); }); }); diff --git a/tests/unit/swagger/definitionsGeneration/definitions.spec.ts b/tests/unit/swagger/definitionsGeneration/definitions.spec.ts index e4e03f092..29d3cc956 100644 --- a/tests/unit/swagger/definitionsGeneration/definitions.spec.ts +++ b/tests/unit/swagger/definitionsGeneration/definitions.spec.ts @@ -3272,7 +3272,7 @@ describe('Definition generation', () => { throw new Error(`There was no ${aPropertyName} schema generated for the ${currentSpec.specName}`); } it(`should produce a valid schema for the ${aPropertyName} property on ${interfaceName} for the ${currentSpec.specName}`, () => { - assertionsPerProperty[aPropertyName](aPropertyName, propertySchema); + assertionsPerProperty[aPropertyName as keyof TestModel](aPropertyName, propertySchema); }); }); diff --git a/tests/unit/swagger/definitionsGeneration/metadata.spec.ts b/tests/unit/swagger/definitionsGeneration/metadata.spec.ts index 4cc5097d6..7d241896b 100644 --- a/tests/unit/swagger/definitionsGeneration/metadata.spec.ts +++ b/tests/unit/swagger/definitionsGeneration/metadata.spec.ts @@ -1056,7 +1056,7 @@ describe('Metadata generation', () => { if (!controller || !controllerIntDefault) throw new Error('AnnotatedTypesController not defined!'); - const getControllerNumberMethods = controller => { + const getControllerNumberMethods = (controller: Tsoa.Controller) => { const getDefault = controller.methods.find(method => method.name === 'getDefault'); const getDouble = controller.methods.find(method => method.name === 'getDouble'); const getInteger = controller.methods.find(method => method.name === 'getInteger'); diff --git a/tests/unit/swagger/pathGeneration/getRoutes.spec.ts b/tests/unit/swagger/pathGeneration/getRoutes.spec.ts index 0297a765f..d300f7d4a 100644 --- a/tests/unit/swagger/pathGeneration/getRoutes.spec.ts +++ b/tests/unit/swagger/pathGeneration/getRoutes.spec.ts @@ -43,7 +43,7 @@ describe('GET route generation', () => { throw new Error('No operation parameters.'); } - return get.parameters as any; + return get.parameters; }; it('should generate a path for a GET route with no path argument', () => { diff --git a/tests/unit/utilities/verifyParameter.ts b/tests/unit/utilities/verifyParameter.ts index fcb05010d..488293f98 100644 --- a/tests/unit/utilities/verifyParameter.ts +++ b/tests/unit/utilities/verifyParameter.ts @@ -9,7 +9,15 @@ export function VerifyPathableParameter(params: Swagger.Parameter[], paramValue: } } -export function VerifyPathableStringParameter(params: Swagger.PathParameter[], paramValue: string, paramType: string, paramIn: string, min?: number, max?: number, pattern?: string) { +export function VerifyPathableStringParameter( + params: Swagger.PathParameter[] | Swagger.Parameter2[], + paramValue: string, + paramType: string, + paramIn: string, + min?: number, + max?: number, + pattern?: string, +) { const parameter = verifyParameter(params, paramValue, paramIn); expect(parameter.type).to.equal(paramType); if (min) { @@ -23,7 +31,15 @@ export function VerifyPathableStringParameter(params: Swagger.PathParameter[], p } } -export function VerifyPathableNumberParameter(params: Swagger.PathParameter[], paramValue: string, paramType: string, paramIn: string, formatType?: string, min?: number, max?: number) { +export function VerifyPathableNumberParameter( + params: Swagger.PathParameter[] | Swagger.Parameter2[], + paramValue: string, + paramType: string, + paramIn: string, + formatType?: string, + min?: number, + max?: number, +) { const parameter = verifyParameter(params, paramValue, paramIn); expect(parameter.type).to.equal(paramType); if (formatType) {