Skip to content

Commit

Permalink
Merge pull request #1697 from chappelo/unsafe-calls
Browse files Browse the repository at this point in the history
remove @typescript-eslint/no-unsafe-call
  • Loading branch information
WoH authored Oct 8, 2024
2 parents e10185b + bba9735 commit 264be8d
Showing 29 changed files with 65 additions and 41 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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',
8 changes: 8 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -397,3 +397,11 @@ export async function generateSpecAndRoutes(args: SwaggerArgs, metadata?: Tsoa.M
throw err;
}
}
export type RouteGeneratorModule<Config extends ExtendedRoutesConfig> = {
default: new (
metadata: Tsoa.Metadata,
routesConfig: Config,
) => {
GenerateCustomRoutes: () => Promise<void>;
};
};
6 changes: 3 additions & 3 deletions packages/cli/src/module/generate-routes.ts
Original file line number Diff line number Diff line change
@@ -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,12 @@ async function getRouteGenerator<Config extends ExtendedRoutesConfig>(metadata:
if (typeof routeGenerator === 'string') {
try {
// try as a module import
const module = await import(routeGenerator);
const module = (await import(routeGenerator)) as RouteGeneratorModule<Config>;
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<Config>;
return new module.default(metadata, routesConfig);
}
} else {
4 changes: 2 additions & 2 deletions packages/cli/src/swagger/specGenerator2.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions packages/cli/src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<ExpressApiHandlerPar
});
if (data && typeof data.pipe === 'function' && data.readable && typeof data._read === 'function') {
response.status(statusCode || 200);
data.pipe(response);
(data as Readable).pipe(response);
} else if (data !== null && data !== undefined) {
response.status(statusCode || 200).json(data);
} else {
Original file line number Diff line number Diff line change
@@ -26,6 +26,6 @@ export abstract class TemplateService<ApiHandlerParameters, ValidationArgsParame
protected buildPromise(methodName: string, controller: Controller | object, validatedArgs: any) {
const prototype = Object.getPrototypeOf(controller);
const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
return descriptor!.value.apply(controller, validatedArgs);
return (descriptor!.value as () => Promise<PropertyDescriptor>).apply(controller, validatedArgs);
}
}
4 changes: 2 additions & 2 deletions tests/esm/fixtures/express/server.ts
Original file line number Diff line number Diff line change
@@ -13,11 +13,11 @@ 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();
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ export default class ServerlessRouteGenerator extends AbstractRouteGenerator<Ser
// This would need to generate a CDK "Stack" that takes the tsoa metadata as input and generates a valid serverless CDK infrastructure stack from template
const templateFileName = this.options.stackTemplate;
const fileName = `${this.options.routesDir}/stack.ts`;
const context = this.buildContext() as unknown as any;
const context = this.buildContext();
context.controllers = context.controllers.map(controller => {
controller.actions = controller.actions.map(action => {
return {
4 changes: 2 additions & 2 deletions tests/fixtures/custom/server.ts
Original file line number Diff line number Diff line change
@@ -23,11 +23,11 @@ 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();
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
4 changes: 2 additions & 2 deletions tests/fixtures/express-dynamic-controllers/server.ts
Original file line number Diff line number Diff line change
@@ -10,11 +10,11 @@ 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();
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
4 changes: 2 additions & 2 deletions tests/fixtures/express-openapi3/server.ts
Original file line number Diff line number Diff line change
@@ -26,11 +26,11 @@ 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();
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
2 changes: 1 addition & 1 deletion tests/fixtures/express-root-security/server.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { RegisterRoutes } from './routes';
export const app: express.Express = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
4 changes: 2 additions & 2 deletions tests/fixtures/express-router-with-custom-multer/server.ts
Original file line number Diff line number Diff line change
@@ -13,14 +13,14 @@ 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();
});

import multer = require('multer');

RegisterRoutes(router, {
(RegisterRoutes as (router: express.Router, options: { multer: ReturnType<typeof multer> }) => void)(router, {
multer: multer({
limits: {
fieldNameSize: 120,
4 changes: 2 additions & 2 deletions tests/fixtures/express-router/server.ts
Original file line number Diff line number Diff line change
@@ -13,12 +13,12 @@ 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();
});

RegisterRoutes(router);
(RegisterRoutes as (router: express.Router) => void)(router);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
4 changes: 2 additions & 2 deletions tests/fixtures/express/server.ts
Original file line number Diff line number Diff line change
@@ -34,11 +34,11 @@ 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();
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
2 changes: 1 addition & 1 deletion tests/fixtures/hapi/server.ts
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ import { RegisterRoutes } from './routes';

export const server = new Server({});

RegisterRoutes(server);
(RegisterRoutes as (app: Server) => void)(server);

server.start().catch(err => {
if (err) {
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-async/server.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ app.use(bodyParser.json());
app.use((req, res, next) => {
methodOverride()(req, res, next);
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Router) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-cpg/server.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ app.use(bodyParser.json());
app.use((req, res, next) => {
methodOverride()(req, res, next);
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-dynamic-container/server.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ app.use(bodyParser.json());
app.use((req, res, next) => {
methodOverride()(req, res, next);
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Express) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
2 changes: 1 addition & 1 deletion tests/fixtures/inversify/server.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ app.use(bodyParser.json());
app.use((req, res, next) => {
methodOverride()(req, res, next);
});
RegisterRoutes(app);
(RegisterRoutes as (app: express.Router) => void)(app);

// It's important that this come after the main routes are registered
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
2 changes: 1 addition & 1 deletion tests/fixtures/koa-multer-options/server.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ app.use(bodyParser());

const router = new KoaRouter();

RegisterRoutes(router);
(RegisterRoutes as (router: KoaRouter) => void)(router);

// It's important that this come after the main routes are registered
app.use(async (context, next) => {
2 changes: 1 addition & 1 deletion tests/fixtures/koa/server.ts
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ app.use(bodyParser());

const router = new KoaRouter();

RegisterRoutes(router);
(RegisterRoutes as (router: KoaRouter) => void)(router);

// It's important that this come after the main routes are registered
app.use(async (context, next) => {
2 changes: 1 addition & 1 deletion tests/fixtures/koaNoAdditional/server.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ app.use(bodyParser());

const router = new KoaRouter();

RegisterRoutes(router);
(RegisterRoutes as (router: KoaRouter) => void)(router);

// It's important that this come after the main routes are registered
app.use(async (context, next) => {
4 changes: 2 additions & 2 deletions tests/integration/koa-multer-options.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});

2 changes: 1 addition & 1 deletion tests/unit/swagger/definitionsGeneration/metadata.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
2 changes: 1 addition & 1 deletion tests/unit/swagger/pathGeneration/getRoutes.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
20 changes: 18 additions & 2 deletions tests/unit/utilities/verifyParameter.ts
Original file line number Diff line number Diff line change
@@ -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) {

0 comments on commit 264be8d

Please sign in to comment.