Detect Date.parse(...) comparisons in require-invalid-date-check-before-compare - #52961
Merged
pelikhan merged 2 commits intoAug 15, 2026
Merged
Conversation
5 tasks
… comparisons Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix date comparison rule to include Date.parse checks
Detect Date.parse(...) comparisons in require-invalid-date-check-before-compare
Aug 15, 2026
pelikhan
marked this pull request as ready for review
August 15, 2026 20:38
Contributor
There was a problem hiding this comment.
Pull request overview
Extends the ESLint rule to detect unsafe relational comparisons involving Date.parse() results.
Changes:
- Tracks variable and inline
Date.parse()expressions. - Recognizes finite/NaN guards and adds tailored diagnostics.
- Adds tests for guarded and unguarded comparisons.
Show a summary per file
| File | Description |
|---|---|
eslint-factory/src/rules/require-invalid-date-check-before-compare.ts |
Adds Date.parse() detection, guard handling, and diagnostics. |
eslint-factory/src/rules/require-invalid-date-check-before-compare.test.ts |
Tests new detection and guard patterns. |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Suppressed comments (1)
eslint-factory/src/rules/require-invalid-date-check-before-compare.ts:290
- Recording
Number.isFinite(a)as a generic guard loses the predicate's polarity. The existing dominance logic will therefore accept unsafe forms such asif (Number.isFinite(a)) return; return a >= cutoffandreturn Number.isFinite(a) || a >= cutoff, where the comparison is reached specifically whenais invalid. Preserve whether a guard is valid-when-true and only accept the matching exit/short-circuit branch (for example,finite && comparisonor an exit on!finite).
const isFiniteTarget = extractIsFiniteCheckTarget(node);
if (isFiniteTarget) {
const variable = resolveVariable(sourceCode, isFiniteTarget);
if (variable) {
const ancestors = sourceCode.getAncestors(node);
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
Comment on lines
+279
to
+283
| const directNaNTarget = extractDirectNaNCheckTarget(node); | ||
| if (directNaNTarget) { | ||
| const variable = resolveVariable(sourceCode, directNaNTarget); | ||
| if (variable) addGuardPath(variable, sourceCode.getAncestors(node), node); | ||
| return; |
Contributor
|
🎉 This pull request is included in a new release. Release: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
require-invalid-date-check-before-compareonly trackednew Date(...)+.getTime()construction, butDate.parse(x)follows the exact same hazard: it returnsNaNfor unparseable input, and a subsequent relational comparison (<,>,<=,>=) silently evaluates tofalseinstead of surfacing an error.Date.parseis actually the more common idiom for timestamp comparisons inactions/setup/js, so the rule was blind to its most likely regression site.Detection
isPotentiallyInvalidDateParseCallto recognizeDate.parse(x)the same wayisPotentiallyInvalidDateConstructionrecognizesnew Date(x).Date.parse(x)alongsidenew Date(x)variables, tagged with asource: "construct" | "parse"so guard/message logic can differentiate them.BinaryExpressionhandling now also treats an inlineDate.parse(...)call as a flaggable comparison side, mirroring inlinenew Date(...).Guard recognition
isIsNaNCallee/isIsFiniteCalleehelpers to recognize both global (isNaN/isFinite) and static (Number.isNaN/Number.isFinite) forms.extractDirectNaNCheckTarget/extractIsFiniteCheckTargetto recognizeNumber.isNaN(name)/!Number.isNaN(name)andNumber.isFinite(name)applied directly to the parsed number (no.getTime()involved) as valid guards.guardDominatesComparison,isExitingIfGuard,guardDirectlyGatesComparison) unchanged, with one addition: when aNumber.isFinite(name)guard is wrapped in!, the enclosing negation is also registered so exiting-guard patterns likeif (!Number.isFinite(a)) return false;are recognized.Messaging
requireInvalidDateCheckParsemessage describing theDate.parse/Number.isFiniteidiom, keeping the originalrequireInvalidDateCheckmessage (and its wording) fornew Date(...)cases untouched.new Date, oneDate.parse), each side is now reported individually with its own tailored message instead of forcing a single combined message.