Skip to content

Use new refa AST transformers #569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-spoons-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": patch
---

Use new refa AST transformers and fixed max character for `v`-flag regexes in `no-dupe-disjunctions` and `no-super-linear-move`.
24 changes: 9 additions & 15 deletions lib/rules/no-dupe-disjunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import { getParser, isCoveredNode, isEqualNodes } from "../utils/regexp-ast"
import type { Expression, FiniteAutomaton, NoParent, ReadonlyNFA } from "refa"
import {
combineTransformers,
Transformers,
DFA,
NFA,
Expand Down Expand Up @@ -144,17 +143,6 @@ function isNonRegular(node: Node): boolean {
)
}

const creationOption: Transformers.CreationOptions = {
ignoreAmbiguity: true,
ignoreOrder: true,
}
const assertionTransformer = combineTransformers([
Transformers.applyAssertions(creationOption),
Transformers.removeUnnecessaryAssertions(creationOption),
Transformers.inline(creationOption),
Transformers.removeDeadBranches(creationOption),
])

/**
* Create an NFA from the given element.
*
Expand All @@ -175,7 +163,13 @@ function toNFA(

let e
if (containsAssertions(expression)) {
e = transform(assertionTransformer, expression)
e = transform(
Transformers.simplify({
ignoreAmbiguity: true,
ignoreOrder: true,
}),
expression,
)
} else {
e = expression
}
Expand All @@ -191,7 +185,7 @@ function toNFA(
} catch (_error) {
return {
nfa: NFA.empty({
maxCharacter: parser.ast.flags.unicode ? 0x10ffff : 0xffff,
maxCharacter: parser.maxCharacter,
}),
partial: true,
}
Expand Down Expand Up @@ -270,7 +264,7 @@ function* iteratePartialAlternatives(
return
}

const maxCharacter = parser.ast.flags.unicode ? 0x10ffff : 0xffff
const maxCharacter = parser.maxCharacter
const partialParser = new PartialParser(parser, {
assertions: "throw",
backreferences: "throw",
Expand Down
22 changes: 7 additions & 15 deletions lib/rules/no-super-linear-move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
visitAst,
JS,
transform,
combineTransformers,
Transformers,
CombinedTransformer,
} from "refa"
import { getJSRegexppAst } from "../utils/regexp-ast"

Expand Down Expand Up @@ -99,24 +99,16 @@ function* findReachableQuantifiers(
}
}

const TRANFORMER_OPTIONS: Transformers.CreationOptions = {
const TRANSFORMER_OPTIONS: Transformers.CreationOptions = {
ignoreAmbiguity: true,
ignoreOrder: true,
}
const PASS_1 = combineTransformers([
Transformers.inline(TRANFORMER_OPTIONS),
Transformers.removeDeadBranches(TRANFORMER_OPTIONS),
Transformers.unionCharacters(TRANFORMER_OPTIONS),
Transformers.moveUpEmpty(TRANFORMER_OPTIONS),
Transformers.nestedQuantifiers(TRANFORMER_OPTIONS),
Transformers.removeUnnecessaryAssertions(TRANFORMER_OPTIONS),
Transformers.applyAssertions(TRANFORMER_OPTIONS),
])
const PASS_2 = combineTransformers([
Transformers.inline(TRANFORMER_OPTIONS),
Transformers.removeDeadBranches(TRANFORMER_OPTIONS),
const PASS_1 = Transformers.simplify(TRANSFORMER_OPTIONS)
const PASS_2 = new CombinedTransformer([
Transformers.inline(TRANSFORMER_OPTIONS),
Transformers.removeDeadBranches(TRANSFORMER_OPTIONS),
Transformers.replaceAssertions({
...TRANFORMER_OPTIONS,
...TRANSFORMER_OPTIONS,
replacement: "empty-set",
}),
])
Expand Down