Skip to content

Commit

Permalink
refactor(shared/sai-editor): enable strict mode (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoBorzi authored May 22, 2024
1 parent 25fe2b0 commit 790101c
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class HandlerService<T extends TableRow> extends SubscriptionHan
}

/* istanbul ignore next */ // TODO: fix coverage
get parsedSelected(): Partial<T> {
get parsedSelected(): { entryorguid: number; source_type: number } {
return JSON.parse(this.selected);
}

Expand Down
18 changes: 9 additions & 9 deletions libs/shared/sai-editor/src/sai-comment-generator.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed, waitForAsync } from '@angular/core/testing';

import { SAI_TYPES, SmartScripts } from '@keira/shared/acore-world-model';
import { of } from 'rxjs';
import { lastValueFrom, of } from 'rxjs';
import {
DYNAMIC_FLAGS,
EVENT_FLAGS,
Expand Down Expand Up @@ -41,16 +41,16 @@ describe('SaiCommentGeneratorService', () => {

beforeEach(() => {
const queryService = TestBed.inject(MysqlQueryService);
spyOn(queryService, 'getCreatureNameById').and.callFake((i) => of(mockCreatureNameById + i).toPromise());
spyOn(queryService, 'getCreatureNameByGuid').and.callFake((i) => of(mockCreatureNameByGuid + i).toPromise());
spyOn(queryService, 'getGameObjectNameById').and.callFake((i) => of(mockGameobjectNameById + i).toPromise());
spyOn(queryService, 'getGameObjectNameByGuid').and.callFake((i) => of(mockGameobjectNameByGuid + i).toPromise());
spyOn(queryService, 'getQuestTitleById').and.callFake((i) => of(mockQuestTitleById + i).toPromise());
spyOn(queryService, 'getItemNameById').and.callFake((i) => of(mockItemNameById + i).toPromise());
spyOn(queryService, 'getQuestTitleByCriteria').and.callFake((i) => of(mockQuestTitleByCriteria + i).toPromise());
spyOn(queryService, 'getCreatureNameById').and.callFake((i) => lastValueFrom(of(mockCreatureNameById + i)));
spyOn(queryService, 'getCreatureNameByGuid').and.callFake((i) => lastValueFrom(of(mockCreatureNameByGuid + i)));
spyOn(queryService, 'getGameObjectNameById').and.callFake((i) => lastValueFrom(of(mockGameobjectNameById + i)));
spyOn(queryService, 'getGameObjectNameByGuid').and.callFake((i) => lastValueFrom(of(mockGameobjectNameByGuid + i)));
spyOn(queryService, 'getQuestTitleById').and.callFake((i) => lastValueFrom(of(mockQuestTitleById + i)));
spyOn(queryService, 'getItemNameById').and.callFake((i) => lastValueFrom(of(mockItemNameById + i)));
spyOn(queryService, 'getQuestTitleByCriteria').and.callFake((i) => lastValueFrom(of(mockQuestTitleByCriteria + i)));

const sqliteQueryService = TestBed.inject(SqliteQueryService);
spyOn(sqliteQueryService, 'getSpellNameById').and.callFake((i) => of(mockGetSpellNameById + i).toPromise());
spyOn(sqliteQueryService, 'getSpellNameById').and.callFake((i) => lastValueFrom(of(mockGetSpellNameById + i)));
});

it('should correctly handle linked events', waitForAsync(async () => {
Expand Down
8 changes: 4 additions & 4 deletions libs/shared/sai-editor/src/sai-comment-generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class SaiCommentGeneratorService {
}

/* Get previous script of links chain */
private getPreviousScriptLink(rows: SmartScripts[], smartScript: SmartScripts): SmartScripts {
private getPreviousScriptLink(rows: SmartScripts[], smartScript: SmartScripts): SmartScripts | null {
if (smartScript.id === 0) {
return null;
}
Expand Down Expand Up @@ -256,7 +256,7 @@ export class SaiCommentGeneratorService {
}

if (actionLine.indexOf('_followGroupParamTwo_') > -1) {
let _followGroupParamTwo_ = '[Unknown Follow Type]';
let _followGroupParamTwo_: string;
switch (Number(smartScript.action_param2)) {
case 1:
_followGroupParamTwo_ = 'Circle';
Expand Down Expand Up @@ -1027,9 +1027,9 @@ export class SaiCommentGeneratorService {
name: string, // the name of the creature or gameobject
): Promise<string> {
const smartScriptLink = this.getPreviousScriptLink(rows, smartScript);
return `${await this.generateEventComment(smartScript, name, smartScriptLink)} - ${await this.generateActionComment(
return `${await this.generateEventComment(smartScript, name, smartScriptLink as SmartScripts)} - ${await this.generateActionComment(
smartScript,
smartScriptLink,
smartScriptLink as SmartScripts,
)}`;
}
}
2 changes: 1 addition & 1 deletion libs/shared/sai-editor/src/sai-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class SaiEditorComponent extends MultiRowEditorComponent<SmartScripts> im
}

get entryOrGuid(): number {
return this.handlerService.parsedSelected.entryorguid;
return this.handlerService.parsedSelected.entryorguid as number;
}

getName(defaultParamName: string, value: string | undefined): string {
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/sai-editor/src/sai-editor.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('SAI Editor Service', () => {

describe('when templateQuery is null', () => {
beforeEach(() => {
handlerService['_templateQuery'] = null;
handlerService['_templateQuery'] = null as any;
});

it('updateFullQuery should correctly work', () => {
Expand Down
11 changes: 6 additions & 5 deletions libs/shared/sai-editor/src/sai-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SAI_ID_2, SAI_ID_FIELDS, SAI_TABLE, SAI_TYPES, SmartScripts } from '@ke
import { MultiRowComplexKeyEditorService } from '@keira/shared/base-abstract-classes';
import { SaiCommentGeneratorService } from './sai-comment-generator.service';
import { SaiHandlerService } from './sai-handler.service';
import { lastValueFrom, Observable } from 'rxjs';

@Injectable({
providedIn: 'root',
Expand All @@ -17,7 +18,7 @@ export class SaiEditorService extends MultiRowComplexKeyEditorService<SmartScrip
}

/* istanbul ignore next */ // because of: https://github.com/gotwarlost/istanbul/issues/690
constructor(protected handlerService: SaiHandlerService) {
constructor(protected override readonly handlerService: SaiHandlerService) {
super(SmartScripts, SAI_TABLE, SAI_ID_FIELDS, SAI_ID_2, handlerService);
}

Expand All @@ -35,7 +36,7 @@ export class SaiEditorService extends MultiRowComplexKeyEditorService<SmartScrip
}
}

protected updateDiffQuery(): void {
protected override updateDiffQuery(): void {
super.updateDiffQuery();

if (this.handlerService.templateQuery && this._diffQuery) {
Expand All @@ -45,7 +46,7 @@ export class SaiEditorService extends MultiRowComplexKeyEditorService<SmartScrip
this.updateEditorStatus();
}

protected checkRowsCorrectness(): void {
protected override checkRowsCorrectness(): void {
this._errors = [];

const links = new Set();
Expand Down Expand Up @@ -73,7 +74,7 @@ export class SaiEditorService extends MultiRowComplexKeyEditorService<SmartScrip
}
}

protected onRowSelected(): void {
protected override onRowSelected(): void {
if (this.handlerService.parsedSelected.source_type === SAI_TYPES.SAI_TYPE_TIMED_ACTIONLIST) {
this._form.controls.event_type.disable();
this._form.controls.event_param3.disable();
Expand Down Expand Up @@ -116,7 +117,7 @@ export class SaiEditorService extends MultiRowComplexKeyEditorService<SmartScrip
structuredClone(this._newRows),
{ ...row },
/* istanbul ignore next */
await this.handlerService.getName()?.toPromise(),
this.handlerService.getName() ? await lastValueFrom(this.handlerService.getName() as Observable<string>) : '',
);
}
}
6 changes: 3 additions & 3 deletions libs/shared/sai-editor/src/sai-handler.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('SaiHandlerService', () => {

service.select(false, { source_type: sourceType, entryorguid: id });

expect(service.templateQuery).toEqual(expectedQuery);
expect(service.templateQuery).toEqual(expectedQuery as string);
});
}

Expand Down Expand Up @@ -126,7 +126,7 @@ describe('SaiHandlerService', () => {
entryorguid: 123,
name: mockName,
returnValue: [],
expectedName: null,
expectedName: null as any,
expectedQuery: 'SELECT name FROM creature_template WHERE entry = 123',
},
{
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('SaiHandlerService', () => {
expect(querySpy).toHaveBeenCalledWith(expectedQuery);
});

service.select(false, { source_type: null, entryorguid: -123 }, mockName, false);
service.select(false, { source_type: undefined, entryorguid: -123 }, mockName, false);

querySpy.and.returnValue(of());
service.getName().subscribe((actualName) => {
Expand Down
14 changes: 7 additions & 7 deletions libs/shared/sai-editor/src/sai-handler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SaiHandlerService extends ComplexKeyHandlerService<SmartScripts> {
[SAI_TABLE]: false,
};

protected _templateQuery: string;
protected _templateQuery!: string;
get templateQuery(): string {
return this._templateQuery;
}
Expand All @@ -36,13 +36,13 @@ export class SaiHandlerService extends ComplexKeyHandlerService<SmartScripts> {
);
}

select(isNew: boolean, id: Partial<SmartScripts>, name: any = null, navigate: boolean = true) {
override select(isNew: boolean, id: Partial<SmartScripts>, name: any = null, navigate: boolean = true) {
super.select(isNew, id, name, navigate);
this._templateQuery = this.getTemplateQuery();
this._templateQuery = this.getTemplateQuery() as string;
}

protected getTemplateQuery(): string {
const selected: Partial<SmartScripts> = JSON.parse(this.selected);
protected getTemplateQuery(): string | null {
const selected: { entryorguid: number; source_type: number } = JSON.parse(this.selected);

if (selected.entryorguid < 0) {
// TODO: add support for GUIDs, if needed
Expand All @@ -67,8 +67,8 @@ export class SaiHandlerService extends ComplexKeyHandlerService<SmartScripts> {
}
}

getName(): Observable<string> {
const sai = this.parsedSelected;
getName(): Observable<string | null> {
const sai = this.parsedSelected as { entryorguid: number; source_type: number };
let query: string;

if (sai.source_type === SAI_TYPES.SAI_TYPE_CREATURE || sai.source_type === SAI_TYPES.SAI_TYPE_TIMED_ACTIONLIST) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SaiTopBarComponentPage extends PageObject<TestHostComponent> {
imports: [SaiTopBarComponent, RouterTestingModule, TranslateTestingModule],
})
class TestHostComponent {
@ViewChild(SaiTopBarComponent, { static: true }) child: SaiTopBarComponent;
@ViewChild(SaiTopBarComponent, { static: true }) child!: SaiTopBarComponent;
constructor(public handlerService: SaiHandlerService) {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeDetectionStrategy, Component, inject, Input, OnInit } from '@angular/core';
import { SAI_TYPES, SmartScripts } from '@keira/shared/acore-world-model';
import { SAI_TYPES } from '@keira/shared/acore-world-model';
import { SaiHandlerService } from '../sai-handler.service';

import { SubscriptionHandler } from '@keira/shared/utils';
Expand All @@ -16,16 +16,16 @@ import { MysqlQueryService } from '@keira/shared/db-layer';
export class SaiTopBarComponent extends SubscriptionHandler implements OnInit {
readonly queryService = inject(MysqlQueryService);

@Input() public handler: SaiHandlerService;
@Input({ required: true }) public handler!: SaiHandlerService;

private _selectedText: string;
private _selectedText!: string;

get selectedText() {
return this._selectedText;
}

async ngOnInit() {
const selected: Partial<SmartScripts> = JSON.parse(this.handler.selected);
const selected: { entryorguid: number; source_type: number } = JSON.parse(this.handler.selected);

switch (selected.source_type) {
case SAI_TYPES.SAI_TYPE_CREATURE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { MysqlQueryService } from '@keira/shared/db-layer';
imports: [NgxDatatableModule, TimedActionlistComponent],
})
class TestHostComponent {
@ViewChild(TimedActionlistComponent) child: TimedActionlistComponent;
creatureId: string;
@ViewChild(TimedActionlistComponent) child!: TimedActionlistComponent;
creatureId!: string;
}

class TimedActionlistPage extends PageObject<TestHostComponent> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import { MysqlQueryService } from '@keira/shared/db-layer';
imports: [NgxDatatableModule, AsyncPipe],
})
export class TimedActionlistComponent implements OnChanges {
@Input() creatureId: string | number;
@Input({ required: true }) creatureId!: string | number;

private readonly queryService = inject(MysqlQueryService);

readonly DTCFG = DTCFG;

private _timedActionLists$: Observable<SmartScripts[]>;
private _timedActionLists$!: Observable<SmartScripts[]>;
get timedActionlists$(): Observable<SmartScripts[]> {
return this._timedActionLists$;
}
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/sai-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"target": "es2022",
"useDefineForClassFields": false,
"forceConsistentCasingInFileNames": true,
// "strict": true,
// "noImplicitOverride": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
Expand Down

0 comments on commit 790101c

Please sign in to comment.