Browse Source

Feature/enable strict null checks in libs/common (#6250)

* feat(ts): enable strict null checks in libs/common

* feat(lint): enable prefer-nullish-coalescing

* feat(lib): resolve errors

* fix(lib): revert changes on DateRange type
pull/6249/head^2
Kenrick Tandrian 6 days ago
committed by GitHub
parent
commit
53be141460
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      libs/common/eslint.config.cjs
  2. 26
      libs/common/src/lib/chart-helper.ts
  3. 2
      libs/common/src/lib/class-transformer.ts
  4. 8
      libs/common/src/lib/helper.ts
  5. 2
      libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts
  6. 2
      libs/common/src/lib/routes/interfaces/internal-route.interface.ts
  7. 4
      libs/common/src/lib/utils/form.util.ts
  8. 2
      libs/common/src/lib/validators/is-currency-code.ts
  9. 3
      libs/common/tsconfig.json

4
libs/common/eslint.config.cjs

@ -18,7 +18,9 @@ module.exports = [
{ {
files: ['**/*.ts', '**/*.tsx'], files: ['**/*.ts', '**/*.tsx'],
// Override or add rules here // Override or add rules here
rules: {} rules: {
'@typescript-eslint/prefer-nullish-coalescing': 'error'
}
}, },
{ {
files: ['**/*.js', '**/*.jsx'], files: ['**/*.js', '**/*.jsx'],

26
libs/common/src/lib/chart-helper.ts

@ -1,4 +1,10 @@
import { Chart, TooltipPosition } from 'chart.js'; import type { ElementRef } from '@angular/core';
import type {
Chart,
ChartTypeRegistry,
Plugin,
TooltipPosition
} from 'chart.js';
import { format } from 'date-fns'; import { format } from 'date-fns';
import { import {
@ -34,12 +40,12 @@ export function getTooltipOptions({
locale = getLocale(), locale = getLocale(),
unit = '' unit = ''
}: { }: {
colorScheme?: ColorScheme; colorScheme: ColorScheme;
currency?: string; currency?: string;
groupBy?: GroupBy; groupBy?: GroupBy;
locale?: string; locale?: string;
unit?: string; unit?: string;
} = {}) { }) {
return { return {
backgroundColor: getBackgroundColor(colorScheme), backgroundColor: getBackgroundColor(colorScheme),
bodyColor: `rgb(${getTextColor(colorScheme)})`, bodyColor: `rgb(${getTextColor(colorScheme)})`,
@ -47,7 +53,7 @@ export function getTooltipOptions({
borderColor: `rgba(${getTextColor(colorScheme)}, 0.1)`, borderColor: `rgba(${getTextColor(colorScheme)}, 0.1)`,
callbacks: { callbacks: {
label: (context) => { label: (context) => {
let label = context.dataset.label || ''; let label = context.dataset.label ?? '';
if (label) { if (label) {
label += ': '; label += ': ';
} }
@ -98,10 +104,10 @@ export function getTooltipPositionerMapTop(
}; };
} }
export function getVerticalHoverLinePlugin( export function getVerticalHoverLinePlugin<T extends keyof ChartTypeRegistry>(
chartCanvas, chartCanvas: ElementRef,
colorScheme?: ColorScheme colorScheme: ColorScheme
) { ): Plugin<T> {
return { return {
afterDatasetsDraw: (chart, _, options) => { afterDatasetsDraw: (chart, _, options) => {
const active = chart.getActiveElements(); const active = chart.getActiveElements();
@ -110,8 +116,8 @@ export function getVerticalHoverLinePlugin(
return; return;
} }
const color = options.color || `rgb(${getTextColor(colorScheme)})`; const color = options.color ?? `rgb(${getTextColor(colorScheme)})`;
const width = options.width || 1; const width = options.width ?? 1;
const { const {
chartArea: { bottom, top } chartArea: { bottom, top }

2
libs/common/src/lib/class-transformer.ts

@ -16,7 +16,7 @@ export function transformToMapOfBig({
return mapOfBig; return mapOfBig;
} }
export function transformToBig({ value }: { value: string }): Big { export function transformToBig({ value }: { value: string }): Big | null {
if (value === null) { if (value === null) {
return null; return null;
} }

8
libs/common/src/lib/helper.ts

@ -144,7 +144,7 @@ export function extractNumberFromString({
}: { }: {
locale?: string; locale?: string;
value: string; value: string;
}): number { }): number | undefined {
try { try {
// Remove non-numeric characters (excluding international formatting characters) // Remove non-numeric characters (excluding international formatting characters)
const numericValue = value.replace(/[^\d.,'’\s]/g, ''); const numericValue = value.replace(/[^\d.,'’\s]/g, '');
@ -273,7 +273,7 @@ export function getNumberFormatDecimal(aLocale?: string) {
return formatObject.find((object) => { return formatObject.find((object) => {
return object.type === 'decimal'; return object.type === 'decimal';
}).value; })?.value;
} }
export function getNumberFormatGroup(aLocale = getLocale()) { export function getNumberFormatGroup(aLocale = getLocale()) {
@ -283,7 +283,7 @@ export function getNumberFormatGroup(aLocale = getLocale()) {
return formatObject.find((object) => { return formatObject.find((object) => {
return object.type === 'group'; return object.type === 'group';
}).value; })?.value;
} }
export function getStartOfUtcDate(aDate: Date) { export function getStartOfUtcDate(aDate: Date) {
@ -394,7 +394,7 @@ export function isRootCurrency(aCurrency: string) {
}); });
} }
export function parseDate(date: string): Date { export function parseDate(date: string): Date | undefined {
if (!date) { if (!date) {
return undefined; return undefined;
} }

2
libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts

@ -39,7 +39,7 @@ export interface PublicPortfolioResponse extends PublicPortfolioResponseV1 {
})[]; })[];
markets: { markets: {
[key in Market]: Pick< [key in Market]: Pick<
PortfolioDetails['markets'][key], NonNullable<PortfolioDetails['markets']>[key],
'id' | 'valueInPercentage' 'id' | 'valueInPercentage'
>; >;
}; };

2
libs/common/src/lib/routes/interfaces/internal-route.interface.ts

@ -2,7 +2,7 @@ import { User } from '@ghostfolio/common/interfaces';
export interface InternalRoute { export interface InternalRoute {
excludeFromAssistant?: boolean | ((aUser: User) => boolean); excludeFromAssistant?: boolean | ((aUser: User) => boolean);
path: string; path?: string;
routerLink: string[]; routerLink: string[];
subRoutes?: Record<string, InternalRoute>; subRoutes?: Record<string, InternalRoute>;
title: string; title: string;

4
libs/common/src/lib/utils/form.util.ts

@ -29,7 +29,7 @@ export async function validateObjectForForm<T>({
if (formControl) { if (formControl) {
formControl.setErrors({ formControl.setErrors({
validationError: Object.values(constraints)[0] validationError: Object.values(constraints ?? {})[0]
}); });
} }
@ -37,7 +37,7 @@ export async function validateObjectForForm<T>({
if (formControlInCustomCurrency) { if (formControlInCustomCurrency) {
formControlInCustomCurrency.setErrors({ formControlInCustomCurrency.setErrors({
validationError: Object.values(constraints)[0] validationError: Object.values(constraints ?? {})[0]
}); });
} }
} }

2
libs/common/src/lib/validators/is-currency-code.ts

@ -9,7 +9,7 @@ import {
import { isISO4217CurrencyCode } from 'class-validator'; import { isISO4217CurrencyCode } from 'class-validator';
export function IsCurrencyCode(validationOptions?: ValidationOptions) { export function IsCurrencyCode(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) { return function (object: object, propertyName: string) {
registerDecorator({ registerDecorator({
propertyName, propertyName,
constraints: [], constraints: [],

3
libs/common/tsconfig.json

@ -12,6 +12,7 @@
], ],
"compilerOptions": { "compilerOptions": {
"module": "preserve", "module": "preserve",
"lib": ["dom", "es2022"] "lib": ["dom", "es2022"],
"strictNullChecks": true
} }
} }

Loading…
Cancel
Save