Skip to content

@typescript-eslint/prefer-regexp-exec does not respect flags #3477

@gabrielecirulli

Description

@gabrielecirulli
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

When used within a function whose arguments are passed to the new RegExp constructor, the @typescript-eslint/prefer-regexp-exec misfires.

function repro(pattern: string) {
  "hello hello".match(new RegExp(pattern, "gi"))?.reduce(() => []);
}

Gets replaced to (when the eslint --fix option is set):

function repro(pattern: string) {
  new RegExp(pattern, "gi").exec("hello hello")?.reduce(() => []);
}

This is an incorrect replacement because they are not equivalent due to the presence of the "g" flag.

Expected Result

The replacement shouldn't take place, the rule shouldn't error.

Actual Result

The rule returns an error and replaces the match if allowed to.

Additional Info

Versions

typescript 4.3.2
@typescript-eslint/eslint-plugin 4.25.0
@typescript-eslint/parser 4.25.0
eslint 7.27.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions