From b73a3f6702258b1390e3212a01398cb3b6f505d3 Mon Sep 17 00:00:00 2001 From: MichalKinas Date: Mon, 30 Oct 2023 11:48:15 +0100 Subject: [PATCH] Add new ESLint rules to cover fixed SonarCloud issues --- .eslintrc.json | 12 +++++++--- .../content-management.service.spec.ts | 2 +- .../services/content-management.service.ts | 24 +++++++++---------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 2049cce6dc..9c44ada551 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -113,7 +113,9 @@ "@angular-eslint/component-class-suffix": "error", "@angular-eslint/directive-class-suffix": "error", "@angular-eslint/no-conflicting-lifecycle": "error", - "@typescript-eslint/no-floating-promises": "off", + "@typescript-eslint/no-floating-promises": "warn", + "@typescript-eslint/no-misused-promises": "warn", + "@typescript-eslint/default-param-last": "warn", "@angular-eslint/no-input-rename": "error", "@angular-eslint/no-output-native": "error", "@angular-eslint/no-output-on-prefix": "error", @@ -122,7 +124,7 @@ "@angular-eslint/no-outputs-metadata-property": "error", "@angular-eslint/use-lifecycle-interface": "error", "@angular-eslint/use-pipe-transform-interface": "error", - "@typescript-eslint/prefer-optional-chain": "warn", + "@typescript-eslint/prefer-optional-chain": "error", "@typescript-eslint/no-misused-new": "error", "@typescript-eslint/no-non-null-assertion": "error", "@typescript-eslint/prefer-function-type": "error", @@ -204,6 +206,7 @@ "newline-per-chained-call": "off", "prefer-rest-params": "error", "prefer-spread": "error", + "prefer-promise-reject-errors": "error", "import/order": "off", "max-len": [ "error", @@ -226,6 +229,8 @@ "no-caller": "error", "no-global-assign": "error", "no-cond-assign": "error", + "no-constant-binary-expression": "error", + "no-useless-rename": "error", "no-console": [ "error", { @@ -324,7 +329,8 @@ { "files": ["*.spec.ts", "*.test.ts"], "rules": { - "@typescript-eslint/no-floating-promises": "off", + "@typescript-eslint/no-floating-promises": "warn", + "@typescript-eslint/no-misused-promises": "warn", "max-lines": "off" } } diff --git a/projects/aca-content/src/lib/services/content-management.service.spec.ts b/projects/aca-content/src/lib/services/content-management.service.spec.ts index da8853e773..327845fa5a 100644 --- a/projects/aca-content/src/lib/services/content-management.service.spec.ts +++ b/projects/aca-content/src/lib/services/content-management.service.spec.ts @@ -1503,7 +1503,7 @@ describe('ContentManagementService', () => { })); it('should raise error when unlock node fails', fakeAsync(() => { - spyOn(contentApi, 'unlockNode').and.callFake(() => new Promise((_resolve, reject) => reject('error'))); + spyOn(contentApi, 'unlockNode').and.callFake(() => new Promise((_resolve, reject) => reject(new Error('error')))); spyOn(store, 'dispatch').and.callThrough(); store.dispatch(new UnlockWriteAction({ entry: { id: 'node-id', name: 'some-file' } })); tick(); diff --git a/projects/aca-content/src/lib/services/content-management.service.ts b/projects/aca-content/src/lib/services/content-management.service.ts index 48367d5bc9..6adb643ccb 100644 --- a/projects/aca-content/src/lib/services/content-management.service.ts +++ b/projects/aca-content/src/lib/services/content-management.service.ts @@ -122,7 +122,7 @@ export class ContentManagementService { } manageVersions(node: any, focusedElementOnCloseSelector?: string) { - if (node && node.entry) { + if (node?.entry) { // shared and favorite const id = node.entry.nodeId || (node as any).entry.guid; @@ -137,7 +137,7 @@ export class ContentManagementService { } manageAspects(node: any, focusedElementOnCloseSelector?: string) { - if (node && node.entry) { + if (node?.entry) { // shared and favorite const id = node.entry.nodeId || (node as any).entry.guid; @@ -174,7 +174,7 @@ export class ContentManagementService { } shareNode(node: any, focusedElementOnCloseSelector?: string): void { - if (node && node.entry) { + if (node?.entry) { // shared and favorite const id = node.entry.nodeId || (node as any).entry.guid; @@ -276,7 +276,7 @@ export class ContentManagementService { this.focusAfterClose(this.createMenuButtonSelector); }), map((node: SiteEntry) => { - if (node && node.entry && node.entry.guid) { + if (node?.entry?.guid) { return node.entry.guid; } return null; @@ -557,7 +557,7 @@ export class ContentManagementService { errorJson = JSON.parse(error.message); } catch {} - if (errorJson && errorJson.error && errorJson.error.statusCode === 403) { + if (errorJson?.error?.statusCode === 403) { i18nMessageString = 'APP.MESSAGES.ERRORS.PERMISSION'; } @@ -609,8 +609,8 @@ export class ContentManagementService { } private undoMoveNodes(moveResponse, selectionParentId: string) { - const movedNodes = moveResponse && moveResponse['succeeded'] ? moveResponse['succeeded'] : []; - const partiallyMovedNodes = moveResponse && moveResponse['partiallySucceeded'] ? moveResponse['partiallySucceeded'] : []; + const movedNodes = moveResponse?.['succeeded'] ?? []; + const partiallyMovedNodes = moveResponse?.['partiallySucceeded'] ?? []; const restoreDeletedNodesBatch = this.nodeActionsService.moveDeletedEntries.map((folderEntry) => this.contentApi.restoreNode(folderEntry.nodeId || folderEntry.id).pipe(map((node) => node.entry)) @@ -623,7 +623,7 @@ export class ContentManagementService { const revertMoveBatch = this.nodeActionsService .flatten(nodesToBeMovedBack) - .filter((node) => node.entry || (node.itemMoved && node.itemMoved.entry)) + .filter((node) => node.entry || node.itemMoved?.entry) .map((node) => { if (node.itemMoved) { return this.nodeActionsService.moveNodeAction(node.itemMoved.entry, node.initialParentId); @@ -647,7 +647,7 @@ export class ContentManagementService { errorJson = JSON.parse(error.message); } catch {} - if (errorJson && errorJson.error && errorJson.error.statusCode === 403) { + if (errorJson?.error?.statusCode === 403) { message = 'APP.MESSAGES.ERRORS.PERMISSION'; } @@ -1009,9 +1009,9 @@ export class ContentManagementService { } private showMoveMessage(nodes: Array, info: any, moveResponse?: any) { - const succeeded = moveResponse && moveResponse['succeeded'] ? moveResponse['succeeded'].length : 0; - const partiallySucceeded = moveResponse && moveResponse['partiallySucceeded'] ? moveResponse['partiallySucceeded'].length : 0; - const failures = moveResponse && moveResponse['failed'] ? moveResponse['failed'].length : 0; + const succeeded = moveResponse?.['succeeded'].length ?? 0; + const partiallySucceeded = moveResponse?.['partiallySucceeded'].length ?? 0; + const failures = moveResponse?.['failed'].length ?? 0; let successMessage = ''; let partialSuccessMessage = '';