|
| 1 | +/* |
| 2 | + * SonarQube JavaScript Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +// https://sonarsource.github.io/rspec/#/rspec/S7759/javascript |
| 18 | + |
| 19 | +import type { Rule } from 'eslint'; |
| 20 | +import type estree from 'estree'; |
| 21 | +import { generateMeta, interceptReport, isMemberExpression } from '../helpers/index.js'; |
| 22 | +import * as meta from './generated-meta.js'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Decorates the unicorn/prefer-date-now rule to filter out reports where |
| 26 | + * `new Date().getTime()`, `+(new Date())`, or similar patterns are used |
| 27 | + * inside a polyfill fallback that checks for `Date.now` availability. |
| 28 | + */ |
| 29 | +export function decorate(rule: Rule.RuleModule): Rule.RuleModule { |
| 30 | + return interceptReport( |
| 31 | + { |
| 32 | + ...rule, |
| 33 | + meta: generateMeta(meta, rule.meta), |
| 34 | + }, |
| 35 | + (context, descriptor) => { |
| 36 | + const node = (descriptor as { node?: estree.Node }).node; |
| 37 | + if (!node) { |
| 38 | + context.report(descriptor); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Check if the node is inside a polyfill pattern |
| 43 | + if (isInsidePolyfillPattern(node, context)) { |
| 44 | + return; // Suppress the report |
| 45 | + } |
| 46 | + |
| 47 | + context.report(descriptor); |
| 48 | + }, |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Checks if the flagged node is inside a Date.now polyfill pattern. |
| 54 | + */ |
| 55 | +function isInsidePolyfillPattern(node: estree.Node, context: Rule.RuleContext): boolean { |
| 56 | + const ancestors = context.sourceCode.getAncestors(node); |
| 57 | + |
| 58 | + for (const ancestor of ancestors) { |
| 59 | + // Pattern 1: Date.now || function() { return new Date().getTime(); } |
| 60 | + if (isLogicalOrPolyfill(node, ancestor, ancestors)) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + // Pattern 2: Date.now ? Date.now() : +(new Date()) |
| 65 | + if (isTernaryPolyfill(node, ancestor, ancestors)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + // Pattern 3: if (!Date.now) { Date.now = function() { ... }; } |
| 70 | + if (isIfStatementPolyfill(node, ancestor, ancestors)) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return false; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Checks for LogicalExpression pattern: Date.now || fallback |
| 80 | + * The flagged node should be in the right (fallback) branch. |
| 81 | + */ |
| 82 | +function isLogicalOrPolyfill( |
| 83 | + node: estree.Node, |
| 84 | + ancestor: estree.Node, |
| 85 | + ancestors: estree.Node[], |
| 86 | +): boolean { |
| 87 | + if (ancestor.type !== 'LogicalExpression' || ancestor.operator !== '||') { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Check if left operand is Date.now |
| 92 | + if (!isDateNow(ancestor.left)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + // Check if flagged node is in the right operand (fallback branch) |
| 97 | + return isDescendantOf(node, ancestor.right, ancestors); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Checks for ConditionalExpression pattern: Date.now ? Date.now() : fallback |
| 102 | + * The flagged node should be in the alternate (fallback) branch. |
| 103 | + */ |
| 104 | +function isTernaryPolyfill( |
| 105 | + node: estree.Node, |
| 106 | + ancestor: estree.Node, |
| 107 | + ancestors: estree.Node[], |
| 108 | +): boolean { |
| 109 | + if (ancestor.type !== 'ConditionalExpression') { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + // Check if test references Date.now (truthy check) |
| 114 | + if (!isDateNow(ancestor.test)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + // Check if flagged node is in the alternate (fallback) branch |
| 119 | + return isDescendantOf(node, ancestor.alternate, ancestors); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Checks for IfStatement pattern: if (!Date.now) { Date.now = ...; } |
| 124 | + * The flagged node should be inside the consequent block that assigns to Date.now. |
| 125 | + */ |
| 126 | +function isIfStatementPolyfill( |
| 127 | + node: estree.Node, |
| 128 | + ancestor: estree.Node, |
| 129 | + ancestors: estree.Node[], |
| 130 | +): boolean { |
| 131 | + if (ancestor.type !== 'IfStatement') { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + // Check if test is !Date.now |
| 136 | + const { test } = ancestor; |
| 137 | + if (test.type !== 'UnaryExpression' || test.operator !== '!') { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + if (!isDateNow(test.argument)) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + // Check if consequent contains an assignment to Date.now |
| 146 | + if (!containsDateNowAssignment(ancestor.consequent)) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + // Check if flagged node is in the consequent (not in the alternate/else branch) |
| 151 | + return isDescendantOf(node, ancestor.consequent, ancestors); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Checks if a node is the `Date.now` member expression. |
| 156 | + */ |
| 157 | +function isDateNow(node: estree.Node): boolean { |
| 158 | + return isMemberExpression(node, 'Date', 'now'); |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Checks if a node or its descendants contain an assignment to Date.now. |
| 163 | + */ |
| 164 | +function containsDateNowAssignment(node: estree.Node): boolean { |
| 165 | + if (node.type === 'ExpressionStatement') { |
| 166 | + return containsDateNowAssignment(node.expression); |
| 167 | + } |
| 168 | + |
| 169 | + if (node.type === 'AssignmentExpression') { |
| 170 | + return isDateNow(node.left); |
| 171 | + } |
| 172 | + |
| 173 | + if (node.type === 'BlockStatement') { |
| 174 | + return node.body.some(stmt => containsDateNowAssignment(stmt)); |
| 175 | + } |
| 176 | + |
| 177 | + return false; |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * Checks if `node` is a descendant of `potentialAncestor` using the ancestors array. |
| 182 | + */ |
| 183 | +function isDescendantOf( |
| 184 | + node: estree.Node, |
| 185 | + potentialAncestor: estree.Node, |
| 186 | + ancestors: estree.Node[], |
| 187 | +): boolean { |
| 188 | + // If node is exactly potentialAncestor, it's a descendant (trivially) |
| 189 | + if (node === potentialAncestor) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + // Walk up from node through ancestors to see if we pass through potentialAncestor |
| 194 | + for (const anc of ancestors) { |
| 195 | + if (anc === potentialAncestor) { |
| 196 | + return true; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return false; |
| 201 | +} |
0 commit comments