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

Update refa & co #568

Merged
merged 6 commits into from
Sep 6, 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/cyan-waves-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Update refa, regexp-ast-analysis, and scslre
6 changes: 5 additions & 1 deletion lib/rules/confusing-quantifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ export default createRule("confusing-quantifier", {
*/
function createVisitor({
node,
flags,
getRegexpLocation,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onQuantifierEnter(qNode) {
if (qNode.min > 0 && isPotentiallyEmpty(qNode.element)) {
if (
qNode.min > 0 &&
isPotentiallyEmpty(qNode.element, flags)
) {
const proposal = quantToString({ ...qNode, min: 0 })
context.report({
node,
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/negation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@
// All other character sets are either case-invariant
// (/./, /\s/, /\d/) or inconsistent (/\w/).

// FIXME: TS Error

Check warning on line 56 in lib/rules/negation.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 56 in lib/rules/negation.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
const ccSet = toCharSet(ccNode, flags)

const negatedElementSet = toCharSet(
// FIXME: TS Error

Check warning on line 61 in lib/rules/negation.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 61 in lib/rules/negation.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
{
...element,
// FIXME: TS Error
// @ts-expect-error -- FIXME
negate: !element.negate,
},
flags,
Expand Down
23 changes: 14 additions & 9 deletions lib/rules/no-contradiction-with-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}

if (assertion.kind === "lookahead" || assertion.kind === "lookbehind") {
if (isPotentiallyEmpty(assertion.alternatives)) {
if (isPotentiallyEmpty(assertion.alternatives, flags)) {
// The assertion is guaranteed to trivially accept/reject.
return true
}
Expand Down Expand Up @@ -80,6 +80,7 @@
function* getNextElements(
start: Element,
dir: MatchingDirection,
flags: ReadonlyFlags,
): Iterable<Element> {
let element = start

Expand All @@ -102,7 +103,7 @@
}
}

// FIXME: TS Error

Check warning on line 106 in lib/rules/no-contradiction-with-assertion.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 106 in lib/rules/no-contradiction-with-assertion.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
const elements = parent.elements
const index = elements.indexOf(element) as number
Expand All @@ -110,7 +111,7 @@
for (let i = index + inc; i >= 0 && i < elements.length; i += inc) {
const e = elements[i]
yield e
if (!isZeroLength(e)) {
if (!isZeroLength(e, flags)) {
return
}
}
Expand Down Expand Up @@ -142,6 +143,7 @@
element: Element,
dir: MatchingDirection,
condition: (e: Element | Alternative) => boolean,
flags: ReadonlyFlags,
): boolean {
if (condition(element)) {
return true
Expand All @@ -151,7 +153,7 @@
// Go into the alternatives of groups
let some = false
element.alternatives.forEach((a) => {
if (tryFindContradictionInAlternative(a, dir, condition)) {
if (tryFindContradictionInAlternative(a, dir, condition, flags)) {
some = true
}
})
Expand All @@ -160,7 +162,7 @@

if (element.type === "Quantifier" && element.max === 1) {
// Go into the element of quantifiers if their maximum is 1
return tryFindContradictionIn(element.element, dir, condition)
return tryFindContradictionIn(element.element, dir, condition, flags)
}

if (
Expand All @@ -173,7 +175,7 @@
// Since we don't consume characters, we want to keep going even if we
// find a contradiction inside the lookaround.
element.alternatives.forEach((a) =>
tryFindContradictionInAlternative(a, dir, condition),
tryFindContradictionInAlternative(a, dir, condition, flags),
)
}

Expand All @@ -188,6 +190,7 @@
alternative: Alternative,
dir: MatchingDirection,
condition: (e: Element | Alternative) => boolean,
flags: ReadonlyFlags,
): boolean {
if (condition(alternative)) {
return true
Expand All @@ -199,10 +202,10 @@
const inc = dir === "ltr" ? 1 : -1
for (let i = first; i >= 0 && i < elements.length; i += inc) {
const e = elements[i]
if (tryFindContradictionIn(e, dir, condition)) {
if (tryFindContradictionIn(e, dir, condition, flags)) {
return true
}
if (!isZeroLength(e)) {
if (!isZeroLength(e, flags)) {
break
}
}
Expand Down Expand Up @@ -271,8 +274,10 @@
getFirstConsumedChar(assertion, dir, flags),
)

for (const element of getNextElements(assertion, dir)) {
if (tryFindContradictionIn(element, dir, contradicts)) {
for (const element of getNextElements(assertion, dir, flags)) {
if (
tryFindContradictionIn(element, dir, contradicts, flags)
) {
break
}
}
Expand Down
14 changes: 14 additions & 0 deletions lib/rules/no-dupe-characters-character-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
defineRegexpVisitor,
toCharSetSource,
fixRemoveCharacterClassElement,
assertValidFlags,
} from "../utils"
import type { CharRange, CharSet } from "refa"
import { JS } from "refa"
Expand Down Expand Up @@ -67,7 +68,7 @@
}

for (const e of elements) {
// FIXME: TS Error

Check warning on line 71 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 71 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
const charSet = toCharSet(e, flags)

Expand Down Expand Up @@ -276,6 +277,8 @@
// report characters that are already matched by some range or set
for (const char of characters) {
for (const other of rangesAndSets) {
// FIXME: TS Error

Check warning on line 280 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 280 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
if (toCharSet(other, flags).has(char.value)) {
reportSubset(regexpContext, char, other)
subsets.add(char)
Expand All @@ -292,7 +295,11 @@
}

if (
// FIXME: TS Error

Check warning on line 298 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 298 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
toCharSet(element, flags).isSubsetOf(
// FIXME: TS Error

Check warning on line 301 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 301 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
toCharSet(other, flags),
)
) {
Expand All @@ -317,16 +324,20 @@
const totalOthers = characterTotal.union(
...rangesAndSets
.filter((e) => !subsets.has(e) && e !== element)
// FIXME: TS Error

Check warning on line 327 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 327 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
.map((e) => toCharSet(e, flags)),
)

// FIXME: TS Error

Check warning on line 332 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 332 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
const elementCharSet = toCharSet(element, flags)
if (elementCharSet.isSubsetOf(totalOthers)) {
const superSetElements = ccNode.elements
.filter((e) => !subsets.has(e) && e !== element)
.filter(
(e) =>
// FIXME: TS Error

Check warning on line 340 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: TS Error'

Check warning on line 340 in lib/rules/no-dupe-characters-character-class.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'fixme' comment: 'FIXME: TS Error'
// @ts-expect-error -- FIXME
!toCharSet(e, flags).isDisjointWith(
elementCharSet,
Expand Down Expand Up @@ -359,6 +370,8 @@
const intersection = toCharSet(
range,
flags,
// FIXME: TS Error
// @ts-expect-error -- FIXME
).intersect(toCharSet(other, flags))
if (intersection.isEmpty) {
continue
Expand All @@ -379,6 +392,7 @@
// (see GH #189).
// to prevent this, we will create a new CharSet
// using `createCharSet`
assertValidFlags(flags)
const interest = JS.createCharSet(
interestingRanges,
flags,
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/no-dupe-disjunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
defineRegexpVisitor,
fixRemoveCharacterClassElement,
fixRemoveAlternative,
assertValidFlags,
} from "../utils"
import { getParser, isCoveredNode, isEqualNodes } from "../utils/regexp-ast"
import type { Expression, FiniteAutomaton, NoParent, ReadonlyNFA } from "refa"
Expand Down Expand Up @@ -434,6 +435,7 @@ function getPartialSubsetRelation(
*/
function faToSource(fa: FiniteAutomaton, flags: ReadonlyFlags): string {
try {
assertValidFlags(flags)
return JS.toLiteral(fa.toRegex(), { flags }).source
} catch (_error) {
return "<ERROR>"
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-empty-capturing-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export default createRule("no-empty-capturing-group", {
*/
function createVisitor({
node,
flags,
getRegexpLocation,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onCapturingGroupEnter(cgNode) {
if (isZeroLength(cgNode)) {
if (isZeroLength(cgNode, flags)) {
context.report({
node,
loc: getRegexpLocation(cgNode),
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-empty-lookarounds-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default createRule("no-empty-lookarounds-assertion", {
*/
function createVisitor({
node,
flags,
getRegexpLocation,
}: RegExpContext): RegExpVisitor.Handlers {
return {
Expand All @@ -35,7 +36,7 @@ export default createRule("no-empty-lookarounds-assertion", {
return
}

if (isPotentiallyEmpty(aNode.alternatives)) {
if (isPotentiallyEmpty(aNode.alternatives, flags)) {
context.report({
node,
loc: getRegexpLocation(aNode),
Expand Down
31 changes: 19 additions & 12 deletions lib/rules/no-misleading-capturing-group.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable eslint-comments/disable-enable-pair -- x */
/* eslint-disable complexity -- x */
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type {
Alternative,
Expand Down Expand Up @@ -48,10 +50,11 @@ function* iterReverse<T>(array: readonly T[]): Iterable<T> {
function* getStartQuantifiers(
root: Element | Alternative | Alternative[],
direction: MatchingDirection,
flags: ReadonlyFlags,
): Iterable<Quantifier> {
if (Array.isArray(root)) {
for (const a of root) {
yield* getStartQuantifiers(a, direction)
yield* getStartQuantifiers(a, direction, flags)
}
return
}
Expand All @@ -60,6 +63,7 @@ function* getStartQuantifiers(
case "Character":
case "CharacterClass":
case "CharacterSet":
case "ExpressionCharacterClass":
case "Backreference":
// we can't go into terminals
break
Expand All @@ -71,8 +75,8 @@ function* getStartQuantifiers(
const elements =
direction === "ltr" ? root.elements : iterReverse(root.elements)
for (const e of elements) {
if (isEmpty(e)) continue
yield* getStartQuantifiers(e, direction)
if (isEmpty(e, flags)) continue
yield* getStartQuantifiers(e, direction, flags)
break
}
break
Expand All @@ -82,17 +86,15 @@ function* getStartQuantifiers(
// of this rule
break
case "Group":
yield* getStartQuantifiers(root.alternatives, direction)
yield* getStartQuantifiers(root.alternatives, direction, flags)
break
case "Quantifier":
yield root
if (root.max === 1) {
yield* getStartQuantifiers(root.element, direction)
yield* getStartQuantifiers(root.element, direction, flags)
}
break
default:
// FIXME: TS Error
// @ts-expect-error -- FIXME
yield assertNever(root)
}
}
Expand Down Expand Up @@ -168,6 +170,9 @@ function uncachedGetSingleRepeatedChar(
case "Character":
case "CharacterClass":
case "CharacterSet":
case "ExpressionCharacterClass":
// FIXME: TS Error
// @ts-expect-error -- FIXME
return toCharSet(element, flags)

case "CapturingGroup":
Expand All @@ -181,8 +186,6 @@ function uncachedGetSingleRepeatedChar(
return getSingleRepeatedChar(element.element, flags, cache)

default:
// FIXME: TS Error
// @ts-expect-error -- FIXME
return assertNever(element)
}
}
Expand Down Expand Up @@ -226,6 +229,7 @@ function getTradingQuantifiersAfter(
case "Character":
case "CharacterClass":
case "CharacterSet":
case "ExpressionCharacterClass":
return state.intersect(
getSingleRepeatedChar(element, flags),
)
Expand All @@ -236,8 +240,6 @@ function getTradingQuantifiersAfter(
return state

default:
// FIXME: TS Error
// @ts-expect-error -- FIXME
return assertNever(element)
}
},
Expand Down Expand Up @@ -329,6 +331,7 @@ export default createRule("no-misleading-capturing-group", {
const startQuantifiers = getStartQuantifiers(
capturingGroup.alternatives,
direction,
flags,
)

for (const quantifier of startQuantifiers) {
Expand Down Expand Up @@ -409,6 +412,7 @@ export default createRule("no-misleading-capturing-group", {
const endQuantifiers = getStartQuantifiers(
capturingGroup.alternatives,
invertMatchingDirection(direction),
flags,
)

for (const quantifier of endQuantifiers) {
Expand Down Expand Up @@ -439,7 +443,10 @@ export default createRule("no-misleading-capturing-group", {
}
if (
trader.quant.min >= 1 &&
!isPotentiallyZeroLength(trader.quant.element)
!isPotentiallyZeroLength(
trader.quant.element,
flags,
)
)
context.report({
node,
Expand Down
Loading