Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-regexp-exec] skip malformed regexes (#6935)
Browse files Browse the repository at this point in the history
Skip malformed regular expressions
  • Loading branch information
yassin-kammoun-sonarsource committed Apr 19, 2023
1 parent 4f3750f commit 05ed60e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-regexp-exec.ts
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts
Expand Up @@ -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: [
{
Expand Down

0 comments on commit 05ed60e

Please sign in to comment.