From 9fcc3c6ceeafa2141925d09c72d15ea731977d60 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 27 Feb 2021 17:41:32 +0100 Subject: [PATCH] Enforce explicit function return types This re-enables the `@typescript-eslint/explicit-module-boundary-types` check and also enables the `@typescript-eslint/explicit-function-return-type` check. Signed-off-by: David Mehren --- .eslintrc.js | 2 +- src/api/private/tokens/tokens.controller.ts | 2 +- src/api/public/me/me.controller.ts | 5 ++- .../monitoring/monitoring.controller.ts | 2 +- src/api/utils/markdownbody-decorator.ts | 2 +- src/auth/auth.service.ts | 10 +++--- src/auth/mock-auth.guard.ts | 2 +- src/config/utils.ts | 5 ++- src/logger/console-logger.service.ts | 18 +++++----- src/main.ts | 2 +- src/media/backends/filesystem-backend.ts | 2 +- src/monitoring/monitoring.service.ts | 8 ++++- src/permissions/permissions.service.spec.ts | 35 +++++++++++-------- src/users/users.service.ts | 2 +- src/utils/swagger.ts | 4 +-- 15 files changed, 59 insertions(+), 42 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 67d0a1004..fa37f4059 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,7 +25,7 @@ module.exports = { 'warn', { argsIgnorePattern: '^_+$' }, ], - '@typescript-eslint/explicit-module-boundary-types': 'off', + '@typescript-eslint/explicit-function-return-type': 'warn', 'no-return-await': 'off', '@typescript-eslint/return-await': ['error', 'always'], '@typescript-eslint/naming-convention': [ diff --git a/src/api/private/tokens/tokens.controller.ts b/src/api/private/tokens/tokens.controller.ts index cb44b93a8..d785e2ccf 100644 --- a/src/api/private/tokens/tokens.controller.ts +++ b/src/api/private/tokens/tokens.controller.ts @@ -53,7 +53,7 @@ export class TokensController { @Delete('/:keyId') @HttpCode(204) - async deleteToken(@Param('keyId') keyId: string) { + async deleteToken(@Param('keyId') keyId: string): Promise { return await this.authService.removeToken(keyId); } } diff --git a/src/api/public/me/me.controller.ts b/src/api/public/me/me.controller.ts index 602efaf99..1169820ea 100644 --- a/src/api/public/me/me.controller.ts +++ b/src/api/public/me/me.controller.ts @@ -86,7 +86,10 @@ export class MeController { @UseGuards(TokenAuthGuard) @Delete('history/:note') @HttpCode(204) - deleteHistoryEntry(@Req() req: Request, @Param('note') note: string) { + deleteHistoryEntry( + @Req() req: Request, + @Param('note') note: string, + ): Promise { // ToDo: Check if user is allowed to delete note try { return this.historyService.deleteHistoryEntry(note, req.user); diff --git a/src/api/public/monitoring/monitoring.controller.ts b/src/api/public/monitoring/monitoring.controller.ts index 4d561f0a3..1905adb08 100644 --- a/src/api/public/monitoring/monitoring.controller.ts +++ b/src/api/public/monitoring/monitoring.controller.ts @@ -25,7 +25,7 @@ export class MonitoringController { @UseGuards(TokenAuthGuard) @Get('prometheus') - getPrometheusStatus() { + getPrometheusStatus(): string { return ''; } } diff --git a/src/api/utils/markdownbody-decorator.ts b/src/api/utils/markdownbody-decorator.ts index 18f1d448b..98dcf0836 100644 --- a/src/api/utils/markdownbody-decorator.ts +++ b/src/api/utils/markdownbody-decorator.ts @@ -41,7 +41,7 @@ export const MarkdownBody = createParamDecorator( } }, [ - (target, key) => { + (target, key): void => { ApiConsumes('text/markdown')( target, key, diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index e078064de..833c24232 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -123,7 +123,7 @@ export class AuthService { return this.toAuthTokenWithSecretDto(createdToken, `${keyId}.${secret}`); } - async setLastUsedToken(keyId: string) { + async setLastUsedToken(keyId: string): Promise { const accessToken = await this.authTokenRepository.findOne({ where: { keyId: keyId }, }); @@ -166,7 +166,7 @@ export class AuthService { return user.authTokens; } - async removeToken(keyId: string) { + async removeToken(keyId: string): Promise { const token = await this.authTokenRepository.findOne({ where: { keyId: keyId }, }); @@ -213,17 +213,17 @@ export class AuthService { // Delete all non valid tokens every sunday on 3:00 AM @Cron('0 0 3 * * 0') - async handleCron() { + async handleCron(): Promise { return await this.removeInvalidTokens(); } // Delete all non valid tokens 5 sec after startup @Timeout(5000) - async handleTimeout() { + async handleTimeout(): Promise { return await this.removeInvalidTokens(); } - async removeInvalidTokens() { + async removeInvalidTokens(): Promise { const currentTime = new Date().getTime(); const tokens: AuthToken[] = await this.authTokenRepository.find(); let removedTokens = 0; diff --git a/src/auth/mock-auth.guard.ts b/src/auth/mock-auth.guard.ts index fc6d166ef..3b77dc9d6 100644 --- a/src/auth/mock-auth.guard.ts +++ b/src/auth/mock-auth.guard.ts @@ -14,7 +14,7 @@ export class MockAuthGuard { private user: User; constructor(private usersService: UsersService) {} - async canActivate(context: ExecutionContext) { + async canActivate(context: ExecutionContext): Promise { const req: Request = context.switchToHttp().getRequest(); if (!this.user) { // this assures that we can create the user 'hardcoded', if we need them before any calls are made or diff --git a/src/config/utils.ts b/src/config/utils.ts index c0d62214c..2064ce19f 100644 --- a/src/config/utils.ts +++ b/src/config/utils.ts @@ -4,7 +4,10 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -export const toArrayConfig = (configValue: string, separator = ',') => { +export const toArrayConfig: ( + configValue: string, + separator?: string, +) => string[] = (configValue: string, separator = ',') => { if (!configValue) { return []; } diff --git a/src/logger/console-logger.service.ts b/src/logger/console-logger.service.ts index a309111bf..aeb1af10b 100644 --- a/src/logger/console-logger.service.ts +++ b/src/logger/console-logger.service.ts @@ -18,11 +18,11 @@ export class ConsoleLoggerService { this.classContext = context; } - setContext(context: string) { + setContext(context: string): void { this.classContext = context; } - error(message: unknown, trace = '', functionContext?: string) { + error(message: unknown, trace = '', functionContext?: string): void { this.printMessage( message, clc.red, @@ -32,7 +32,7 @@ export class ConsoleLoggerService { this.printStackTrace(trace); } - log(message: unknown, functionContext?: string) { + log(message: unknown, functionContext?: string): void { this.printMessage( message, clc.green, @@ -41,7 +41,7 @@ export class ConsoleLoggerService { ); } - warn(message: unknown, functionContext?: string) { + warn(message: unknown, functionContext?: string): void { this.printMessage( message, clc.yellow, @@ -50,7 +50,7 @@ export class ConsoleLoggerService { ); } - debug(message: unknown, functionContext?: string) { + debug(message: unknown, functionContext?: string): void { this.printMessage( message, clc.magentaBright, @@ -59,7 +59,7 @@ export class ConsoleLoggerService { ); } - verbose(message: unknown, functionContext?: string) { + verbose(message: unknown, functionContext?: string): void { this.printMessage( message, clc.cyanBright, @@ -68,7 +68,7 @@ export class ConsoleLoggerService { ); } - private makeContextString(functionContext: string) { + private makeContextString(functionContext: string): string { let context = this.classContext; if (functionContext) { context += '.' + functionContext + '()'; @@ -81,7 +81,7 @@ export class ConsoleLoggerService { color: (message: string) => string, context = '', isTimeDiffEnabled?: boolean, - ) { + ): void { const output = isObject(message) ? `${color('Object:')}\n${JSON.stringify(message, null, 2)}\n` : color(message as string); @@ -117,7 +117,7 @@ export class ConsoleLoggerService { return result; } - private printStackTrace(trace: string) { + private printStackTrace(trace: string): void { if (!trace) { return; } diff --git a/src/main.ts b/src/main.ts index ccecea528..e9c2bb5cd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,7 +15,7 @@ import { NestConsoleLoggerService } from './logger/nest-console-logger.service'; import { setupPrivateApiDocs, setupPublicApiDocs } from './utils/swagger'; import { BackendType } from './media/backends/backend-type.enum'; -async function bootstrap() { +async function bootstrap(): Promise { const app = await NestFactory.create(AppModule); const logger = await app.resolve(NestConsoleLoggerService); logger.log('Switching logger', 'AppBootstrap'); diff --git a/src/media/backends/filesystem-backend.ts b/src/media/backends/filesystem-backend.ts index 47f554fd7..70ceb4e79 100644 --- a/src/media/backends/filesystem-backend.ts +++ b/src/media/backends/filesystem-backend.ts @@ -63,7 +63,7 @@ export class FilesystemBackend implements MediaBackend { return join(this.uploadDirectory, fileName); } - private async ensureDirectory() { + private async ensureDirectory(): Promise { try { await fs.access(this.uploadDirectory); } catch (e) { diff --git a/src/monitoring/monitoring.service.ts b/src/monitoring/monitoring.service.ts index 6c4f18287..9755d9faf 100644 --- a/src/monitoring/monitoring.service.ts +++ b/src/monitoring/monitoring.service.ts @@ -16,7 +16,13 @@ let versionCache: null | { preRelease?: string; commit?: string; } = null; -async function getServerVersionFromPackageJson() { +async function getServerVersionFromPackageJson(): Promise<{ + major: number; + minor: number; + patch: number; + preRelease?: string; + commit?: string; +}> { if (versionCache === null) { const rawFileContent: string = await fs.readFile( joinPath(__dirname, '../../package.json'), diff --git a/src/permissions/permissions.service.spec.ts b/src/permissions/permissions.service.spec.ts index d123cdd2c..a5bcde491 100644 --- a/src/permissions/permissions.service.spec.ts +++ b/src/permissions/permissions.service.spec.ts @@ -11,23 +11,23 @@ @typescript-eslint/require-await */ import { Test, TestingModule } from '@nestjs/testing'; -import { LoggerModule } from '../logger/logger.module'; -import { GuestPermission, PermissionsService } from './permissions.service'; -import { User } from '../users/user.entity'; -import { Note } from '../notes/note.entity'; -import { UsersModule } from '../users/users.module'; -import { NotesModule } from '../notes/notes.module'; -import { PermissionsModule } from './permissions.module'; import { getRepositoryToken } from '@nestjs/typeorm'; +import { AuthToken } from '../auth/auth-token.entity'; +import { Group } from '../groups/group.entity'; +import { LoggerModule } from '../logger/logger.module'; +import { AuthorColor } from '../notes/author-color.entity'; +import { Note } from '../notes/note.entity'; +import { NotesModule } from '../notes/notes.module'; +import { Tag } from '../notes/tag.entity'; +import { Authorship } from '../revisions/authorship.entity'; +import { Revision } from '../revisions/revision.entity'; +import { Identity } from '../users/identity.entity'; +import { User } from '../users/user.entity'; +import { UsersModule } from '../users/users.module'; import { NoteGroupPermission } from './note-group-permission.entity'; import { NoteUserPermission } from './note-user-permission.entity'; -import { Identity } from '../users/identity.entity'; -import { AuthToken } from '../auth/auth-token.entity'; -import { Authorship } from '../revisions/authorship.entity'; -import { AuthorColor } from '../notes/author-color.entity'; -import { Revision } from '../revisions/revision.entity'; -import { Tag } from '../notes/tag.entity'; -import { Group } from '../groups/group.entity'; +import { PermissionsModule } from './permissions.module'; +import { GuestPermission, PermissionsService } from './permissions.service'; describe('PermissionsService', () => { let permissionsService: PermissionsService; @@ -154,6 +154,7 @@ describe('PermissionsService', () => { noteEverybodyWrite, ]; } + const notes = createNoteUserPermissionNotes(); describe('mayRead works with', () => { @@ -342,6 +343,7 @@ describe('PermissionsService', () => { [user2groupWrite, user2groupRead, null], // group4: don't allow user1 to read or write via group ]; } + /* * creates the matrix multiplication of group0 to group4 of createAllNoteGroupPermissions */ @@ -414,7 +416,10 @@ describe('PermissionsService', () => { ): NoteGroupPermission[][] { const results = []; - function permute(arr: NoteGroupPermission[], memo: NoteGroupPermission[]) { + function permute( + arr: NoteGroupPermission[], + memo: NoteGroupPermission[], + ): NoteGroupPermission[][] { let cur; for (let i = 0; i < arr.length; i++) { diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 814743b95..7714d0dd5 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -26,7 +26,7 @@ export class UsersService { return this.userRepository.save(user); } - async deleteUser(userName: string) { + async deleteUser(userName: string): Promise { // TODO: Handle owned notes and edits const user = await this.userRepository.findOne({ where: { userName: userName }, diff --git a/src/utils/swagger.ts b/src/utils/swagger.ts index f0167992c..e242d5397 100644 --- a/src/utils/swagger.ts +++ b/src/utils/swagger.ts @@ -9,7 +9,7 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { PrivateApiModule } from '../api/private/private-api.module'; import { PublicApiModule } from '../api/public/public-api.module'; -export function setupPublicApiDocs(app: INestApplication) { +export function setupPublicApiDocs(app: INestApplication): void { const publicApiOptions = new DocumentBuilder() .setTitle('HedgeDoc Public API') // TODO: Use real version @@ -25,7 +25,7 @@ export function setupPublicApiDocs(app: INestApplication) { SwaggerModule.setup('apidoc', app, publicApi); } -export function setupPrivateApiDocs(app: INestApplication) { +export function setupPrivateApiDocs(app: INestApplication): void { const privateApiOptions = new DocumentBuilder() .setTitle('HedgeDoc Private API') // TODO: Use real version