refactor: replace TSLint with ESLint

This commit is contained in:
Wieland Schöbl
2021-06-30 13:53:44 +02:00
committed by Jovan Krunić
parent 67fb4a43c9
commit d696215d08
147 changed files with 5471 additions and 2704 deletions

View File

@@ -13,14 +13,11 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {DecimalPipe} from '@angular/common';
import {Injectable, OnDestroy, Pipe, PipeTransform} from '@angular/core';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {Subscription} from 'rxjs';
// tslint:disable: completed-docs
@Injectable()
@Pipe({
name: 'join',
@@ -29,21 +26,21 @@ import {Subscription} from 'rxjs';
export class ArrayJoinPipe implements PipeTransform {
value = '';
transform(anArray: [], separator: string): unknown {
transform(anArray: unknown[], separator: string | unknown): unknown {
if (typeof separator !== 'string' || separator.length <= 0) {
return this.value;
}
if (!Array.isArray(anArray)){
throw new SyntaxError(`Wrong parameter in ArrayJoinPipe. Expected a valid Array, received: ${anArray}`);
if (!Array.isArray(anArray)) {
throw new SyntaxError(
`Wrong parameter in ArrayJoinPipe. Expected a valid Array, received: ${anArray}`,
);
}
this.value = anArray.join(separator);
return this.value;
}
}
@Injectable()
@@ -52,25 +49,25 @@ export class ArrayJoinPipe implements PipeTransform {
pure: false, // required to update the value when the promise is resolved
})
export class StringSplitPipe implements PipeTransform {
value = Array<unknown>();
value = new Array<unknown>();
transform(aString: string, splitter: string): unknown {
transform<T>(aString: string | T, splitter: string | T): T {
if (typeof splitter !== 'string' || splitter.length <= 0) {
return this.value;
return this.value as never;
}
if (typeof aString !== 'string'){
throw new SyntaxError(`Wrong parameter in StringSplitPipe. Expected a valid String, received: ${aString}`);
if (typeof aString !== 'string') {
throw new SyntaxError(
`Wrong parameter in StringSplitPipe. Expected a valid String, received: ${aString}`,
);
}
this.value = aString.split(splitter);
return this.value;
return this.value as never;
}
}
@Injectable()
@Pipe({
name: 'numberLocalized',
@@ -78,8 +75,11 @@ export class StringSplitPipe implements PipeTransform {
})
export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
decimalPipe: DecimalPipe;
locale: string;
onLangChange: Subscription;
value: unknown;
constructor(private readonly translate: TranslateService) {
@@ -109,21 +109,22 @@ export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
* Default is `3`.
*/
transform(value: unknown, digitsInfo?: string | undefined): unknown {
this.updateValue(value, digitsInfo);
this._dispose();
if (typeof this.onLangChange === 'undefined' || this.onLangChange.closed ) {
this.onLangChange = this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
if (typeof this.onLangChange === 'undefined' || this.onLangChange.closed) {
this.onLangChange = this.translate.onLangChange.subscribe(
(event: LangChangeEvent) => {
this.locale = event.lang;
this.updateValue(value, digitsInfo);
});
}
},
);
}
return this.value;
}
updateValue(value: unknown, digitsInfo?: string | undefined): void {
// this.value = this.locale;
this.value = this.decimalPipe.transform(value, digitsInfo,this.locale);
this.value = this.decimalPipe.transform(value, digitsInfo, this.locale);
}
}

View File

@@ -14,45 +14,55 @@
*/
import {ModuleWithProviders, NgModule, Provider} from '@angular/core';
import {ArrayJoinPipe, NumberLocalizedPipe, StringSplitPipe} from './common-string-pipes';
import {ThingTranslateDefaultParser, ThingTranslateParser} from './thing-translate.parser';
import {ThingPropertyNameTranslatePipe, ThingTranslatePipe} from './thing-translate.pipe';
import {
ArrayJoinPipe,
NumberLocalizedPipe,
StringSplitPipe,
} from './common-string-pipes';
import {
ThingTranslateDefaultParser,
ThingTranslateParser,
} from './thing-translate.parser';
import {
ThingPropertyNameTranslatePipe,
ThingTranslatePipe,
} from './thing-translate.pipe';
import {ThingTranslateService} from './thing-translate.service';
// tslint:disable: completed-docs
export interface ThingTranslateModuleConfig {
parser?: Provider;
}
@NgModule({
declarations: [
ThingTranslatePipe,
ThingPropertyNameTranslatePipe,
ArrayJoinPipe,
StringSplitPipe,
NumberLocalizedPipe,
// ThingTranslatorDirective
StringSplitPipe,
ThingPropertyNameTranslatePipe,
ThingTranslatePipe,
],
exports: [
ThingTranslatePipe,
ThingPropertyNameTranslatePipe,
ArrayJoinPipe,
StringSplitPipe,
NumberLocalizedPipe,
// ThingTranslatorDirective
StringSplitPipe,
ThingPropertyNameTranslatePipe,
ThingTranslatePipe,
],
})
export class ThingTranslateModule {
/**
* Use this method in your other (non root) modules to import the directive/pipe
*/
static forChild(config: ThingTranslateModuleConfig = {}): ModuleWithProviders<ThingTranslateModule> {
static forChild(
config: ThingTranslateModuleConfig = {},
): ModuleWithProviders<ThingTranslateModule> {
return {
ngModule: ThingTranslateModule,
providers: [
config.parser ?? {provide: ThingTranslateParser, useClass: ThingTranslateDefaultParser},
config.parser ?? {
provide: ThingTranslateParser,
useClass: ThingTranslateDefaultParser,
},
ThingTranslateService,
],
};
@@ -61,11 +71,16 @@ export class ThingTranslateModule {
/**
* Use this method in your root module to provide the TranslatorService
*/
static forRoot(config: ThingTranslateModuleConfig = {}): ModuleWithProviders<ThingTranslateModule> {
static forRoot(
config: ThingTranslateModuleConfig = {},
): ModuleWithProviders<ThingTranslateModule> {
return {
ngModule: ThingTranslateModule,
providers: [
config.parser ?? {provide: ThingTranslateParser, useClass: ThingTranslateDefaultParser},
config.parser ?? {
provide: ThingTranslateParser,
useClass: ThingTranslateDefaultParser,
},
ThingTranslateService,
],
};

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-types */
/*
* Copyright (C) 2020-2021 StApps
* This program is free software: you can redistribute it and/or modify it
@@ -15,7 +16,7 @@
import {Injectable} from '@angular/core';
// tslint:disable: member-ordering prefer-function-over-method completed-docs
/* eslint-disable @typescript-eslint/member-ordering, class-methods-use-this */
export abstract class ThingTranslateParser {
/**
@@ -27,22 +28,23 @@ export abstract class ThingTranslateParser {
@Injectable()
export class ThingTranslateDefaultParser extends ThingTranslateParser {
getValueFromKeyPath(instance: object, keyPath: string): unknown {
// keyPath = aproperty[0].anotherproperty["arrayproperty"][42].finalproperty
let path = keyPath.replace(/(?:\"|\')'"/gmi, '.');
let path = keyPath.replace(/["']'"/gim, '.');
// path = aproperty[0].anotherproperty[.arrayproperty.][42].finalproperty
path = path.replace(/(?:\[|\])/gmi, '.');
path = path.replace(/[\[\]]/gim, '.');
// path = aproperty.0..anotherproperty..arrayproperty...42..finalproperty
path = path.replace(/\.{2,}/gmi, '.');
// TODO
// eslint-disable-next-line @typescript-eslint/no-unused-vars
path = path.replace(/\.{2,}/gim, '.');
// path = aproperty.0.anotherproperty.arrayproperty.42.finalproperty
const keyPathChain = keyPath.split('.');
// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let property = instance as any;
for(const key of keyPathChain) {
for (const key of keyPathChain) {
property = property[key] ?? undefined;
}
@@ -50,6 +52,9 @@ export class ThingTranslateDefaultParser extends ThingTranslateParser {
}
}
/**
* TODO
*/
export function isDefined<T>(value?: T | null): value is T {
return typeof value !== 'undefined' && value !== null;
}

View File

@@ -14,14 +14,13 @@
*/
import {Injectable, OnDestroy, Pipe, PipeTransform} from '@angular/core';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {TranslateService} from '@ngx-translate/core';
import {isThing, SCThings, SCThingType} from '@openstapps/core';
import {Subscription} from 'rxjs';
import {ThingTranslateService} from './thing-translate.service';
// tslint:disable: member-ordering prefer-function-over-method completed-docs
@Injectable()
@Pipe({
name: 'thingTranslate',
@@ -29,28 +28,35 @@ import {ThingTranslateService} from './thing-translate.service';
})
export class ThingTranslatePipe implements PipeTransform, OnDestroy {
value: unknown;
lastKey?: string;
lastThing: SCThings;
onLangChange: Subscription;
constructor(private readonly translate: TranslateService,
// private readonly _ref: ChangeDetectorRef,
private readonly thingTranslate: ThingTranslateService) {
}
constructor(
private readonly translate: TranslateService,
// private readonly _ref: ChangeDetectorRef,
private readonly thingTranslate: ThingTranslateService,
) {}
updateValue(key: string, thing: SCThings): void {
this.value = this.thingTranslate.get(thing, key);
}
transform<T extends SCThings, P extends string[] | keyof T>(query: P, thing: T):
P extends keyof T ? T[P] : P | unknown {
transform<T extends SCThings, P extends string[] | string | keyof T>(
query: P,
thing: T,
): P extends keyof T ? T[P] : P | unknown {
if (typeof query !== 'string' || query.length <= 0) {
// tslint:disable-next-line:no-any
return query as any;
return query as never;
}
if (!isThing(thing)) {
throw new SyntaxError(`Wrong parameter in ThingTranslatePipe. Expected a valid SCThing, received: ${thing}`);
throw new SyntaxError(
`Wrong parameter in ThingTranslatePipe. Expected a valid SCThing, received: ${thing}`,
);
}
// store the params, in case they change
@@ -63,7 +69,7 @@ export class ThingTranslatePipe implements PipeTransform, OnDestroy {
this._dispose();
if (this.onLangChange?.closed ?? true) {
this.onLangChange = this.translate.onLangChange.subscribe((_event: LangChangeEvent) => {
this.onLangChange = this.translate.onLangChange.subscribe(() => {
if (typeof this.lastKey === 'string') {
this.lastKey = undefined; // we want to make sure it doesn't return the same value until it's been updated
this.updateValue(query, thing);
@@ -71,8 +77,7 @@ export class ThingTranslatePipe implements PipeTransform, OnDestroy {
});
}
// tslint:disable-next-line:no-any
return this.value as any;
return this.value as never;
}
/**
@@ -89,21 +94,26 @@ export class ThingTranslatePipe implements PipeTransform, OnDestroy {
}
}
@Injectable()
@Pipe({
name: 'propertyNameTranslate',
pure: false, // required to update the value when the promise is resolved
})
export class ThingPropertyNameTranslatePipe implements PipeTransform, OnDestroy {
export class ThingPropertyNameTranslatePipe
implements PipeTransform, OnDestroy
{
value: unknown;
lastKey?: string;
lastType: string;
onLangChange: Subscription;
constructor(private readonly translate: TranslateService,
private readonly thingTranslate: ThingTranslateService) {
}
constructor(
private readonly translate: TranslateService,
private readonly thingTranslate: ThingTranslateService,
) {}
updateValue(key: string, type: string): void {
this.value = this.thingTranslate.getPropertyName(type as SCThingType, key);
@@ -115,12 +125,15 @@ export class ThingPropertyNameTranslatePipe implements PipeTransform, OnDestroy
}
if (!isThing(thingOrType) && typeof thingOrType !== 'string') {
throw new SyntaxError(`Wrong parameter in ThingTranslatePipe. Expected a valid SCThing or String, received: ${thingOrType}`);
throw new SyntaxError(
`Wrong parameter in ThingTranslatePipe. Expected a valid SCThing or String, received: ${thingOrType}`,
);
}
// store the params, in case they change
this.lastKey = query;
this.lastType = typeof thingOrType === 'string' ? thingOrType : thingOrType.type;
this.lastType =
typeof thingOrType === 'string' ? thingOrType : thingOrType.type;
this.updateValue(query, this.lastType);
@@ -128,7 +141,7 @@ export class ThingPropertyNameTranslatePipe implements PipeTransform, OnDestroy
this._dispose();
if (this.onLangChange?.closed ?? true) {
this.onLangChange = this.translate.onLangChange.subscribe((_event: LangChangeEvent) => {
this.onLangChange = this.translate.onLangChange.subscribe(() => {
if (typeof this.lastKey === 'string') {
this.lastKey = undefined; // we want to make sure it doesn't return the same value until it's been updated
this.updateValue(query, this.lastType);

View File

@@ -15,58 +15,81 @@
import {Injectable, OnDestroy} from '@angular/core';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {SCLanguage, SCLanguageCode, SCThings, SCThingTranslator, SCThingType, SCTranslations} from '@openstapps/core';
import {
SCLanguage,
SCLanguageCode,
SCThings,
SCThingTranslator,
SCThingType,
SCTranslations,
} from '@openstapps/core';
import moment from 'moment';
import {Subscription} from 'rxjs';
import {isDefined, ThingTranslateParser} from './thing-translate.parser';
// export const DEFAULT_LANGUAGE = new InjectionToken<string>('DEFAULT_LANGUAGE');
// tslint:disable: member-ordering prefer-function-over-method newline-per-chained-call completed-docs
/* eslint-disable @typescript-eslint/member-ordering, class-methods-use-this, newline-per-chained-call, */
@Injectable({
providedIn: 'root',
})
export class ThingTranslateService implements OnDestroy {
onLangChange: Subscription;
translator: SCThingTranslator;
/**
*
* @param translateService Instance of Angular TranslateService
* @param parser An instance of the parser currently used
* @param language TODO
*/
constructor(private readonly translateService: TranslateService,
public parser: ThingTranslateParser){
this.translator = new SCThingTranslator((translateService.currentLang ?? translateService.defaultLang) as SCLanguageCode);
constructor(
private readonly translateService: TranslateService,
public parser: ThingTranslateParser,
) {
this.translator = new SCThingTranslator(
(translateService.currentLang ??
translateService.defaultLang) as SCLanguageCode,
);
/** set the default language from configuration */
this.onLangChange = this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
this.translator.language = event.lang as keyof SCTranslations<SCLanguage>;
moment.locale(event.lang);
});
this.onLangChange = this.translateService.onLangChange.subscribe(
(event: LangChangeEvent) => {
this.translator.language =
event.lang as keyof SCTranslations<SCLanguage>;
moment.locale(event.lang);
},
);
}
/**
* Returns the parsed result of the translations
*/
// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types
getParsedResult(target: object, key: string): any {
return this.parser.getValueFromKeyPath(target, key);
}
/**
* Gets the translated value of a key (or an array of keys)
*
* @param thing SCThing to get
* @param keyPath Path to the key
* @returns the translated key, or an object of translated keys
*/
public get(thing: SCThings, keyPath: string | string[]): string | number | boolean | object {
public get(
thing: SCThings,
keyPath: string | string[],
// eslint-disable-next-line @typescript-eslint/ban-types
): string | number | boolean | object {
if (!isDefined(keyPath) || keyPath.length === 0) {
throw new Error(`Parameter "keyPath" required`);
}
if (keyPath instanceof Array) {
return this.getParsedResult(this.translator.translate(thing), keyPath.join('.'));
if (Array.isArray(keyPath)) {
return this.getParsedResult(
this.translator.translate(thing),
keyPath.join('.'),
);
}
return this.getParsedResult(this.translator.translate(thing), keyPath);
@@ -74,24 +97,30 @@ export class ThingTranslateService implements OnDestroy {
/**
* Gets the translated value of a key (or an array of keys)
*
* @param type Type of the property
* @param keyPath Path to the key
* @returns the translated key, or an object of translated keys
*/
public getPropertyName(type: SCThingType, keyPath: string | string[]): string {
const translatedPropertyNames = this.translator.translatedPropertyNames(type);
public getPropertyName(
type: SCThingType,
keyPath: string | string[],
): string {
const translatedPropertyNames =
this.translator.translatedPropertyNames(type);
if (!isDefined(translatedPropertyNames)) {
throw new Error(`Parameter "type" is an invalid SCThingType`);
}
if (!isDefined(keyPath) || keyPath.length === 0) {
throw new Error(`Parameter "keyPath" required`);
}
if (keyPath instanceof Array) {
return this.getParsedResult(translatedPropertyNames!, keyPath.join('.'));
if (Array.isArray(keyPath)) {
return this.getParsedResult(translatedPropertyNames, keyPath.join('.'));
}
return this.getParsedResult(translatedPropertyNames!, keyPath);
return this.getParsedResult(translatedPropertyNames, keyPath);
}
// tslint:disable-next-line: completed-docs
ngOnDestroy() {
if (!this.onLangChange.closed) {
this.onLangChange.unsubscribe();