Skip to content

Commit

Permalink
Sonarcloud code smell fixes part I
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKinas committed Oct 30, 2023
1 parent de4cf4d commit d8bd002
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 112 deletions.
2 changes: 1 addition & 1 deletion app/proxy.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
// workaround for REPO-2260
onProxyRes: function (proxyRes) {
const header = proxyRes.headers['www-authenticate'];
if (header && header.startsWith('Basic')) {
if (header?.startsWith('Basic')) {
proxyRes.headers['www-authenticate'] = 'x' + header;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ if (environment.production) {
enableProdMode();
}

void platformBrowserDynamic().bootstrapModule(AppModule);
platformBrowserDynamic().bootstrapModule(AppModule);
2 changes: 1 addition & 1 deletion e2e/playwright/viewer/src/tests/viewer-protected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test.describe('viewer file', () => {
let folderId: string;
let fileDocxId: string;

test.beforeAll(async ({ fileAction, shareAction, favoritesPageAction: favoritesPageAction }) => {
test.beforeAll(async ({ fileAction, shareAction, favoritesPageAction }) => {
await apiClientFactory.setUpAcaBackend('hruser');
const node = await apiClientFactory.nodes.createNode('-my-', { name: randomFolderName, nodeType: 'cm:folder', relativePath: '/' });
folderId = node.entry.id;
Expand Down
56 changes: 25 additions & 31 deletions e2e/protractor/suites/actions/copy-move/copy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,53 +206,47 @@ describe('Copy content', () => {
await dataTable.doubleClickOnRowByName(source);
});

it('[C217135] Copy a file', async () => copyFile(file1, '', destinationPF));
it('[C217135] Copy a file', async () => copyFile(file1, destinationPF));

it('[C291888] Copy a folder with content', async () => copyFolderWithContent(folder1, '', destinationPF));
it('[C291888] Copy a folder with content', async () => copyFolderWithContent(folder1, destinationPF));

it('[C291889] Copy multiple items', async () => copyMultipleItems([file2, file3], '', destinationPF));
it('[C291889] Copy multiple items', async () => copyMultipleItems([file2, file3], destinationPF));

it('[C217137] Copy a file with a name that already exists on the destination', async () =>
copyFileWithNameThatAlreadyExists(existingFile, '', destinationPF));
copyFileWithNameThatAlreadyExists(existingFile, destinationPF));

it('[C217138] Copy a folder with a name that already exists on the destination', async () =>
copyFolderWithNameThatAlreadyExists(existingFolder, '', destinationPF));
copyFolderWithNameThatAlreadyExists(existingFolder, destinationPF));

it('[C280282] Copy items into a library', async () => copyItemsIntoLibrary([file1, folder1], '', folderSitePF));
it('[C280282] Copy items into a library', async () => copyItemsIntoLibrary([file1, folder1], folderSitePF));

it('[C217139] Copy locked file', async () =>
copyLockedFile(fileLocked1, '', destinationPF, () => {
copyLockedFile(fileLocked1, destinationPF, '', () => {
locationId = sourceId;
destinationId = destinationIdPF;
}));

it('[C217140] Copy folder that contains locked file', async () =>
copyFolderThatContainsLockedFile(folderWithLockedFiles, '', destinationPF, () => {
copyFolderThatContainsLockedFile(folderWithLockedFiles, destinationPF, '', () => {
locationId = folderWithLockedFilesId;
destinationId = destinationIdPF;
}));

it('[C217171] Undo copy of files', async () => undoCopyFile(file4, '', destinationPF));
it('[C217171] Undo copy of files', async () => undoCopyFile(file4, destinationPF));

it('[C217172] Undo copy of folders', async () => undoCopyFolder(folder2, '', destinationPF));
it('[C217172] Undo copy of folders', async () => undoCopyFolder(folder2, destinationPF));

it('[C217173] Undo copy of a file when a file with same name already exists on the destination', async () =>
undoCopyFileWithExistingName(fileInFolder, '', folder2, async () => {
undoCopyFileWithExistingName(fileInFolder, folder2, '', async () => {
await dataTable.doubleClickOnRowByName(folder1);
await dataTable.waitForHeader();
}));

it('[C217174] Undo copy of a folder when a folder with same name already exists on the destination', async () =>
undoCopyFolderWithExistingName(folderExisting, '', destinationPF));
undoCopyFolderWithExistingName(folderExisting, destinationPF));
});

async function baseCopy(
itemName: string | string[],
location: string,
destination: string,
undo: boolean = false,
expectedMsg: string = 'Copied 1 item'
) {
async function baseCopy(itemName: string | string[], location: string, destination: string, undo = false, expectedMsg = 'Copied 1 item') {
if (itemName instanceof Array) {
await dataTable.selectMultipleItems(itemName, location);
} else {
Expand Down Expand Up @@ -302,15 +296,15 @@ describe('Copy content', () => {
await baseCopy(itemName, location, destination, undo, expectedMsg);
}

async function copyFile(fileName: string, location: string = '', destination: string, doBefore?: () => void) {
async function copyFile(fileName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(fileName, location, destination, doBefore);
expect(await dataTable.isItemPresent(fileName)).toBe(true, `${fileName} not present in source folder`);
await page.clickPersonalFilesAndWait();
await dataTable.doubleClickOnRowByName(destination);
expect(await dataTable.isItemPresent(fileName)).toBe(true, `${fileName} not present in ${destination} folder`);
}

async function copyFolderWithContent(folderName: string, location: string = '', destination: string, doBefore?: () => void) {
async function copyFolderWithContent(folderName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(folderName, location, destination, doBefore);
expect(await dataTable.isItemPresent(folderName)).toBe(true, `${folderName} not present in source folder`);
await page.clickPersonalFilesAndWait();
Expand All @@ -322,7 +316,7 @@ describe('Copy content', () => {
expect(await dataTable.isItemPresent(fileInFolder)).toBe(true, `${fileInFolder} is not present in ${folderName} folder in ${destination}`);
}

async function copyMultipleItems(items: string[], location: string = '', destination: string, doBefore?: () => void) {
async function copyMultipleItems(items: string[], destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(items, location, destination, doBefore, false, 'Copied 2 items');
expect(await dataTable.isItemPresent(items[0])).toBe(true, `${items[0]} not present in source folder`);
expect(await dataTable.isItemPresent(items[1])).toBe(true, `${items[1]} not present in source folder`);
Expand All @@ -332,15 +326,15 @@ describe('Copy content', () => {
expect(await dataTable.isItemPresent(items[1])).toBe(true, `${items[1]} not present in ${destination} folder`);
}

async function copyFileWithNameThatAlreadyExists(fileName: string, location: string = '', destination: string, doBefore?: () => void) {
async function copyFileWithNameThatAlreadyExists(fileName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(fileName, location, destination, doBefore);
expect(await dataTable.isItemPresent(fileName)).toBe(true, `${fileName}.txt not present in source folder`);
await page.clickPersonalFilesAndWait();
await dataTable.doubleClickOnRowByName(destination);
expect(await dataTable.isItemPresent(fileName)).toBe(true, `${fileName}.txt not present in ${destination} folder`);
}

async function copyFolderWithNameThatAlreadyExists(folderName: string, location: string = '', destination: string, doBefore?: () => void) {
async function copyFolderWithNameThatAlreadyExists(folderName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(folderName, location, destination, doBefore);
expect(await dataTable.isItemPresent(folderName)).toBe(true, `${folderName} not present in source folder`);
await page.clickPersonalFilesAndWait();
Expand All @@ -351,7 +345,7 @@ describe('Copy content', () => {
expect(await dataTable.isItemPresent(file3InFolder)).toBe(true, `${file3InFolder} not present in ${destination} folder in ${folderName}`);
}

async function copyItemsIntoLibrary(items: string[], location: string = '', destination: string, doBefore?: () => void) {
async function copyItemsIntoLibrary(items: string[], destination: string, location = '', doBefore?: () => void) {
if (doBefore) {
doBefore();
}
Expand Down Expand Up @@ -382,7 +376,7 @@ describe('Copy content', () => {
}
}

async function copyLockedFile(fileName: string, location: string = '', destination: string, doBefore?: () => void) {
async function copyLockedFile(fileName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(fileName, location, destination, doBefore);
expect(await dataTable.isItemPresent(fileName)).toBe(true, `${fileName} not present in source folder`);
expect(await apis.nodes.isFileLockedByName(fileName, locationId)).toBe(true, `${fileName} not locked in ${location}`);
Expand All @@ -392,7 +386,7 @@ describe('Copy content', () => {
expect(await apis.nodes.isFileLockedByName(fileName, destinationId)).toBe(false, `${fileName} is locked in ${destination}`);
}

async function copyFolderThatContainsLockedFile(folderName: string, location: string = '', destination: string, doBefore?: () => void) {
async function copyFolderThatContainsLockedFile(folderName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(folderName, location, destination, doBefore);
expect(await dataTable.isItemPresent(folderName)).toBe(true, `${folderName} not present in source folder`);
await page.clickPersonalFilesAndWait();
Expand All @@ -412,7 +406,7 @@ describe('Copy content', () => {
);
}

async function undoCopyFile(fileName: string, location: string = '', destination: string, doBefore?: () => void) {
async function undoCopyFile(fileName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(fileName, location, destination, doBefore, true);
await dataTable.doubleClickOnRowByName(destination);
expect(await dataTable.isItemPresent(fileName)).toBe(false, `${fileName} present in ${destination} folder`);
Expand All @@ -421,7 +415,7 @@ describe('Copy content', () => {
expect(await dataTable.isEmpty()).toBe(true, 'Trash is not empty');
}

async function undoCopyFolder(folderName: string, location: string = '', destination: string, doBefore?: () => void) {
async function undoCopyFolder(folderName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(folderName, location, destination, doBefore, true);
await dataTable.doubleClickOnRowByName(destination);
expect(await dataTable.isItemPresent(folderName)).toBe(false, `${folderName} present in ${destination} folder`);
Expand All @@ -430,7 +424,7 @@ describe('Copy content', () => {
expect(await dataTable.isEmpty()).toBe(true, 'Trash is not empty');
}

async function undoCopyFileWithExistingName(fileName: string, location: string = '', destination: string, doBefore?: () => Promise<void>) {
async function undoCopyFileWithExistingName(fileName: string, destination: string, location = '', doBefore?: () => Promise<void>) {
await baseCopyDoBeforeAsync(fileName, location, destination, doBefore, true);
await dataTable.doubleClickOnRowByName(source);
await dataTable.doubleClickOnRowByName(folder2);
Expand All @@ -441,7 +435,7 @@ describe('Copy content', () => {
expect(await dataTable.isEmpty()).toBe(true, 'Trash is not empty');
}

async function undoCopyFolderWithExistingName(folderName: string, location: string = '', destination: string, doBefore?: () => void) {
async function undoCopyFolderWithExistingName(folderName: string, destination: string, location = '', doBefore?: () => void) {
await baseCopyDoBefore(folderName, location, destination, doBefore, true);
await dataTable.doubleClickOnRowByName(destination);
expect(await dataTable.isItemPresent(folderName)).toBe(true, `${folderName} not present in ${destination} folder`);
Expand Down
8 changes: 4 additions & 4 deletions e2e/protractor/suites/list-views/sort-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { AdminActions, LoginPage, RepoClient, FILES, BrowsingPage, DataTable, Cr
import { BrowserActions, ContentNodeSelectorDialogPage, DocumentListPage, PaginationPage, ViewerPage } from '@alfresco/adf-testing';

describe('Remember sorting', () => {
interface nodesIds {
interface NodesIds {
[index: string]: string;
}

Expand All @@ -38,9 +38,9 @@ describe('Remember sorting', () => {
const folderToMove = `folder1`;
const folderToContain = `folder2`;
const uiCreatedFolder = `folder3`;
const filesIdsUser1: nodesIds = {};
const filesIdsUser2: nodesIds = {};
const folderIds: nodesIds = {};
const filesIdsUser1: NodesIds = {};
const filesIdsUser2: NodesIds = {};
const folderIds: NodesIds = {};

const testData = {
user1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,7 @@ export class RuleActionUiComponent implements ControlValueAccessor, OnInit, OnCh
setDefaultParameters() {
this.parameters = {};
(this.selectedActionDefinition?.parameterDefinitions ?? []).forEach((paramDef: ActionParameterDefinition) => {
switch (paramDef.type) {
case 'd:boolean':
this.parameters[paramDef.name] = false;
break;
default:
this.parameters[paramDef.name] = '';
}
this.parameters[paramDef.name] = paramDef.type === 'd:boolean' ? false : '';
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe('PreviewComponent', () => {
});

it('should return empty nearest nodes for crashed fields id request', async () => {
spyOn(component, 'getFileIds').and.returnValue(Promise.reject('err'));
spyOn(component, 'getFileIds').and.returnValue(Promise.reject(new Error('err')));

const nearest = await component.getNearestNodes('node1', 'folder1');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class PreviewComponent extends PageComponent implements OnInit, OnDestroy
this.node = await this.contentApi.getNodeInfo(id).toPromise();
this.store.dispatch(new SetSelectedNodesAction([{ entry: this.node }]));

if (this.node && this.node.isFile) {
if (this.node?.isFile) {
const nearest = await this.getNearestNodes(this.node.id, this.node.parentId);

this.previousNodeId = nearest.left;
Expand All @@ -196,11 +196,9 @@ export class PreviewComponent extends PageComponent implements OnInit, OnDestroy

@HostListener('document:keydown', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
const key = event.keyCode;
const rightArrow = 39;
const leftArrow = 37;
const key = event.key;

if (key === rightArrow || key === leftArrow) {
if (key === 'ArrowRight' || key === 'ArrowLeft') {
event.preventDefault();
event.stopImmediatePropagation();
}
Expand Down Expand Up @@ -410,19 +408,11 @@ export class PreviewComponent extends PageComponent implements OnInit, OnDestroy
}

items.sort((a: any, b: any) => {
let left = ObjectUtils.getValue(a, key);
if (left) {
left = left instanceof Date ? left.valueOf().toString() : left.toString();
} else {
left = '';
}
let left = ObjectUtils.getValue(a, key) ?? '';
left = left instanceof Date ? left.valueOf().toString() : left.toString();

let right = ObjectUtils.getValue(b, key);
if (right) {
right = right instanceof Date ? right.valueOf().toString() : right.toString();
} else {
right = '';
}
let right = ObjectUtils.getValue(b, key) ?? '';
right = right instanceof Date ? right.valueOf().toString() : right.toString();

return direction === 'asc' ? left.localeCompare(right, undefined, options) : right.localeCompare(left, undefined, options);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export class LocationLinkComponent implements OnInit {
ngOnInit() {
if (this.context) {
const node: NodeEntry = this.context.row.node;
if (node && node.entry && node.entry.path) {
if (node?.entry && node?.entry?.path) {
const path = node.entry.path;

if (path && path.name && path.elements) {
if (path?.name && path?.elements) {
if (this.showLocation) {
this.displayText = of(path.name.substring(1).replace(/\//g, ' &#8250; '));
} else {
Expand Down Expand Up @@ -144,7 +144,9 @@ export class LocationLinkComponent implements OnInit {

let result: string = null;

const elements = path.elements.map((e) => Object.assign({}, e));
const elements = path.elements.map((e) => {
return { ...e };
});
const personalFiles = this.translationService.instant('APP.BROWSE.PERSONAL.TITLE');
const fileLibraries = this.translationService.instant('APP.BROWSE.LIBRARIES.TITLE');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class ToggleSharedComponent implements OnInit, OnDestroy {
this.selectionState = selectionState;

this.isShared =
(this.selectionState.first && this.selectionState.first.entry && (this.selectionState.first.entry as any).sharedByUser) ||
(this.selectionState?.first?.entry && (this.selectionState.first.entry as any).sharedByUser) ||
!!this.selectionState?.first?.entry?.properties?.['qshare:sharedId'];

this.selectionLabel = this.isShared ? 'APP.ACTIONS.SHARE_EDIT' : 'APP.ACTIONS.SHARE';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ContextMenuItemComponent {
}

private hasClickAction(actionRef: ContentActionRef): boolean {
return !!(actionRef && actionRef.actions && actionRef.actions.click);
return !!actionRef?.actions?.click;
}

trackByActionId(_: number, obj: ContentActionRef): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export class OutsideEventDirective implements OnInit, OnDestroy {
@Output()
clickOutside: EventEmitter<null> = new EventEmitter();

constructor() {}

ngOnInit() {
this.subscriptions = this.subscriptions.concat([
fromEvent(document.body, 'click')
Expand All @@ -54,11 +52,9 @@ export class OutsideEventDirective implements OnInit, OnDestroy {
private findAncestor(el: Element): boolean {
const className = 'aca-context-menu';

if (el.classList.contains(className)) {
return true;
while (el && !el.classList.contains(className)) {
el = el.parentElement;
}
// eslint-disable-next-line curly
while ((el = el.parentElement) && !el.classList.contains(className));
return !!el;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ export class ContextMenuService {
}

private createInjector(contextmenuOverlayRef: ContextMenuOverlayRef): Injector {
const injectionTokens = new WeakMap();

injectionTokens.set(ContextMenuOverlayRef, contextmenuOverlayRef);
injectionTokens.set(CONTEXT_MENU_DIRECTION, this.direction);

return Injector.create({
parent: this.injector,
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class FavoriteLibrariesComponent extends PageComponent implements OnInit
}

navigateTo(node: SiteEntry) {
if (node && node.entry && node.entry.guid) {
if (node?.entry?.guid) {
this.store.dispatch(new NavigateLibraryAction(node.entry.guid, 'favorite/libraries'));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class FavoritesComponent extends PageComponent implements OnInit {
const { isFolder, id } = favorite;

// TODO: rework as it will fail on non-English setups
const isSitePath = (path: PathInfo): boolean => path && path.elements && path.elements.some(({ name }: PathElement) => name === 'Sites');
const isSitePath = (path: PathInfo): boolean => path?.elements?.some(({ name }: PathElement) => name === 'Sites');

if (isFolder) {
this.contentApi
Expand All @@ -97,7 +97,7 @@ export class FavoritesComponent extends PageComponent implements OnInit {
}

onNodeDoubleClick(node: NodeEntry) {
if (node && node.entry) {
if (node?.entry) {
if (node.entry.isFolder) {
this.navigate(node.entry);
}
Expand Down
Loading

0 comments on commit d8bd002

Please sign in to comment.