From 7862ba68dc7e153a26e4d03e221bad23f7f486c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dabiel=20Gonz=C3=A1lez=20Ramos?= Date: Tue, 27 Jun 2023 18:27:04 +0300 Subject: [PATCH] fix(sonarlint): code smells and security reviews (#303) --- .../_storybook/bq-notification.stories.tsx | 5 ++--- .../bee-q/src/shared/test-utils/computedStyle.ts | 2 +- packages/bee-q/src/shared/utils/getRandom.ts | 12 ++++++------ sonar-project.properties | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/bee-q/src/components/notification/_storybook/bq-notification.stories.tsx b/packages/bee-q/src/components/notification/_storybook/bq-notification.stories.tsx index 00cd51cde..088fbb41d 100644 --- a/packages/bee-q/src/components/notification/_storybook/bq-notification.stories.tsx +++ b/packages/bee-q/src/components/notification/_storybook/bq-notification.stories.tsx @@ -3,6 +3,7 @@ import { html } from 'lit-html'; import mdx from './bq-notification.mdx'; +import { getRandomFromArray } from '../../../shared/utils'; import { NOTIFICATION_TYPE } from '../bq-notification.types'; const meta: Meta = { @@ -132,9 +133,7 @@ export const Warning: Story = { export const Stacked: Story = { render: (args: Args) => { const onButtonClick = () => { - const getRandom = (arr: string[]) => arr[Math.floor(Math.random() * arr.length)]; - const type = getRandom(NOTIFICATION_TYPE as unknown as string[]); - + const [type] = getRandomFromArray(NOTIFICATION_TYPE as unknown as string[], 1); const notification = Object.assign(document.createElement('bq-notification'), { type, autoDismiss: args['auto-dismiss'], diff --git a/packages/bee-q/src/shared/test-utils/computedStyle.ts b/packages/bee-q/src/shared/test-utils/computedStyle.ts index f713954e2..9f71bb530 100644 --- a/packages/bee-q/src/shared/test-utils/computedStyle.ts +++ b/packages/bee-q/src/shared/test-utils/computedStyle.ts @@ -21,7 +21,7 @@ export const computedStyle = ( throw new Error(`Could not find element ${lightDomSelector}`); } - if (!!shadowDomSelector) { + if (shadowDomSelector) { element = element.shadowRoot.querySelector(shadowDomSelector); if (!element) { diff --git a/packages/bee-q/src/shared/utils/getRandom.ts b/packages/bee-q/src/shared/utils/getRandom.ts index ca0b390b8..65e06b081 100644 --- a/packages/bee-q/src/shared/utils/getRandom.ts +++ b/packages/bee-q/src/shared/utils/getRandom.ts @@ -5,17 +5,17 @@ * @param {Number} n - The number of random items to take * @returns {Array} An array with the number of random items specified */ -export const getRandomFromArray = (arr: Array, n: number): Array => { - let len = arr.length; +export const getRandomFromArray = (arr: T[], n: number): T[] => { + let length = arr.length; const result = new Array(n); - const taken = new Array(len); + const taken = new Array(length); - if (n > len) throw new RangeError('getRandom: more elements taken than available'); + if (n > length) throw new RangeError('getRandom: more elements taken than available'); while (n--) { - const x = Math.floor(Math.random() * len); + const x = Date.now() % length; result[n] = arr[x in taken ? taken[x] : x]; - taken[x] = --len in taken ? taken[len] : len; + taken[x] = --length in taken ? taken[length] : length; } return result; }; diff --git a/sonar-project.properties b/sonar-project.properties index 20909a153..a4790ba6c 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=endava sonar.projectKey=Endava_bee-q -sonar.cpd.exclusions=packages/bee-q/src/components/*.tsx +sonar.cpd.exclusions=packages/bee-q/src/components/*.ts,packages/bee-q/src/components/*.tsx sonar.test.inclusions=**/*.spec.* sonar.testExecutionReportPaths=packages/bee-q/test-report.xml sonar.javascript.lcov.reportPaths=packages/bee-q/coverage/lcov.info