Skip to content

False positives in no-empty-alternative and prefer-character-class after updating eslint utils #828

Open
@kachkaev

Description

@kachkaev

Information:

  • ESLint version: 9.25.1
  • eslint-plugin-regexp version: 2.7.0

Description

After updating transient dependencies, I noticed new violations of regexp/no-empty-alternative and regexp/prefer-character-class rules.

This was related to updating @eslint-community/eslint-utils from 4.6.1 to 4.7.0. Both versions are within the dependency range:

"@eslint-community/eslint-utils": "^4.2.0",

This upstream PR looks relevant: eslint-community/eslint-utils#255

Here is an MWE:

const a = ["x", "y"] as const;
const b = ["x", "y", ""] as const;
const c = ["x", "y", "z"] as const;

const r1 = new RegExp(`^(${a.join("|")})$`);
const r2 = new RegExp(`^(${b.join("|")})$`);
const r3 = new RegExp(`^(${c.join("|")})$`);

Before @eslint-community/eslint-utils was updated, no errors were triggered for the above code. But now I see this:

const r1 = new RegExp(`^(${a.join("|")})$`);
const r2 = new RegExp(`^(${b.join("|")})$`); // This empty alternative might be a mistake. If not, use a quantifier instead. eslint(regexp/no-empty-alternative)
const r3 = new RegExp(`^(${c.join("|")})$`); // Unexpected the disjunction of single element alternatives. Use character class '[...]' instead. eslint(regexp/prefer-character-class)

I believe that these are false positives. If a RegExp is built from the variables, it is impossible to meet the rules without introducing multiple sources of truth.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions