Skip to content
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

Improve no-dupe-disjunctions by reordering alternatives #281

Merged
merged 2 commits into from
Jul 28, 2021
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
27 changes: 24 additions & 3 deletions lib/rules/no-dupe-disjunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from "regexp-ast-analysis"
import { RegExpParser } from "regexpp"
import { UsageOfPattern } from "../utils/get-usage-of-pattern"
import { canReorder } from "../utils/reorder-alternatives"

type ParentNode = Group | CapturingGroup | Pattern | LookaroundAssertion

Expand Down Expand Up @@ -482,6 +483,7 @@ function* findPrefixDuplicationNfa(
*/
function* findDuplicationNfa(
alternatives: Alternative[],
context: RegExpContext,
{ hasNothingAfter, parser, ignoreOverlap }: Options,
): Iterable<Result> {
const previous: [NFA, boolean, Alternative][] = []
Expand Down Expand Up @@ -524,9 +526,28 @@ function* findDuplicationNfa(
yield { type: "Subset", alternative, others }
break

case SubsetRelation.leftSupersetOfRight:
yield { type: "Superset", alternative, others }
case SubsetRelation.leftSupersetOfRight: {
const reorder = canReorder(
[alternative, ...others],
context,
)

if (reorder) {
// We are allowed to freely reorder the alternatives.
// This means that we can reverse the order of our
// alternatives to convert the superset into a subset.
for (const other of others) {
yield {
type: "Subset",
alternative: other,
others: [alternative],
}
}
} else {
yield { type: "Superset", alternative, others }
}
break
}

case SubsetRelation.none:
case SubsetRelation.unknown:
Expand Down Expand Up @@ -574,7 +595,7 @@ function* findDuplication(

// NFA-based approach
if (!options.noNfa) {
yield* findDuplicationNfa(alternatives, options)
yield* findDuplicationNfa(alternatives, context, options)
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-dupe-disjunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ tester.run("no-dupe-disjunctions", rule as any, {
},
{
code: String(/\b(?:\d|foo|\w+)\b/),
errors: [
{
message:
"Unexpected useless alternative. This alternative is a strict subset of '\\w+' and can be removed.",
column: 7,
},
{
message:
"Unexpected useless alternative. This alternative is a strict subset of '\\w+' and can be removed.",
column: 10,
},
],
},
{
code: String(/(?:\d|foo|\w+)a/),
options: [{ report: "interesting" }],
errors: [
"Unexpected superset. This alternative is a superset of '\\d|foo'. It might be possible to remove the other alternative(s).",
Expand Down