From 63192f36884abd0cb1b1f8d8dc50ca3b2f476e29 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Wed, 19 Apr 2023 13:31:16 +0200 Subject: [PATCH] Skip malformed regular expressions --- packages/eslint-plugin/src/rules/prefer-regexp-exec.ts | 7 ++++++- .../eslint-plugin/tests/rules/prefer-regexp-exec.test.ts | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts index 60bf310947fa..5de7ab892a5a 100644 --- a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts +++ b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts @@ -125,7 +125,12 @@ export default createRule({ argumentNode.type === AST_NODE_TYPES.Literal && typeof argumentNode.value === 'string' ) { - const regExp = RegExp(argumentNode.value); + let regExp: RegExp; + try { + regExp = RegExp(argumentNode.value); + } catch { + return; + } return context.report({ node: memberNode.property, messageId: 'regExpExecOverStringMatch', diff --git a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts index 2ac3c7a07ba7..46f192ab0ca1 100644 --- a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts @@ -74,6 +74,12 @@ const matchCount = (str: string, re: RegExp) => { return (str.match(re) || []).length; }; `, + // https://github.com/typescript-eslint/typescript-eslint/issues/6928 + ` +function test(str: string) { + str.match('[a-z'); +} + `, ], invalid: [ { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy