Skip to content

fix(prefer-regexp-exec): resolve RegExp copies and validate patterns with the TypeScript scanner - #1420

Merged
camc314 merged 1 commit into
mainfrom
fix/prefer-regexp-exec-static-regexp-validation
Nov 29, 2026
Merged

camc314 merged 1 commit into
mainfrom
fix/prefer-regexp-exec-static-regexp-validation

Conversation

@camc314

@camc314 camc314 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

This fixes three problems with RegExp constructor calls and string patterns:

  • new RegExp(/a/g) copies the g flag from its argument, but the rule only read the flags argument. It reported s.match(new RegExp(/a/g)) and changed the result from all matches to the first match. The static value of the constructor call now inherits flags from a static regex argument, and an undefined flags argument is treated as missing.
  • String patterns were validated with regexp2. This accepted syntax that JavaScript rejects, such as (?i)a, so the fix could produce invalid code. Patterns are now validated with TypeScript's regular expression scanner.
  • The fix wrote an empty pattern as //, which starts a comment, and wrote line terminators into the literal as is. Literals are now built the way V8's RegExp.prototype.toString builds them: an empty pattern becomes (?:), and slashes outside character classes and line terminators are escaped.
function f(s: string) {
  return s.match(new RegExp(/a/g)); // no longer reported
}
'a'.match('(?i)a'); // no longer reported
'a'.match(''); // fixed to /(?:)/.exec('a')

@camc314
camc314 marked this pull request as ready for review November 29, 2026 17:52
Copilot AI balanced review requested due to automatic review settings November 29, 2026 17:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T17:54:21.103848Z ce55a6c Draft marked ready
â„đïļ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@camc314
camc314 enabled auto-merge (squash) November 29, 2026 17:52
@camc314
camc314 merged commit baaf46b into main Nov 29, 2026
7 checks passed
@camc314
camc314 deleted the fix/prefer-regexp-exec-static-regexp-validation branch November 29, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants