Skip to content

Commit d655d8d

Browse files
Use regexp-ast-analysis's caching (#317)
* Update to `[email protected]` * Use `regexp-ast-analysis`'s caching
1 parent 38e5f31 commit d655d8d

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

lib/utils/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Rule, AST, SourceCode } from "eslint"
88
import { getStringIfConstant } from "./ast-utils"
99
import type { ReadonlyFlags, ToCharSetElement } from "regexp-ast-analysis"
1010
// eslint-disable-next-line no-restricted-imports -- Implement RegExpContext#toCharSet
11-
import { toCharSet } from "regexp-ast-analysis"
11+
import { toCharSet, toCache } from "regexp-ast-analysis"
1212
import type { CharSet } from "refa"
1313
import { JS } from "refa"
1414
import type { UsageOfPattern } from "./get-usage-of-pattern"
@@ -89,7 +89,7 @@ type RegExpContextBase = {
8989
patternAst: Pattern
9090
patternSource: PatternSource
9191

92-
flags: ReadonlyFlags
92+
flags: Required<ReadonlyFlags>
9393
}
9494
export type RegExpContextForLiteral = {
9595
node: ESTree.RegExpLiteral
@@ -109,7 +109,7 @@ type UnparsableRegExpContextBase = {
109109
node: ESTree.Expression
110110
regexpNode: ESTree.RegExpLiteral | ESTree.CallExpression
111111

112-
flags: ReadonlyFlags
112+
flags: Required<ReadonlyFlags>
113113
flagsString: string | null
114114
ownsFlags: boolean
115115

@@ -578,7 +578,7 @@ function buildRegExpContextBase({
578578
regexpNode,
579579
flagsNode,
580580
context,
581-
flags,
581+
flags: originalFlags,
582582
parsedPattern,
583583
}: {
584584
patternSource: PatternSource
@@ -589,8 +589,8 @@ function buildRegExpContextBase({
589589
parsedPattern: Pattern
590590
}): RegExpContextBase {
591591
const sourceCode = context.getSourceCode()
592+
const flags = toCache(originalFlags)
592593

593-
const cacheCharSet = new WeakMap<ToCharSetElement, CharSet>()
594594
let cacheUsageOfPattern: UsageOfPattern | null = null
595595
return {
596596
toCharSet: (node, optionFlags) => {
@@ -599,13 +599,7 @@ function buildRegExpContextBase({
599599
return toCharSet(node, optionFlags)
600600
}
601601

602-
let charSet = cacheCharSet.get(node)
603-
if (charSet) {
604-
return charSet
605-
}
606-
charSet = toCharSet(node, flags)
607-
cacheCharSet.set(node, charSet)
608-
return charSet
602+
return toCharSet(node, flags)
609603
},
610604
getRegexpLocation: (range, offsets) => {
611605
if (offsets) {
@@ -652,7 +646,7 @@ function buildUnparsableRegExpContextBase({
652646
patternNode,
653647
regexpNode,
654648
context,
655-
flags,
649+
flags: originalFlags,
656650
flagsString,
657651
flagsNode,
658652
ownsFlags,
@@ -667,6 +661,7 @@ function buildUnparsableRegExpContextBase({
667661
ownsFlags: boolean
668662
}): UnparsableRegExpContextBase {
669663
const sourceCode = context.getSourceCode()
664+
const flags = toCache(originalFlags)
670665

671666
return {
672667
regexpNode,

0 commit comments

Comments
 (0)