Skip to content

JS-1113 Fix FP on S7759 for polyfill fallback using Date#getTime() for Date.now()#6333

Merged
ss-vibe-bot[bot] merged 4 commits intomasterfrom
fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus
Feb 6, 2026
Merged

JS-1113 Fix FP on S7759 for polyfill fallback using Date#getTime() for Date.now()#6333
ss-vibe-bot[bot] merged 4 commits intomasterfrom
fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus

Conversation

@ss-vibe-bot
Copy link
Contributor

@ss-vibe-bot ss-vibe-bot bot commented Feb 5, 2026

Summary

Fix false positives in rule S7759 when Date#getTime() or +(new Date()) is used as a polyfill fallback for Date.now(). These patterns are common in legacy code to provide compatibility with older environments that don't support Date.now().

Changes

  • Add decorator to detect and suppress reports for legitimate polyfill patterns:
    • Logical OR: Date.now || function() { return new Date().getTime(); }
    • Ternary: Date.now ? Date.now() : +(new Date())
    • IfStatement: if (!Date.now) { Date.now = function() { ... }; }
  • Add comprehensive test coverage for polyfill patterns
  • Add tests derived from real-world ruling analysis (chained ternary, class static property, export const patterns)
  • Sync expected ruling files

Relates to JS-1113

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Ruling Report

Code no longer flagged (10 issues)

S7759

TypeScript/lib/tsc.js:86

    84 | var ts;
    85 | (function (ts) {
>   86 |     ts.timestamp = typeof performance !== "undefined" && performance.now ? function () { return performance.now(); } : Date.now ? Date.now : function () { return +(new Date()); };
    87 | })(ts || (ts = {}));
    88 | (function (ts) {

TypeScript/lib/typingsInstaller.js:96

    94 | var ts;
    95 | (function (ts) {
>   96 |     ts.timestamp = typeof performance !== "undefined" && performance.now ? function () { return performance.now(); } : Date.now ? Date.now : function () { return +(new Date()); };
    97 | })(ts || (ts = {}));
    98 | (function (ts) {

TypeScript/src/compiler/performance.ts:5

     3 |     declare const performance: { now?(): number } | undefined;
     4 |     /** Gets a timestamp with (at least) ms resolution */
>    5 |     export const timestamp = typeof performance !== "undefined" && performance.now ? () => performance.now() : Date.now ? Date.now : () => +(new Date());
     6 | }
     7 | 

ace/lib/ace/mode/javascript/jshint.js:3188

  3186 |   // A (possibly faster) way to get the current timestamp as an integer.
  3187 |   var now = Date.now || function() {
> 3188 |     return new Date().getTime();
  3189 |   };
  3190 | 

ace/tool/perf-test.html:61

    59 |   <script>
    60 |     if (!Date.now) {
>   61 |         Date.now = function() { return (new Date()).getTime() };
    62 |     }
    63 | 

es5-shim/es5-shim.js:1619

  1617 |     if (!Date.now) {
  1618 |         Date.now = function now() {
> 1619 |             return new Date().getTime();
  1620 |         };
  1621 |     }

qunit/src/core/utilities.js:7

     5 | export const hasOwn = Object.prototype.hasOwnProperty;
     6 | export const now = Date.now || function() {
>    7 | 	return new Date().getTime();
     8 | };
     9 | 

qunit/test/autostart.html:16

    14 | 	( function() {
    15 | 		var now = Date.now || function() {
>   16 | 			return new Date().getTime();
    17 | 		};
    18 | 		var asyncDelay = 1000;

rxjs/src/Scheduler.ts:26

    24 | export class Scheduler implements IScheduler {
    25 | 
>   26 |   public static now: () => number = Date.now ? Date.now : () => +new Date();
    27 | 
    28 |   constructor(private SchedulerAction: typeof Action,

underscore/underscore.js:1464

  1462 |   // A (possibly faster) way to get the current timestamp as an integer.
  1463 |   _.now = Date.now || function() {
> 1464 |     return new Date().getTime();
  1465 |   };
  1466 | 

@ss-vibe-bot
Copy link
Contributor Author

ss-vibe-bot bot commented Feb 5, 2026

github-actions[bot] 2026-02-05T13:10:39Z addressed
Thanks for the ruling report! These 10 issues are expected to no longer be flagged - they are all polyfill fallback patterns where Date#getTime() or +(new Date()) is used as a fallback when Date.now is not available.

The fix correctly detects and suppresses issues for these patterns:

  • Logical OR: Date.now || function() { return new Date().getTime(); }
  • Ternary: Date.now ? Date.now : () => +(new Date())
  • If-statement: if (!Date.now) { Date.now = function() { ... }; }

All 10 examples in the report match these polyfill patterns and should not be flagged as issues. The expected ruling files have been synced to reflect this change.

@ss-vibe-bot ss-vibe-bot bot force-pushed the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch from f61f0a3 to b516604 Compare February 6, 2026 13:51
@ss-vibe-bot ss-vibe-bot bot marked this pull request as ready for review February 6, 2026 14:09
@ss-vibe-bot ss-vibe-bot bot force-pushed the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch from b516604 to 2145fad Compare February 6, 2026 14:33
@francois-mora-sonarsource
Copy link
Contributor

LGTM

@francois-mora-sonarsource francois-mora-sonarsource requested a review from a team February 6, 2026 15:30
@ss-vibe-bot ss-vibe-bot bot force-pushed the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch from 2145fad to fa8240c Compare February 6, 2026 15:33
@ss-vibe-bot
Copy link
Contributor Author

ss-vibe-bot bot commented Feb 6, 2026

francois-mora-sonarsource 2026-02-06T15:29:59Z addressed
Thanks for the review!

@ss-vibe-bot ss-vibe-bot bot force-pushed the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch from a8428d7 to 708698f Compare February 6, 2026 16:03
Copy link
Contributor

@francois-mora-sonarsource francois-mora-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM

@ss-vibe-bot ss-vibe-bot bot force-pushed the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch from 708698f to ebe91e7 Compare February 6, 2026 16:33
Vibe Bot and others added 4 commits February 6, 2026 17:03
Tests cover the scenario where Date#getTime() or +(new Date()) is used as
a polyfill fallback for Date.now(). The tests verify that the decorator
correctly suppresses reports for:
- Logical OR patterns: Date.now || function() { return new Date().getTime(); }
- Ternary patterns: Date.now ? Date.now() : +(new Date())
- IfStatement patterns: if (!Date.now) { Date.now = function() { ... }; }

True positives are also tested to ensure direct usage without polyfill
patterns still raises issues correctly.

Relates to JS-1113
Add decorator to suppress false positives when Date#getTime() or similar
patterns are used as polyfill fallbacks for Date.now(). The fix detects
three polyfill patterns: logical OR (Date.now || fallback), ternary
(Date.now ? primary : fallback), and if-statement (if (!Date.now) {
Date.now = fallback }).

Implementation follows the approved proposal, using a decorator to
traverse ancestor nodes and suppress reports when the flagged code is
inside a legitimate polyfill pattern.

Relates to JS-1113
Add test cases derived from real-world polyfill patterns found during
ruling analysis:
- Chained ternary polyfill (performance.now || Date.now || fallback)
- Class static property with ternary polyfill
- Export const with Date.now polyfill

The implementation correctly handles all 48 ruling entries with no
mismatches between expected and actual behavior.
Ticket: JS-1113

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ss-vibe-bot ss-vibe-bot bot force-pushed the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch from ebe91e7 to 6a9f9c4 Compare February 6, 2026 17:03
@sonarqube-next
Copy link

sonarqube-next bot commented Feb 6, 2026

@ss-vibe-bot ss-vibe-bot bot merged commit 3cf63a1 into master Feb 6, 2026
39 checks passed
@ss-vibe-bot ss-vibe-bot bot deleted the fix/JS-1113-fix-fp-on-s7759-polyfill-fallback-using-dategettime-for-datenow-opus branch February 6, 2026 17:34
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.

1 participant