From 17399398720ae413af1438f24fce9f2cbb7092fb Mon Sep 17 00:00:00 2001 From: barsnes Date: Wed, 14 Aug 2024 13:34:04 +0200 Subject: [PATCH 1/3] chore: replace `forEach` with `for..of` --- biome.jsonc | 2 +- packages/cli/bin/designsystemet.ts | 4 +-- .../src/migrations/codemods/css/plugins.ts | 30 +++++++++---------- .../codemods/jsx/classname-prefix.ts | 20 ++++++------- .../cli/src/tokens/utils/permutateThemes.ts | 4 +-- .../src/components/form/Combobox/Combobox.tsx | 4 +-- .../form/Combobox/internal/ComboboxInput.tsx | 8 ++--- .../form/NativeSelect/NativeSelect.test.tsx | 4 +-- 8 files changed, 37 insertions(+), 39 deletions(-) diff --git a/biome.jsonc b/biome.jsonc index 1d85a15174..3a19c8636f 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -64,7 +64,7 @@ "useAriaPropsForRole": "off" }, "complexity": { - "noForEach": "off" // TODO: Enable this rule + "noForEach": "error" // TODO: Enable this rule }, "correctness": { "useExhaustiveDependencies": "off" diff --git a/packages/cli/bin/designsystemet.ts b/packages/cli/bin/designsystemet.ts index dd19bc2e90..709c2d8ec1 100644 --- a/packages/cli/bin/designsystemet.ts +++ b/packages/cli/bin/designsystemet.ts @@ -34,9 +34,9 @@ program const { glob, list } = opts; if (list) { - Object.keys(migrations).forEach((key) => { + for (const key of Object.keys(migrations)) { console.log(key); - }); + } } else if (migrationKey) { const migration = migrations[migrationKey as keyof typeof migrations]; if (!migration) { diff --git a/packages/cli/src/migrations/codemods/css/plugins.ts b/packages/cli/src/migrations/codemods/css/plugins.ts index 974423cf15..96dfff55e0 100644 --- a/packages/cli/src/migrations/codemods/css/plugins.ts +++ b/packages/cli/src/migrations/codemods/css/plugins.ts @@ -17,13 +17,13 @@ export const cssClassRename: PluginGenerator = (dictionary) => ({ if (!selector) return; - Object.entries(dictionary).forEach(([from, to]) => { + for (const [from, to] of Object.entries(dictionary)) { if (!selector.includes(from)) return; const newSelector = selector.replace(new RegExp(from, 'g'), to); rule.selector = newSelector; - }); + } }, }); @@ -34,23 +34,21 @@ export const cssVarRename: PluginGenerator = (dictionary) => ({ const deleted = new Set(); - Object.entries(dictionary).forEach(([from, to]) => { + for (const [from, to] of Object.entries(dictionary)) { if (!R.isEmpty(to)) { - switch (true) { - case R.includes(from, value): - to === '[delete]' && deleted.add(deleteMsg(decl, from)); - decl.value = value.replace(from, to); - break; - - case R.includes(from, prop): - if (decl.variable) { - to === '[delete]' && deleted.add(deleteMsg(decl, from)); - decl.prop = prop.replace(from, to); - break; - } + if (R.includes(from, value)) { + if (to === '[delete]') { + deleted.add(deleteMsg(decl, from)); + } + decl.value = value.replace(from, to); + } else if (R.includes(from, prop) && decl.variable) { + if (to === '[delete]') { + deleted.add(deleteMsg(decl, from)); + } + decl.prop = prop.replace(from, to); } } - }); + } if (deleted.size > 0) { Array.from(deleted).forEach(printDelete); diff --git a/packages/cli/src/migrations/codemods/jsx/classname-prefix.ts b/packages/cli/src/migrations/codemods/jsx/classname-prefix.ts index a2302f62bb..9b0267d372 100644 --- a/packages/cli/src/migrations/codemods/jsx/classname-prefix.ts +++ b/packages/cli/src/migrations/codemods/jsx/classname-prefix.ts @@ -25,11 +25,11 @@ const replaceInLiteral = (node: string) => { }; const replaceInTemplateLiteral = (node: TemplateElement[]) => { - node.forEach((element) => { + for (const element of node) { const value = element.value.raw; - if (typeof value !== 'string') return; + if (typeof value !== 'string') continue; element.value.raw = replaceInLiteral(value); - }); + } }; const processNode = (node: Node) => { @@ -76,13 +76,13 @@ function replaceClassNamePrefix(file: FileInfo, api: API): string | undefined { const j = api.jscodeshift; const root = j(file.source); - root.find(j.JSXElement).forEach((path) => { - j(path) - .find(j.JSXAttribute, { name: { name: 'className' } }) - .forEach((nodePath) => { - processNode(nodePath.value.value as Node); - }); - }); + for (const path of root.find(j.JSXElement).paths()) { + const nodes = j(path).find(j.JSXAttribute, { name: { name: 'className' } }); + + for (const nodePath of nodes.paths()) { + processNode(nodePath.value.value as Node); + } + } return root.toSource({ quote: 'single', diff --git a/packages/cli/src/tokens/utils/permutateThemes.ts b/packages/cli/src/tokens/utils/permutateThemes.ts index feef119570..2925fbab5a 100644 --- a/packages/cli/src/tokens/utils/permutateThemes.ts +++ b/packages/cli/src/tokens/utils/permutateThemes.ts @@ -28,7 +28,7 @@ export function permutateThemes(themes: ThemeObject[], { separator = '-' } = {} } // Sort themes by groups const groups: Record = {}; - themes.forEach((theme) => { + for (const theme of themes) { if (theme.group) { groups[theme.group] = [...(groups[theme.group] ?? []), theme]; } else { @@ -36,7 +36,7 @@ export function permutateThemes(themes: ThemeObject[], { separator = '-' } = {} `Theme ${theme.name} does not have a group property, which is required for multi-dimensional theming.`, ); } - }); + } if (Object.keys(groups).length <= 1) { return mapThemesToSetsObject(themes); diff --git a/packages/react/src/components/form/Combobox/Combobox.tsx b/packages/react/src/components/form/Combobox/Combobox.tsx index bedc1c5777..24a6ba21ae 100644 --- a/packages/react/src/components/form/Combobox/Combobox.tsx +++ b/packages/react/src/components/form/Combobox/Combobox.tsx @@ -268,9 +268,9 @@ export const ComboboxComponent = forwardRef( inputRef.current?.focus(); } else { /* clear newSelectedOptions */ - Object.keys(newSelectedOptions).forEach((key) => { + for (const key of Object.keys(newSelectedOptions)) { delete newSelectedOptions[key]; - }); + } newSelectedOptions[prefix(option.value)] = option; setInputValue(option?.label || ''); // move cursor to the end of the input diff --git a/packages/react/src/components/form/Combobox/internal/ComboboxInput.tsx b/packages/react/src/components/form/Combobox/internal/ComboboxInput.tsx index 84a3660beb..7a14054da6 100644 --- a/packages/react/src/components/form/Combobox/internal/ComboboxInput.tsx +++ b/packages/react/src/components/form/Combobox/internal/ComboboxInput.tsx @@ -72,13 +72,13 @@ const ComboboxInput = ({ setActiveIndex(0); // check if input value is the same as a label, if so, select it - Object.values(options).forEach((option) => { + for (const option of Object.values(options)) { if (option.label.toLowerCase() === value.toLowerCase()) { /* if option is already selected, discard selecting it, since it would de-select */ - if (selectedOptions[prefix(option.value)]) return; - handleSelectOption({ option: option }); + if (selectedOptions[prefix(option.value)]) continue; + handleSelectOption({ option }); } - }); + } }; const showClearButton = diff --git a/packages/react/src/components/form/NativeSelect/NativeSelect.test.tsx b/packages/react/src/components/form/NativeSelect/NativeSelect.test.tsx index 792eebe09f..46b44a9b2d 100644 --- a/packages/react/src/components/form/NativeSelect/NativeSelect.test.tsx +++ b/packages/react/src/components/form/NativeSelect/NativeSelect.test.tsx @@ -54,9 +54,9 @@ describe('NativeSelect', () => { it('Renders all options', () => { render(); - options.forEach(({ label, value }) => { + for (const { label, value } of options) { expect(screen.getByRole('option', { name: label })).toHaveValue(value); - }); + } }); it('Lets the user select a value', async () => { From bd9377f411d85b7ab0937414dacd871617e9a464 Mon Sep 17 00:00:00 2001 From: barsnes Date: Wed, 14 Aug 2024 13:34:37 +0200 Subject: [PATCH 2/3] remove TODO --- biome.jsonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biome.jsonc b/biome.jsonc index 3a19c8636f..77aedeb8fb 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -64,7 +64,7 @@ "useAriaPropsForRole": "off" }, "complexity": { - "noForEach": "error" // TODO: Enable this rule + "noForEach": "error" }, "correctness": { "useExhaustiveDependencies": "off" From 249588bd38d6a6961549c2cd9f932edc540a1ffe Mon Sep 17 00:00:00 2001 From: barsnes Date: Wed, 14 Aug 2024 13:46:53 +0200 Subject: [PATCH 3/3] remove rule as it is enabled by default --- biome.jsonc | 3 --- 1 file changed, 3 deletions(-) diff --git a/biome.jsonc b/biome.jsonc index 77aedeb8fb..2f9cb075bf 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -63,9 +63,6 @@ "noNoninteractiveTabindex": "off", "useAriaPropsForRole": "off" }, - "complexity": { - "noForEach": "error" - }, "correctness": { "useExhaustiveDependencies": "off" },