Skip to content

JS-1997 Fix S1848 RegExp validation FP#7441

Merged
francois-mora-sonarsource merged 5 commits into
masterfrom
fix/JS-1997-regexp-validation
Jul 7, 2026
Merged

JS-1997 Fix S1848 RegExp validation FP#7441
francois-mora-sonarsource merged 5 commits into
masterfrom
fix/JS-1997-regexp-validation

Conversation

@francois-mora-sonarsource

Copy link
Copy Markdown
Contributor

Part of JS-1997

Summary

  • Suppress S1848 for discarded built-in RegExp constructors used as a validation step before returning parsed regex data.
  • Keep reporting arbitrary discarded RegExp constructors, unrelated returns, and locally shadowed RegExp/globalThis constructors.
  • Add JS/TS-focused unit coverage for the Jira reproducer and guardrails.

Validation

  • npm run generate-meta
  • npx tsx --test packages/analysis/src/jsts/rules/S1848/**/*.test.ts
  • npm run eslint-plugin:check
  • npx prettier --check packages/analysis/src/jsts/rules/S1848/rule.ts packages/analysis/src/jsts/rules/S1848/unit.test.ts

@francois-mora-sonarsource francois-mora-sonarsource requested a review from a team July 2, 2026 13:24
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Jul 2, 2026

Copy link
Copy Markdown

JS-1997

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Ruling Report

No changes to ruling expected issues in this PR

@sonarqube-next

sonarqube-next Bot commented Jul 3, 2026

Copy link
Copy Markdown

@francois-mora-sonarsource francois-mora-sonarsource requested review from a team and removed request for a team July 3, 2026 09:27
Comment on lines +113 to +117

const nextStatement = getNextSiblingStatement(node, context);
if (nextStatement?.type !== 'ReturnStatement') {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would challenge the need for verifying that the next statement is a "return statement". Most likely a new RegExp(...) statement is always about validating a string, regardless of what follows this line. Wdyt ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I kept the return check intentionally to make the exception prove a very specific intent: the discarded new RegExp(...) is used to validate a value that is immediately returned.

A later use of the same identifier is weaker evidence. For example, this should still be reported:

new RegExp(pattern); // forgot: this.regexp = new RegExp(pattern);
this.pattern = pattern;

Here pattern is reused afterwards, but that does not show the constructor was only for validation. It is also compatible with a real S1848 bug where the regexp result was meant to be stored and got discarded by mistake.

So the current heuristic is deliberately narrow:

  • built-in RegExp
  • immediate following return
  • returned value directly contains one of the validated identifier arguments

That felt like the safest boundary for JS-1997. If we want to broaden it later, I think we would need more concrete cases than just identifier being used afterwards.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation, i'm ok with that

@francois-mora-sonarsource francois-mora-sonarsource merged commit 0de3ad6 into master Jul 7, 2026
48 checks passed
@francois-mora-sonarsource francois-mora-sonarsource deleted the fix/JS-1997-regexp-validation branch July 7, 2026 13:18
@gitar-bot

gitar-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

Refines RegExp constructor validation logic to suppress false positives in S1848 while maintaining strict checks for discarded instances. Comprehensive unit coverage confirms the fix for the specified JS-1997 use case.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

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