Skip to content

Commit fe8f9de

Browse files
authored
chore: replace forEach with for..of (#2251)
resolves #2247
1 parent d270bd5 commit fe8f9de

File tree

8 files changed

+36
-41
lines changed

8 files changed

+36
-41
lines changed

biome.jsonc

-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@
6363
"noNoninteractiveTabindex": "off",
6464
"useAriaPropsForRole": "off"
6565
},
66-
"complexity": {
67-
"noForEach": "off" // TODO: Enable this rule
68-
},
6966
"correctness": {
7067
"useExhaustiveDependencies": "off"
7168
},

packages/cli/bin/designsystemet.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ program
3434
const { glob, list } = opts;
3535

3636
if (list) {
37-
Object.keys(migrations).forEach((key) => {
37+
for (const key of Object.keys(migrations)) {
3838
console.log(key);
39-
});
39+
}
4040
} else if (migrationKey) {
4141
const migration = migrations[migrationKey as keyof typeof migrations];
4242
if (!migration) {

packages/cli/src/migrations/codemods/css/plugins.ts

+14-16
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export const cssClassRename: PluginGenerator = (dictionary) => ({
1717

1818
if (!selector) return;
1919

20-
Object.entries(dictionary).forEach(([from, to]) => {
20+
for (const [from, to] of Object.entries(dictionary)) {
2121
if (!selector.includes(from)) return;
2222

2323
const newSelector = selector.replace(new RegExp(from, 'g'), to);
2424

2525
rule.selector = newSelector;
26-
});
26+
}
2727
},
2828
});
2929

@@ -34,23 +34,21 @@ export const cssVarRename: PluginGenerator = (dictionary) => ({
3434

3535
const deleted = new Set<string>();
3636

37-
Object.entries(dictionary).forEach(([from, to]) => {
37+
for (const [from, to] of Object.entries(dictionary)) {
3838
if (!R.isEmpty(to)) {
39-
switch (true) {
40-
case R.includes(from, value):
41-
to === '[delete]' && deleted.add(deleteMsg(decl, from));
42-
decl.value = value.replace(from, to);
43-
break;
44-
45-
case R.includes(from, prop):
46-
if (decl.variable) {
47-
to === '[delete]' && deleted.add(deleteMsg(decl, from));
48-
decl.prop = prop.replace(from, to);
49-
break;
50-
}
39+
if (R.includes(from, value)) {
40+
if (to === '[delete]') {
41+
deleted.add(deleteMsg(decl, from));
42+
}
43+
decl.value = value.replace(from, to);
44+
} else if (R.includes(from, prop) && decl.variable) {
45+
if (to === '[delete]') {
46+
deleted.add(deleteMsg(decl, from));
47+
}
48+
decl.prop = prop.replace(from, to);
5149
}
5250
}
53-
});
51+
}
5452

5553
if (deleted.size > 0) {
5654
Array.from(deleted).forEach(printDelete);

packages/cli/src/migrations/codemods/jsx/classname-prefix.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ const replaceInLiteral = (node: string) => {
2525
};
2626

2727
const replaceInTemplateLiteral = (node: TemplateElement[]) => {
28-
node.forEach((element) => {
28+
for (const element of node) {
2929
const value = element.value.raw;
30-
if (typeof value !== 'string') return;
30+
if (typeof value !== 'string') continue;
3131
element.value.raw = replaceInLiteral(value);
32-
});
32+
}
3333
};
3434

3535
const processNode = (node: Node) => {
@@ -76,13 +76,13 @@ function replaceClassNamePrefix(file: FileInfo, api: API): string | undefined {
7676
const j = api.jscodeshift;
7777
const root = j(file.source);
7878

79-
root.find(j.JSXElement).forEach((path) => {
80-
j(path)
81-
.find(j.JSXAttribute, { name: { name: 'className' } })
82-
.forEach((nodePath) => {
83-
processNode(nodePath.value.value as Node);
84-
});
85-
});
79+
for (const path of root.find(j.JSXElement).paths()) {
80+
const nodes = j(path).find(j.JSXAttribute, { name: { name: 'className' } });
81+
82+
for (const nodePath of nodes.paths()) {
83+
processNode(nodePath.value.value as Node);
84+
}
85+
}
8686

8787
return root.toSource({
8888
quote: 'single',

packages/cli/src/tokens/utils/permutateThemes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ export function permutateThemes(themes: ThemeObject[], { separator = '-' } = {}
2828
}
2929
// Sort themes by groups
3030
const groups: Record<string, ThemeObject[]> = {};
31-
themes.forEach((theme) => {
31+
for (const theme of themes) {
3232
if (theme.group) {
3333
groups[theme.group] = [...(groups[theme.group] ?? []), theme];
3434
} else {
3535
throw new Error(
3636
`Theme ${theme.name} does not have a group property, which is required for multi-dimensional theming.`,
3737
);
3838
}
39-
});
39+
}
4040

4141
if (Object.keys(groups).length <= 1) {
4242
return mapThemesToSetsObject(themes);

packages/react/src/components/form/Combobox/Combobox.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ export const ComboboxComponent = forwardRef<HTMLInputElement, ComboboxProps>(
268268
inputRef.current?.focus();
269269
} else {
270270
/* clear newSelectedOptions */
271-
Object.keys(newSelectedOptions).forEach((key) => {
271+
for (const key of Object.keys(newSelectedOptions)) {
272272
delete newSelectedOptions[key];
273-
});
273+
}
274274
newSelectedOptions[prefix(option.value)] = option;
275275
setInputValue(option?.label || '');
276276
// move cursor to the end of the input

packages/react/src/components/form/Combobox/internal/ComboboxInput.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ const ComboboxInput = ({
7272
setActiveIndex(0);
7373

7474
// check if input value is the same as a label, if so, select it
75-
Object.values(options).forEach((option) => {
75+
for (const option of Object.values(options)) {
7676
if (option.label.toLowerCase() === value.toLowerCase()) {
7777
/* if option is already selected, discard selecting it, since it would de-select */
78-
if (selectedOptions[prefix(option.value)]) return;
79-
handleSelectOption({ option: option });
78+
if (selectedOptions[prefix(option.value)]) continue;
79+
handleSelectOption({ option });
8080
}
81-
});
81+
}
8282
};
8383

8484
const showClearButton =

packages/react/src/components/form/NativeSelect/NativeSelect.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ describe('NativeSelect', () => {
5454

5555
it('Renders all options', () => {
5656
render();
57-
options.forEach(({ label, value }) => {
57+
for (const { label, value } of options) {
5858
expect(screen.getByRole('option', { name: label })).toHaveValue(value);
59-
});
59+
}
6060
});
6161

6262
it('Lets the user select a value', async () => {

0 commit comments

Comments
 (0)