Skip to content

Commit 201d6e3

Browse files
authored
infra(unicorn): no-array-for-each (#2461)
1 parent 6cb5aa2 commit 201d6e3

File tree

10 files changed

+65
-76
lines changed

10 files changed

+65
-76
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ module.exports = defineConfig({
5555
'unicorn/filename-case': 'off',
5656
'unicorn/import-style': 'off',
5757
'unicorn/no-array-callback-reference': 'off',
58-
'unicorn/no-array-for-each': 'off',
5958
'unicorn/no-array-reduce': 'off',
6059
'unicorn/no-await-expression-member': 'off',
6160
'unicorn/no-for-loop': 'off',

scripts/apidoc/parameterDefaults.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ function patchSignatureParameterDefaults(
130130
throw new Error('Unexpected parameter length mismatch');
131131
}
132132

133-
signatureParameters.forEach(
134-
(param, index) =>
135-
(param.defaultValue = parameterDefaults[index] || param.defaultValue)
136-
);
133+
for (const [index, param] of signatureParameters.entries()) {
134+
param.defaultValue = parameterDefaults[index] || param.defaultValue;
135+
}
137136
}

src/modules/datatype/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ export class DatatypeModule {
407407
const properties = ['foo', 'bar', 'bike', 'a', 'b', 'name', 'prop'];
408408
const returnObject: Record<string, string | number> = {};
409409

410-
properties.forEach((prop) => {
410+
for (const prop of properties) {
411411
returnObject[prop] = this.boolean()
412412
? this.faker.string.sample()
413413
: this.faker.number.int();
414-
});
414+
}
415415

416416
return JSON.stringify(returnObject);
417417
}

src/modules/system/index.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,11 @@ export class SystemModule {
154154
* @since 3.1.0
155155
*/
156156
fileType(): string {
157-
const typeSet = new Set<string>();
158157
const mimeTypes = this.faker.definitions.system.mimeTypes;
159158

160-
Object.keys(mimeTypes).forEach((m) => {
161-
const type = m.split('/')[0];
162-
163-
typeSet.add(type);
164-
});
165-
159+
const typeSet = new Set(
160+
Object.keys(mimeTypes).map((key) => key.split('/')[0])
161+
);
166162
const types = Array.from(typeSet);
167163
return this.faker.helpers.arrayElement(types);
168164
}
@@ -179,22 +175,15 @@ export class SystemModule {
179175
* @since 3.1.0
180176
*/
181177
fileExt(mimeType?: string): string {
182-
if (typeof mimeType === 'string') {
183-
const mimes = this.faker.definitions.system.mimeTypes;
184-
return this.faker.helpers.arrayElement(mimes[mimeType].extensions);
185-
}
186-
187178
const mimeTypes = this.faker.definitions.system.mimeTypes;
188-
const extensionSet = new Set<string>();
189179

190-
Object.keys(mimeTypes).forEach((m) => {
191-
if (mimeTypes[m].extensions instanceof Array) {
192-
mimeTypes[m].extensions.forEach((ext) => {
193-
extensionSet.add(ext);
194-
});
195-
}
196-
});
180+
if (typeof mimeType === 'string') {
181+
return this.faker.helpers.arrayElement(mimeTypes[mimeType].extensions);
182+
}
197183

184+
const extensionSet = new Set(
185+
Object.values(mimeTypes).flatMap(({ extensions }) => extensions)
186+
);
198187
const extensions = Array.from(extensionSet);
199188
return this.faker.helpers.arrayElement(extensions);
200189
}

test/all_functional.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ describe('functional tests', () => {
119119
}
120120

121121
describe.each(Object.entries(modules))('%s', (module, methods) => {
122-
methods.forEach((meth) => {
122+
// eslint-disable-next-line vitest/prefer-each -- need to dynamically succeed/fail
123+
for (const meth of methods) {
123124
const testAssertion = () => {
124125
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
125126
faker.seed(1);
@@ -140,7 +141,7 @@ describe('functional tests', () => {
140141
// We expect a failure here to ensure we remove the exclusions when fixed
141142
it.fails(`${meth}()`, testAssertion);
142143
}
143-
});
144+
}
144145
});
145146
});
146147
});
@@ -153,7 +154,8 @@ describe('faker.helpers.fake functional tests', () => {
153154
}
154155

155156
describe.each(Object.entries(modules))('%s', (module, methods) => {
156-
methods.forEach((meth) => {
157+
// eslint-disable-next-line vitest/prefer-each -- need to dynamically succeed/fail
158+
for (const meth of methods) {
157159
const testAssertion = () => {
158160
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
159161
faker.seed(1);
@@ -172,7 +174,7 @@ describe('faker.helpers.fake functional tests', () => {
172174
// We expect a failure here to ensure we remove the exclusions when fixed
173175
it.fails(`${meth}()`, testAssertion);
174176
}
175-
});
177+
}
176178
});
177179
});
178180
});

test/modules/color.spec.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ describe('color', () => {
8888
it('should return a random rgb color in decimal format', () => {
8989
const color = faker.color.rgb({ format: 'decimal' });
9090
expect(color).length(3);
91-
color.forEach((value: number) => {
91+
for (const value of color) {
9292
expect(value).toBeGreaterThanOrEqual(0);
9393
expect(value).toBeLessThanOrEqual(255);
94-
});
94+
}
9595
});
9696
});
9797

@@ -124,10 +124,10 @@ describe('color', () => {
124124
});
125125
expect(color[color.length - 1]).toBeGreaterThanOrEqual(0);
126126
expect(color[color.length - 1]).toBeLessThanOrEqual(1);
127-
color.slice(0, 4).forEach((value: number) => {
127+
for (const value of color.slice(0, 4)) {
128128
expect(value).toBeGreaterThanOrEqual(0);
129129
expect(value).toBeLessThanOrEqual(255);
130-
});
130+
}
131131
});
132132
});
133133

@@ -154,21 +154,21 @@ describe('color', () => {
154154
it('should return a random cmyk color', () => {
155155
const color = faker.color.cmyk();
156156
expect(color).length(4);
157-
color.forEach((value: number) => {
157+
for (const value of color) {
158158
expect(value).toBeGreaterThanOrEqual(0);
159159
expect(value).toBeLessThanOrEqual(1);
160-
});
160+
}
161161
});
162162
});
163163

164164
describe(`cmyk({ format: 'decimal' })`, () => {
165165
it('should return a random cmyk color in decimal format', () => {
166166
const color = faker.color.cmyk({ format: 'decimal' });
167167
expect(color).length(4);
168-
color.forEach((value: number) => {
168+
for (const value of color) {
169169
expect(value).toBeGreaterThanOrEqual(0);
170170
expect(value).toBeLessThanOrEqual(1);
171-
});
171+
}
172172
});
173173
});
174174

@@ -196,10 +196,10 @@ describe('color', () => {
196196
expect(color).length(3);
197197
expect(color[0]).toBeGreaterThanOrEqual(0);
198198
expect(color[0]).toBeLessThanOrEqual(360);
199-
color.slice(1).forEach((value: number) => {
199+
for (const value of color.slice(1)) {
200200
expect(value).toBeGreaterThanOrEqual(0);
201201
expect(value).toBeLessThanOrEqual(1);
202-
});
202+
}
203203
});
204204
});
205205

@@ -246,10 +246,10 @@ describe('color', () => {
246246
expect(color).length(3);
247247
expect(color[0]).toBeGreaterThanOrEqual(0);
248248
expect(color[0]).toBeLessThanOrEqual(360);
249-
color.slice(1).forEach((value: number) => {
249+
for (const value of color.slice(1)) {
250250
expect(value).toBeGreaterThanOrEqual(0);
251251
expect(value).toBeLessThanOrEqual(1);
252-
});
252+
}
253253
});
254254
});
255255

@@ -259,10 +259,10 @@ describe('color', () => {
259259
expect(color).length(3);
260260
expect(color[0]).toBeGreaterThanOrEqual(0);
261261
expect(color[0]).toBeLessThanOrEqual(360);
262-
color.slice(1).forEach((value: number) => {
262+
for (const value of color.slice(1)) {
263263
expect(value).toBeGreaterThanOrEqual(0);
264264
expect(value).toBeLessThanOrEqual(1);
265-
});
265+
}
266266
});
267267
});
268268

@@ -286,10 +286,10 @@ describe('color', () => {
286286
expect(color).length(3);
287287
expect(color[0]).toBeGreaterThanOrEqual(0);
288288
expect(color[0]).toBeLessThanOrEqual(1);
289-
color.forEach((value: number) => {
289+
for (const value of color) {
290290
expect(value).toBeGreaterThanOrEqual(-100);
291291
expect(value).toBeLessThanOrEqual(100);
292-
});
292+
}
293293
});
294294
});
295295

@@ -299,10 +299,10 @@ describe('color', () => {
299299
expect(color).length(3);
300300
expect(color[0]).toBeGreaterThanOrEqual(0);
301301
expect(color[0]).toBeLessThanOrEqual(1);
302-
color.forEach((value: number) => {
302+
for (const value of color) {
303303
expect(value).toBeGreaterThanOrEqual(-100);
304304
expect(value).toBeLessThanOrEqual(100);
305-
});
305+
}
306306
});
307307
});
308308

@@ -328,10 +328,10 @@ describe('color', () => {
328328
expect(color).length(3);
329329
expect(color[0]).toBeGreaterThanOrEqual(0);
330330
expect(color[0]).toBeLessThanOrEqual(1);
331-
color.forEach((value: number) => {
331+
for (const value of color) {
332332
expect(value).toBeGreaterThanOrEqual(0);
333333
expect(value).toBeLessThanOrEqual(230);
334-
});
334+
}
335335
});
336336
});
337337

@@ -341,10 +341,10 @@ describe('color', () => {
341341
expect(color).length(3);
342342
expect(color[0]).toBeGreaterThanOrEqual(0);
343343
expect(color[0]).toBeLessThanOrEqual(1);
344-
color.forEach((value: number) => {
344+
for (const value of color) {
345345
expect(value).toBeGreaterThanOrEqual(0);
346346
expect(value).toBeLessThanOrEqual(230);
347-
});
347+
}
348348
});
349349
});
350350

@@ -368,21 +368,21 @@ describe('color', () => {
368368
it('should return a random color for a CSS color space in decimal format', () => {
369369
const color = faker.color.colorByCSSColorSpace();
370370
expect(color).length(3);
371-
color.forEach((value: number) => {
371+
for (const value of color) {
372372
expect(value).toBeGreaterThanOrEqual(0);
373373
expect(value).toBeLessThanOrEqual(1);
374-
});
374+
}
375375
});
376376
});
377377

378378
describe(`colorByCSSColorSpace({ format: 'decimal' })`, () => {
379379
it('should return a random color for a CSS color space in decimal format', () => {
380380
const color = faker.color.colorByCSSColorSpace({ format: 'decimal' });
381381
expect(color).length(3);
382-
color.forEach((value: number) => {
382+
for (const value of color) {
383383
expect(value).toBeGreaterThanOrEqual(0);
384384
expect(value).toBeLessThanOrEqual(1);
385-
});
385+
}
386386
});
387387
});
388388

test/modules/helpers.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ describe('helpers', () => {
375375
expect(subset.length).toBeLessThanOrEqual(testArray.length);
376376

377377
// Check elements
378-
subset.forEach((element) => {
378+
for (const element of subset) {
379379
expect(testArray).toContain(element);
380-
});
380+
}
381381

382382
// Check uniqueness
383383
expect(subset).not.toContainDuplicates();
@@ -391,9 +391,9 @@ describe('helpers', () => {
391391
expect(subset).toHaveLength(3);
392392

393393
// Check elements
394-
subset.forEach((element) => {
394+
for (const element of subset) {
395395
expect(testArray).toContain(element);
396-
});
396+
}
397397

398398
// Check uniqueness
399399
expect(subset).toHaveLength(new Set(subset).size);
@@ -411,9 +411,9 @@ describe('helpers', () => {
411411
expect(subset.length).toBeLessThanOrEqual(4);
412412

413413
// Check elements
414-
subset.forEach((element) => {
414+
for (const element of subset) {
415415
expect(testArray).toContain(element);
416-
});
416+
}
417417

418418
// Check uniqueness
419419
expect(subset).not.toContainDuplicates();
@@ -427,9 +427,9 @@ describe('helpers', () => {
427427
expect(subset.length).toBe(5);
428428

429429
// Check elements
430-
subset.forEach((element) => {
430+
for (const element of subset) {
431431
expect(testArray).toContain(element);
432-
});
432+
}
433433
});
434434

435435
it('should return an empty array when array length > 0 and count = 0', () => {

test/modules/system.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ describe('system', () => {
395395
(options, count: number) => {
396396
const cron = faker.system.cron(options).split(' ');
397397
expect(cron).toHaveLength(count);
398-
cron.forEach((cronElement, i) =>
398+
for (const [index, cronElement] of cron.entries()) {
399399
expect(
400400
cronElement,
401-
`generated cron, ${cronElement} should match regex ${regexElements[i]}`
402-
).toMatch(new RegExp(regexElements[i]))
403-
);
401+
`generated cron, ${cronElement} should match regex ${regexElements[index]}`
402+
).toMatch(new RegExp(regexElements[index]));
403+
}
404404
}
405405
);
406406

test/scripts/apidoc/signature.debug.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ const methods = loadExampleMethods();
1212

1313
initMarkdownRenderer()
1414
.then(() => {
15-
Object.entries(methods).forEach(([name, method]) => {
15+
for (const [name, method] of Object.entries(methods)) {
1616
console.log('Analyzing:', name);
1717
const result = analyzeSignature(method, '', method.name);
1818
console.log('Result:', result);
19-
});
19+
}
2020
})
2121
.catch(console.error);

0 commit comments

Comments
 (0)