Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove @typescript-eslint/no-unsafe-call #1697

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/swagger/specGenerator2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/custom/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/express-dynamic-controllers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/express-openapi3/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/express-root-security/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
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
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/express-router/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/hapi/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-async/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-cpg/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-dynamic-container/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/inversify/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/koa-multer-options/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/koa/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/koaNoAdditional/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/koa-multer-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
});
});

Expand All @@ -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');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

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