JS-1113 Fix FP on S7759 for polyfill fallback using Date#getTime() for Date.now()#6333
Conversation
Ruling ReportCode no longer flagged (10 issues)S7759TypeScript/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 | |
|
github-actions[bot] 2026-02-05T13:10:39Z addressed The fix correctly detects and suppresses issues for these patterns:
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. |
f61f0a3 to
b516604
Compare
b516604 to
2145fad
Compare
|
LGTM |
2145fad to
fa8240c
Compare
|
francois-mora-sonarsource 2026-02-06T15:29:59Z addressed |
a8428d7 to
708698f
Compare
708698f to
ebe91e7
Compare
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>
ebe91e7 to
6a9f9c4
Compare
|




Summary
Fix false positives in rule S7759 when
Date#getTime()or+(new Date())is used as a polyfill fallback forDate.now(). These patterns are common in legacy code to provide compatibility with older environments that don't supportDate.now().Changes
Date.now || function() { return new Date().getTime(); }Date.now ? Date.now() : +(new Date())if (!Date.now) { Date.now = function() { ... }; }Relates to JS-1113