Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [prefer-regexp-exec] Runtime errors on malformed regular expressions #6928

@yassin-kammoun-sonarsource

Description

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Issue Description

Hello friends,

The rule prefer-regexp-exec raises a runtime error in case the argument of String#match denotes a malformed regular expression.

For instance, linting

'foo'.match('[malformed');

gives

Oops! Something went wrong! :(

ESLint: 8.38.0

SyntaxError: Invalid regular expression: /[malformed/: Unterminated character class
Occurred while linting /tmp/buggy-prefer-regexp-exec/file.ts:1
Rule: "@typescript-eslint/prefer-regexp-exec"
    at RegExp (<anonymous>)
    at CallExpression[arguments.length=1] > MemberExpression.callee[property.name='match'][computed=false] (/tmp/buggy-prefer-regexp-exec/node_modules/@typescript-eslint/eslint-plugin/dist/rules/prefer-regexp-exec.js:112:36)
    at ruleErrorHandler (/tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/linter.js:1077:28)
    at /tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
    at NodeEventGenerator.applySelectors (/tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
    at NodeEventGenerator.enterNode (/tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/node-event-generator.js:340:14)
    at CodePathAnalyzer.enterNode (/tmp/buggy-prefer-regexp-exec/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:795:23)

After looking at the rule's implementation, the error is due to this particular line of code:

const regExp = RegExp(argumentNode.value);

It calls the constructor function RegExp on the argument of String#match. However, the constructor throws a runtime error if the (statically known) regex is malformed.

I would argue that the rule should not break even if it's linting a string literal denoting a malformed regular expression. Instead, it should just keep silent and ignore the visited node. Therefore, a simple fix could be:

let regExp: RegExp;
try {
    regExp = RegExp(argumentNode.value);
} catch {
    return;
}

Happy to provide a PR if the bug I reported and the fix I suggested make sense.

Reproduction Repository Link

https://github.com/yassin-kammoun-sonarsource/buggy-prefer-regexp-exec

Repro Steps

  1. clone the repo
  2. npm install
  3. npx eslint .

Versions

package version
@typescript-eslint/eslint-plugin 5.59.0
@typescript-eslint/parser 5.59.0
TypeScript 5.0.4
ESLint 8.38.0
node 18.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions