Skip to content

Commit

Permalink
✨ feat(app): add sonar lint and fix some eslint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
drackp2m committed Oct 30, 2024
1 parent 0f068f9 commit 3676496
Show file tree
Hide file tree
Showing 17 changed files with 1,844 additions and 136 deletions.
12 changes: 0 additions & 12 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.maxNumberOfProblems": 1000,
"cSpell.words": [
"codegen",
"commitlint",
"devkit",
"mikro",
"nestjs",
"ngneat",
"ngrx",
"stylelint",
"tsparticles"
],
"svg.preview.background": "editor"
}
3 changes: 1 addition & 2 deletions apps/api/src/module/game/factory/game.faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export interface GameFakerOptions {
}

export class GameFaker {
private readonly basicFaker = new BasicFaker();
static makeOne = new GameFaker().makeEntity;
static readonly makeOne = new GameFaker().makeEntity;

static make(
quantity: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export class GameParticipantRepository {
}

async update(entity: GameParticipant): Promise<GameParticipant> {
await this.entityManager.fork().persistAndFlush(entity);

return entity;
return this.insert(entity);
}

async delete(entity: GameParticipant): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/module/user/factory/user.faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface UserFakerOptions {
}

export class UserFaker {
static makeOne = new UserFaker().makeEntity;
static readonly makeOne = new UserFaker().makeEntity;

static make(
amount: number,
Expand Down
14 changes: 8 additions & 6 deletions apps/api/src/shared/util/bootstrap.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import { ApiConfig } from '../module/config/types/api-config.type';
import { HttpExceptionFilter } from './exception-filter';

export class BootstrapHelper {
static validationPipe = new ValidationPipe({
static readonly validationPipe = new ValidationPipe({
whitelist: true,
transform: true,
});

static exceptionsFilter = new HttpExceptionFilter();
static readonly exceptionsFilter = new HttpExceptionFilter();

static globalPrefix = (appConfig: ApiConfig): [prefix: string, options?: GlobalPrefixOptions] => {
static readonly globalPrefix = (
appConfig: ApiConfig,
): [prefix: string, options?: GlobalPrefixOptions] => {
return [appConfig.prefix, { exclude: ['', 'api'] }];
};

static nestApplicationOptions = (appConfig: ApiConfig): NestApplicationOptions => {
static readonly nestApplicationOptions = (appConfig: ApiConfig): NestApplicationOptions => {
const nestApplicationOptions: NestApplicationOptions = {};

if (appConfig.environment !== 'production') {
Expand All @@ -33,13 +35,13 @@ export class BootstrapHelper {
return nestApplicationOptions;
};

static apiConfig = (app: INestApplication): ApiConfig => {
static readonly apiConfig = (app: INestApplication): ApiConfig => {
const configService = app.get(ConfigurationService);

return configService.api;
};

static logAppBootstrap = (appConfig: ApiConfig): void => {
static readonly logAppBootstrap = (appConfig: ApiConfig): void => {
const isProduction = appConfig.environment === 'production';
const port = isProduction ? '' : `:${appConfig.port}`;

Expand Down
4 changes: 1 addition & 3 deletions apps/api/src/shared/util/custom-entity.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export class CustomRepository<T extends CustomBaseEntity<T>> {
}

async update(entity: T): Promise<T> {
await this.entityManager.fork().persistAndFlush(entity);

return entity;
return this.insert(entity);
}

async delete(entity: T): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions apps/app/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export default [
],
},
},
{
files: ['**/*.ts/1_inline-template-app.component.ts-1.component.html'],
rules: {
'sonarjs/no-element-overwrite': 'off',
'sonarjs/no-same-line-conditional': 'off',
'sonarjs/no-unenclosed-multiline-block': 'off',
},
},
{
files: ['**/*.html'],
rules: {
Expand Down
2 changes: 1 addition & 1 deletion apps/app/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
},
"development": {
"buildTarget": "app:build:development",
"hmr": true,
"liveReload": false,
"host": "0.0.0.0",
"disableHostCheck": true,
"hmr": true,
"ssl": true,
"sslKey": "certs/set-self-signed.key",
"sslCert": "certs/set-self-signed.crt"
Expand Down
3 changes: 1 addition & 2 deletions apps/app/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Component, effect, inject } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { ApolloModule } from 'apollo-angular';

import { SendPingGQL } from '@set-online/apollo-definitions';

Expand All @@ -10,7 +9,7 @@ import { pingValue } from './app.config';
standalone: true,
selector: 'set-root',
template: `<router-outlet />`,
imports: [RouterOutlet, ApolloModule],
imports: [RouterOutlet],
providers: [SendPingGQL],
})
export class AppComponent {
Expand Down
4 changes: 2 additions & 2 deletions apps/app/src/app/interceptor/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ export class AuthInterceptor implements HttpInterceptor {
const isAuthStoreLoading = this.authStore.isLoading();

if (event !== undefined && isAuthStoreLoading === false) {
void this.authStore.tryToRefreshTokens();
this.authStore.tryToRefreshTokens();
}

return this.authStoreLoadingFinished$.pipe(
take(1),
switchMap(() => {
if (this.authStore.tokensAreValid() === false) {
void this.router.navigate(['/login']);
this.router.navigate(['/login']);
}

return next.handle(req);
Expand Down
1 change: 1 addition & 0 deletions apps/app/src/app/page/game/game.page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ main {
// display: grid;
// grid-gap: var(--spacing-md);
// grid-template-columns: repeat(4, auto);

gap: 8px;
justify-content: center;

Expand Down
28 changes: 23 additions & 5 deletions apps/app/src/app/page/game/service/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class GameService {
return allCardsAreDefined && this.isSet(firstCard, secondCard, thirdCard);
}

// FixMe => replace with fillBoardGaps (nulls)
// FixMe: replace with fillBoardGaps (nulls)
getUpdatedBoardCards(
boardCards: CardInterface[],
setCards: CardInterface[],
Expand Down Expand Up @@ -123,12 +123,16 @@ export class GameService {

// FixMe => move this to lib
private getRandomNumber(): number {
const randomNumber = Math.floor(Math.random() * 3) + 1;
const crypto = window.crypto;
const array = new Uint32Array(1);
crypto.getRandomValues(array);

if (!randomNumber) {
if (!array[0]) {
throw new Error('Random number is not defined');
}

const randomNumber = (array[0] % 3) + 1;

return randomNumber;
}

Expand All @@ -142,8 +146,8 @@ export class GameService {
throw new Error('Enum is empty');
}

const randomIndex = Math.floor(Math.random() * enumKeys.length);
const randomEnumKey = enumKeys[randomIndex];
const randomIndex = this.getRandomNumberInRange(0, enumKeys.length - 1);
const randomEnumKey = enumKeys[randomIndex ?? 0];

const enumKey = randomEnumKey ?? firstEnumKey;

Expand All @@ -169,4 +173,18 @@ export class GameService {

return isBasicEnum ? enumKeys.slice(enumKeys.length / 2) : enumKeys;
}

private getRandomNumberInRange(min: number, max: number): number | undefined {
const crypto = window.crypto;
const array = new Uint32Array(1);
crypto.getRandomValues(array);

if (!array[0]) {
return undefined;
}

const randomNumber = Math.floor((array[0] / (0xffffffff + 1)) * (max - min + 1)) + min;

return randomNumber;
}
}
2 changes: 1 addition & 1 deletion apps/app/src/app/store/user.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const initialState: UserState = {
error: undefined,
};

export type UserStore = InstanceType<typeof UserStore>;
// export type UserStore = InstanceType<typeof UserStore>;

export const UserStore = signalStore(
{ providedIn: 'root' },
Expand Down
20 changes: 20 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "0.2",
"ignorePaths": [],
"dictionaryDefinitions": [],
"dictionaries": [],
"words": [
"codegen",
"commitlint",
"devkit",
"mikro",
"nestjs",
"ngneat",
"ngrx",
"stylelint",
"tsparticles",
"sonarjs"
],
"ignoreWords": [],
"import": []
}
22 changes: 18 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { fixupPluginRules } from '@eslint/compat';
import nx from '@nx/eslint-plugin';
import tsParser from '@typescript-eslint/parser';
import eslintPluginImport from 'eslint-plugin-import';
import prettier from 'eslint-plugin-prettier';
import sonarjs from 'eslint-plugin-sonarjs';
import unusedImports from 'eslint-plugin-unused-imports';
import globals from 'globals';

export default [
sonarjs.configs.recommended,
...nx.configs['flat/base'],
...nx.configs['flat/typescript'],
...nx.configs['flat/javascript'],
{
plugins: {
prettier,
'@nx': nx,
import: fixupPluginRules(eslintPluginImport),
import: eslintPluginImport,
'unused-imports': unusedImports,
prettier: prettier,
},
},
{
Expand All @@ -28,7 +29,7 @@ export default [
},
},
{
files: ['**/*.ts', '**/*.js'],
files: ['**/*.ts', '**/*.js', '**/*.mjs'],
rules: {
'@nx/enforce-module-boundaries': [
'error',
Expand Down Expand Up @@ -83,6 +84,15 @@ export default [
},
},
rules: {
...Object.fromEntries(
Object.entries(sonarjs.configs.recommended.rules).map(([ruleName, ruleValue]) => {
return [`${ruleName}`, ruleValue.replace('error', 'warn')];
}),
),
'sonarjs/todo-tag': 'off',
'sonarjs/fixme-tag': 'off',
'sonarjs/unused-import': 'off',
'sonarjs/pseudo-random': 'warn',
'unused-imports/no-unused-imports': 'warn',
'no-unused-private-class-members': 'warn',
'@typescript-eslint/no-unused-vars': [
Expand Down Expand Up @@ -159,5 +169,9 @@ export default [
...globals.jest,
},
},
rules: {
'sonarjs/no-skipped-test': 'off',
'sonarjs/no-hardcoded-credentials': 'off',
},
},
];
Loading

0 comments on commit 3676496

Please sign in to comment.