JS-1997 Fix S1848 RegExp validation FP#7441
Conversation
Ruling Report✅ No changes to ruling expected issues in this PR |
|
|
|
||
| const nextStatement = getNextSiblingStatement(node, context); | ||
| if (nextStatement?.type !== 'ReturnStatement') { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks for the explanation, i'm ok with that
Code Review ✅ ApprovedRefines 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. OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |




Part of JS-1997
Summary
Validation