From e83f1e206b7b967e6d9461db424f44d1ffebbbbf Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 20 Feb 2021 21:15:45 +0100 Subject: [PATCH] ESLint: Enable @typescript-eslint/naming-convention rule This check enforces consistent variable naming. Signed-off-by: David Mehren --- .eslintrc.js | 24 ++++++++++++++++++++++++ src/api/utils/markdownbody-decorator.ts | 2 ++ src/auth/auth.service.spec.ts | 6 +++--- src/auth/auth.service.ts | 6 +++--- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3bb81225a..35c52642b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -28,5 +28,29 @@ module.exports = { '@typescript-eslint/explicit-module-boundary-types': 'off', 'no-return-await': 'off', '@typescript-eslint/return-await': ['error', 'always'], + '@typescript-eslint/naming-convention': [ + 'error', + { + selector: 'default', + format: ['camelCase'], + leadingUnderscore: 'allow', + trailingUnderscore: 'allow', + }, + { + selector: 'enumMember', + format: ['UPPER_CASE'], + }, + { + selector: 'variable', + format: ['camelCase', 'UPPER_CASE'], + leadingUnderscore: 'allow', + trailingUnderscore: 'allow', + }, + + { + selector: 'typeLike', + format: ['PascalCase'], + }, + ], }, }; diff --git a/src/api/utils/markdownbody-decorator.ts b/src/api/utils/markdownbody-decorator.ts index 1c81df3ea..18f1d448b 100644 --- a/src/api/utils/markdownbody-decorator.ts +++ b/src/api/utils/markdownbody-decorator.ts @@ -18,6 +18,8 @@ import * as getRawBody from 'raw-body'; * * Implementation inspired by https://stackoverflow.com/questions/52283713/how-do-i-pass-plain-text-as-my-request-body-using-nestjs */ +// Override naming convention as decorators are in PascalCase +// eslint-disable-next-line @typescript-eslint/naming-convention export const MarkdownBody = createParamDecorator( async (_, context: ExecutionContext) => { // we have to check req.readable because of raw-body issue #57 diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index 8506b5070..356570fdd 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -69,7 +69,7 @@ describe('AuthService', () => { .then((result) => expect(result).toBeTruthy()); }); it('fails, if secret is too short', async () => { - const secret = service.BufferToBase64Url(await service.randomString(54)); + const secret = service.bufferToBase64Url(await service.randomString(54)); const hash = await service.hashPassword(secret); service .checkPassword(secret, hash) @@ -277,10 +277,10 @@ describe('AuthService', () => { }); }); - describe('BufferToBase64Url', () => { + describe('bufferToBase64Url', () => { it('works', () => { expect( - service.BufferToBase64Url( + service.bufferToBase64Url( Buffer.from('testsentence is a test sentence'), ), ).toEqual('dGVzdHNlbnRlbmNlIGlzIGEgdGVzdCBzZW50ZW5jZQ'); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index ed3b36c43..5d746eb61 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -69,7 +69,7 @@ export class AuthService { return randomBytes(length); } - BufferToBase64Url(text: Buffer): string { + bufferToBase64Url(text: Buffer): string { // This is necessary as the is no base64url encoding in the toString method // but as can be seen on https://tools.ietf.org/html/rfc4648#page-7 // base64url is quite easy buildable from base64 @@ -93,8 +93,8 @@ export class AuthService { `User '${user.userName}' has already 200 tokens and can't have anymore`, ); } - const secret = this.BufferToBase64Url(await this.randomString(54)); - const keyId = this.BufferToBase64Url(await this.randomString(8)); + const secret = this.bufferToBase64Url(await this.randomString(54)); + const keyId = this.bufferToBase64Url(await this.randomString(8)); const accessToken = await this.hashPassword(secret); let token; // Tokens can only be valid for a maximum of 2 years