Skip to content

Commit b4cbab5

Browse files
authored
Merge pull request Expensify#76347 from gelocraft/fix-no-array-for-each-1764509572
fix(eslint): fix unicorn/no-array-for-each rule violations
2 parents d1b6f6d + 9d27169 commit b4cbab5

File tree

4 files changed

+52
-55
lines changed

4 files changed

+52
-55
lines changed

src/libs/SearchQueryUtils.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,20 @@ const createKeyToUserFriendlyMap = () => {
6565
const map = new Map<string, string>();
6666

6767
// Map SYNTAX_FILTER_KEYS values to their user-friendly names
68-
// eslint-disable-next-line unicorn/no-array-for-each
69-
Object.entries(CONST.SEARCH.SYNTAX_FILTER_KEYS).forEach(([keyName, keyValue]) => {
68+
for (const [keyName, keyValue] of Object.entries(CONST.SEARCH.SYNTAX_FILTER_KEYS)) {
7069
if (!(keyName in CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS)) {
71-
return;
70+
continue;
7271
}
7372
map.set(keyValue, CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS[keyName as keyof typeof CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS]);
74-
});
73+
}
7574

7675
// Map SYNTAX_ROOT_KEYS values to their user-friendly names
77-
// eslint-disable-next-line unicorn/no-array-for-each
78-
Object.entries(CONST.SEARCH.SYNTAX_ROOT_KEYS).forEach(([keyName, keyValue]) => {
76+
for (const [keyName, keyValue] of Object.entries(CONST.SEARCH.SYNTAX_ROOT_KEYS)) {
7977
if (!(keyName in CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS)) {
80-
return;
78+
continue;
8179
}
8280
map.set(keyValue, CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS[keyName as keyof typeof CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS]);
83-
});
81+
}
8482

8583
return map;
8684
};
@@ -193,8 +191,7 @@ function buildFilterValuesString(filterName: string, queryFilters: QueryFilter[]
193191
const allowedOps = new Set<string>([CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, CONST.SEARCH.SYNTAX_OPERATORS.NOT_EQUAL_TO]);
194192

195193
let filterValueString = '';
196-
// eslint-disable-next-line unicorn/no-array-for-each
197-
queryFilters.forEach((queryFilter, index) => {
194+
for (const [index, queryFilter] of queryFilters.entries()) {
198195
const previousValueHasSameOp = allowedOps.has(queryFilter.operator) && queryFilters?.at(index - 1)?.operator === queryFilter.operator;
199196
const nextValueHasSameOp = allowedOps.has(queryFilter.operator) && queryFilters?.at(index + 1)?.operator === queryFilter.operator;
200197

@@ -208,7 +205,7 @@ function buildFilterValuesString(filterName: string, queryFilters: QueryFilter[]
208205
} else {
209206
filterValueString += ` ${filterName}${operatorToCharMap[queryFilter.operator]}${sanitizeSearchValue(queryFilter.value.toString())}`;
210207
}
211-
});
208+
}
212209

213210
return filterValueString;
214211
}
@@ -252,13 +249,12 @@ function getFilters(queryJSON: SearchQueryJSON) {
252249
value: node.right as string | number,
253250
});
254251
} else {
255-
// eslint-disable-next-line unicorn/no-array-for-each
256-
node.right.forEach((element) => {
252+
for (const element of node.right) {
257253
filterArray.push({
258254
operator: node.operator,
259255
value: element,
260256
});
261-
});
257+
}
262258
}
263259
filters.push({key: nodeKey, filters: filterArray});
264260
}
@@ -351,26 +347,26 @@ function getQueryHashes(query: SearchQueryJSON): {primaryHash: number; recentSea
351347
// their value when computing the similarSearchHash
352348
const similarSearchValueBasedFilters = new Set<SearchFilterKey>([CONST.SEARCH.SYNTAX_FILTER_KEYS.ACTION]);
353349

354-
query.flatFilters
350+
const flatFilters = query.flatFilters
355351
.map((filter) => {
356352
const filterKey = filter.key;
357353
const filters = cloneDeep(filter.filters);
358354
filters.sort((a, b) => customCollator.compare(a.value.toString(), b.value.toString()));
359355
return {filterString: buildFilterValuesString(filterKey, filters), filterKey};
360356
})
361-
.sort((a, b) => customCollator.compare(a.filterString, b.filterString))
362-
// eslint-disable-next-line unicorn/no-array-for-each
363-
.forEach(({filterString, filterKey}) => {
364-
if (!similarSearchIgnoredFilters.has(filterKey)) {
365-
filterSet.add(filterKey);
366-
}
357+
.sort((a, b) => customCollator.compare(a.filterString, b.filterString));
367358

368-
if (similarSearchValueBasedFilters.has(filterKey)) {
369-
filterSet.add(filterString.trim());
370-
}
359+
for (const {filterString, filterKey} of flatFilters) {
360+
if (!similarSearchIgnoredFilters.has(filterKey)) {
361+
filterSet.add(filterKey);
362+
}
371363

372-
orderedQuery += ` ${filterString}`;
373-
});
364+
if (similarSearchValueBasedFilters.has(filterKey)) {
365+
filterSet.add(filterString.trim());
366+
}
367+
368+
orderedQuery += ` ${filterString}`;
369+
}
374370

375371
const similarSearchHash = hashText(Array.from(filterSet).join(''), 2 ** 32);
376372
const recentSearchHash = hashText(orderedQuery, 2 ** 32);
@@ -555,14 +551,13 @@ function buildQueryStringFromFilterFormValues(filterValues: Partial<SearchAdvanc
555551
// When switching types/setting the type, ensure we aren't polluting our query with filters that are
556552
// only available for the previous type. Remove all filters that are not allowed for the new type
557553
const providedFilterKeys = Object.keys(supportedFilterValues) as SearchAdvancedFiltersKey[];
558-
// eslint-disable-next-line unicorn/no-array-for-each
559-
providedFilterKeys.forEach((filter) => {
554+
for (const filter of providedFilterKeys) {
560555
if (isFilterSupported(filter, supportedFilterValues.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE)) {
561-
return;
556+
continue;
562557
}
563558

564559
supportedFilterValues[filter] = undefined;
565-
});
560+
}
566561

567562
// We separate type and status filters from other filters to maintain hashes consistency for saved searches
568563
const {type, status, groupBy, ...otherFilters} = supportedFilterValues;
@@ -701,17 +696,15 @@ function buildQueryStringFromFilterFormValues(filterValues: Partial<SearchAdvanc
701696

702697
filtersString.push(...mappedFilters);
703698

704-
// eslint-disable-next-line unicorn/no-array-for-each
705-
DATE_FILTER_KEYS.forEach((dateKey) => {
699+
for (const dateKey of DATE_FILTER_KEYS) {
706700
const dateFilter = buildDateFilterQuery(supportedFilterValues, dateKey);
707701
filtersString.push(dateFilter);
708-
});
702+
}
709703

710-
// eslint-disable-next-line unicorn/no-array-for-each
711-
AMOUNT_FILTER_KEYS.forEach((filterKey) => {
704+
for (const filterKey of AMOUNT_FILTER_KEYS) {
712705
const amountFilter = buildAmountFilterQuery(filterKey, supportedFilterValues);
713706
filtersString.push(amountFilter);
714-
});
707+
}
715708

716709
return filtersString.filter(Boolean).join(' ').trim();
717710
}

src/libs/SearchUIUtils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,8 +2588,9 @@ function getColumnsToShow(
25882588
};
25892589

25902590
if (Array.isArray(data)) {
2591-
// eslint-disable-next-line unicorn/no-array-for-each
2592-
data.forEach(updateColumns);
2591+
for (const item of data) {
2592+
updateColumns(item);
2593+
}
25932594
} else {
25942595
for (const key of Object.keys(data)) {
25952596
if (!isTransactionEntry(key)) {

src/libs/actions/OnyxUpdateManager/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,30 @@ function handleMissingOnyxUpdates(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFr
204204
function updateAuthTokenIfNecessary(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromServer>): void {
205205
// Consolidate all of the given Onyx updates
206206
const onyxUpdates: OnyxUpdate[] = [];
207-
// eslint-disable-next-line unicorn/no-array-for-each
208-
onyxUpdatesFromServer?.updates?.forEach((updateEvent) => onyxUpdates.push(...updateEvent.data));
207+
if (onyxUpdatesFromServer?.updates) {
208+
for (const updateEvent of onyxUpdatesFromServer.updates) {
209+
onyxUpdates.push(...updateEvent.data);
210+
}
211+
}
209212
onyxUpdates.push(...(onyxUpdatesFromServer?.response?.onyxData ?? []));
210213

211214
// Find any session updates
212215
const sessionUpdates = onyxUpdates?.filter((onyxUpdate) => onyxUpdate.key === ONYXKEYS.SESSION);
213216

214217
// If any of the updates changes the authToken, let's update it now
215-
// eslint-disable-next-line unicorn/no-array-for-each
216-
sessionUpdates?.forEach((sessionUpdate) => {
217-
const session = (sessionUpdate.value ?? {}) as Session;
218-
const newAuthToken = session.authToken ?? '';
219-
if (!newAuthToken) {
220-
return;
221-
}
218+
if (sessionUpdates) {
219+
for (const sessionUpdate of sessionUpdates) {
220+
const session = (sessionUpdate.value ?? {}) as Session;
221+
const newAuthToken = session.authToken ?? '';
222+
if (!newAuthToken) {
223+
continue;
224+
}
222225

223-
Log.info('[OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.');
224-
updateSessionAuthTokens(newAuthToken);
225-
setAuthToken(newAuthToken);
226-
});
226+
Log.info('[OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.');
227+
updateSessionAuthTokens(newAuthToken);
228+
setAuthToken(newAuthToken);
229+
}
230+
}
227231
}
228232

229233
export default () => {

src/libs/actions/OnyxUpdateManager/utils/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ function detectGapsAndSplit(lastUpdateIDFromClient: number): DetectGapAndSplitRe
9999

100100
// Add all deferred updates after the gap(s) to "updatesAfterGaps".
101101
// If "firstUpdateToBeAppliedAfterGap" is set to the last deferred update, the array will be empty.
102-
// eslint-disable-next-line unicorn/no-array-for-each
103-
Object.entries(pendingDeferredUpdates).forEach(([lastUpdateID, update]) => {
102+
for (const [lastUpdateID, update] of Object.entries(pendingDeferredUpdates)) {
104103
if (Number(lastUpdateID) < firstUpdateToBeAppliedAfterGap) {
105-
return;
104+
continue;
106105
}
107106

108107
updatesAfterGaps[Number(lastUpdateID)] = update;
109-
}, {});
108+
}
110109
}
111110

112111
return {applicableUpdates, updatesAfterGaps, latestMissingUpdateID};

0 commit comments

Comments
 (0)