mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
refactor(lint): share keyed first-match predicate patterns (#16163)
This commit is contained in:
1 parent
2d9a339afd
commit
dcc17fd75a
1 file changed
+14
-24
@@ -13,14 +13,10 @@ const DESTRUCTURED = String.raw`\(\s*\{[^}]*?\b(${KEY})\b(?!\s*:)[^}]*\}\s*(?::[
|
||||
const FIND = String.raw`\.find(?:Last)?\(\s*`;
|
||||
const FILTER = String.raw`\.filter\(\s*`;
|
||||
|
||||
const FIND_PATTERNS = [
|
||||
new RegExp(FIND + PLAIN, "gu"),
|
||||
new RegExp(FIND + DESTRUCTURED, "gu"),
|
||||
];
|
||||
const FILTER_PATTERNS = [
|
||||
new RegExp(FILTER + PLAIN, "gu"),
|
||||
new RegExp(FILTER + DESTRUCTURED, "gu"),
|
||||
];
|
||||
const PATTERNS = [PLAIN, DESTRUCTURED].map((predicate) => ({
|
||||
find: new RegExp(FIND + predicate, "gu"),
|
||||
filter: new RegExp(FILTER + predicate, "gu"),
|
||||
}));
|
||||
|
||||
/** Index of the quote that closes the literal opening at `start`, or -1. */
|
||||
const literalEnd = (content, start) => {
|
||||
@@ -72,20 +68,14 @@ const indexesFirstResult = (content, open) => {
|
||||
return content.startsWith("[0]", end);
|
||||
};
|
||||
|
||||
const count = (content, pattern, accept) =>
|
||||
[...content.matchAll(pattern)].filter(accept).length;
|
||||
|
||||
/** Number of first-match lookups by key field in `content`. */
|
||||
export const countKeyedFirstMatches = (content) =>
|
||||
FIND_PATTERNS.reduce(
|
||||
(total, pattern) => total + count(content, pattern, () => true),
|
||||
0,
|
||||
) +
|
||||
FILTER_PATTERNS.reduce(
|
||||
(total, pattern) =>
|
||||
total +
|
||||
count(content, pattern, (match) =>
|
||||
indexesFirstResult(content, match.index + ".filter".length),
|
||||
),
|
||||
0,
|
||||
);
|
||||
export const countKeyedFirstMatches = (content) => {
|
||||
let total = 0;
|
||||
for (const { find, filter } of PATTERNS) {
|
||||
for (const _match of content.matchAll(find)) total += 1;
|
||||
for (const match of content.matchAll(filter))
|
||||
if (indexesFirstResult(content, match.index + ".filter".length))
|
||||
total += 1;
|
||||
}
|
||||
return total;
|
||||
};
|
||||
Reference in new issue
Block a user