Skip to content

Commit 05f4a34

Browse files
Updated refa, regex-ast-analysis, and scslre (#518)
* Updated dep * Use new APIs * Use new `getClosestAncestor`
1 parent 8755332 commit 05f4a34

File tree

7 files changed

+160
-98
lines changed

7 files changed

+160
-98
lines changed

lib/rules/no-dupe-disjunctions.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function unionAll(nfas: readonly ReadonlyNFA[]): ReadonlyNFA {
303303
return total
304304
}
305305

306-
const DFA_OPTIONS: DFA.CreationOptions = { maxNodes: 100_000 }
306+
const MAX_DFA_NODES = 100_000
307307

308308
/**
309309
* Returns whether one NFA is a subset of another.
@@ -313,8 +313,12 @@ function isSubsetOf(
313313
subset: ReadonlyNFA,
314314
): boolean | null {
315315
try {
316-
const a = DFA.fromIntersection(superset, subset, DFA_OPTIONS)
317-
const b = DFA.fromFA(subset, DFA_OPTIONS)
316+
const a = DFA.fromIntersection(
317+
superset,
318+
subset,
319+
new DFA.LimitedNodeFactory(MAX_DFA_NODES),
320+
)
321+
const b = DFA.fromFA(subset, new DFA.LimitedNodeFactory(MAX_DFA_NODES))
318322
a.minimize()
319323
b.minimize()
320324
return a.structurallyEqual(b)
@@ -339,13 +343,17 @@ function getSubsetRelation(
339343
right: ReadonlyNFA,
340344
): SubsetRelation {
341345
try {
342-
const inter = DFA.fromIntersection(left, right, DFA_OPTIONS)
346+
const inter = DFA.fromIntersection(
347+
left,
348+
right,
349+
new DFA.LimitedNodeFactory(MAX_DFA_NODES),
350+
)
343351
inter.minimize()
344352

345-
const l = DFA.fromFA(left, DFA_OPTIONS)
353+
const l = DFA.fromFA(left, new DFA.LimitedNodeFactory(MAX_DFA_NODES))
346354
l.minimize()
347355

348-
const r = DFA.fromFA(right, DFA_OPTIONS)
356+
const r = DFA.fromFA(right, new DFA.LimitedNodeFactory(MAX_DFA_NODES))
349357
r.minimize()
350358

351359
const subset = l.structurallyEqual(inter)

lib/rules/require-unicode-regexp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function isCompatibleQuantifier(
211211
return false
212212
}
213213

214-
if (!rangeEqual(cs.ranges, uCs.without([ASTRAL]).ranges)) {
214+
if (!rangeEqual(cs.ranges, uCs.without(ASTRAL).ranges)) {
215215
// failed condition 3
216216
return false
217217
}

lib/rules/sort-alternatives.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,11 @@ function getLexicographicallySmallestFromAlternative(
198198
const nfa = NFA.fromRegex(
199199
expression,
200200
{ maxCharacter: result.maxCharacter },
201-
{ maxNodes: 1000 },
201+
{},
202+
new NFA.LimitedNodeFactory(1000),
202203
)
203204

204-
return getLexicographicallySmallestFromNfa(
205-
nfa.nodes.initial,
206-
nfa.nodes.finals,
207-
)
205+
return getLexicographicallySmallestFromNfa(nfa.initial, nfa.finals)
208206
} catch (_error) {
209207
return undefined
210208
}

lib/utils/fix-simplify-quantifier.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Rule } from "eslint"
2-
import type { ClosestAncestor } from "regexp-ast-analysis"
32
import { getClosestAncestor } from "regexp-ast-analysis"
4-
import type { Node, Quantifier } from "@eslint-community/regexpp/ast"
3+
import type { Quantifier } from "@eslint-community/regexpp/ast"
54
import type { RegExpContext } from "."
65
import { quantToString } from "."
76
import type { CanSimplify } from "./regexp-ast/simplify-quantifier"
@@ -14,10 +13,7 @@ export function fixSimplifyQuantifier(
1413
result: CanSimplify,
1514
{ fixReplaceNode }: RegExpContext,
1615
): [replacement: string, fix: (fixer: Rule.RuleFixer) => Rule.Fix | null] {
17-
const ancestor = getClosestAncestorOfAll([
18-
quantifier,
19-
...result.dependencies,
20-
])
16+
const ancestor = getClosestAncestor(quantifier, ...result.dependencies)
2117

2218
let replacement: string
2319
if (quantifier.min === 0) {
@@ -45,18 +41,3 @@ export function fixSimplifyQuantifier(
4541
}),
4642
]
4743
}
48-
49-
/**
50-
* Returns the closest ancestor of all given elements.
51-
*/
52-
function getClosestAncestorOfAll<T extends Node>(
53-
elements: readonly [T, ...T[]],
54-
): ClosestAncestor<T, T> {
55-
let leftMost = elements[0]
56-
let rightMost = elements[0]
57-
for (const e of elements) {
58-
if (e.start < leftMost.start) leftMost = e
59-
if (e.end > rightMost.end) rightMost = e
60-
}
61-
return getClosestAncestor(leftMost, rightMost)
62-
}

lib/utils/regexp-ast/simplify-quantifier.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ function toNfa(element: Element, parser: JS.Parser): NFA {
240240
assertions: "throw",
241241
backreferences: "throw",
242242
})
243-
return NFA.fromRegex(expression, { maxCharacter }, { maxNodes: 1000 })
243+
return NFA.fromRegex(
244+
expression,
245+
{ maxCharacter },
246+
{},
247+
new NFA.LimitedNodeFactory(1000),
248+
)
244249
}
245250

246251
/**
@@ -261,15 +266,15 @@ function canAbsorbElementFormal(
261266
try {
262267
// perform a full NFA/DFA language check
263268
const qNfa = toNfa(quantifier, parser)
264-
const qDfa = DFA.fromFA(qNfa, { maxNodes: 1000 })
269+
const qDfa = DFA.fromFA(qNfa, new DFA.LimitedNodeFactory(1000))
265270
const eNfa = toNfa(element, parser)
266271
// We want to check whether `QE* = Q`. To perform this check, it's
267272
// sufficient to check `QE? = Q`. Proof sketch:
268273
// 1. If `QE? = Q`, then `Q = QE? = (QE?)E? = (QE?E?)E? = ... = QE*`.
269274
// 2. If `QE? != Q`, then `Q ⊂ QE? ⊆ QE*`, so `QE* != Q`.
270275
eNfa.quantify(0, 1)
271276
qNfa.append(eNfa)
272-
const qeDfa = DFA.fromFA(qNfa, { maxNodes: 1000 })
277+
const qeDfa = DFA.fromFA(qNfa, new DFA.LimitedNodeFactory(1000))
273278

274279
qDfa.minimize()
275280
qeDfa.minimize()

0 commit comments

Comments
 (0)