Skip to content

Commit cd53d9e

Browse files
authored
prefer-math-min-max: Ignore Date objects (#2903)
1 parent 6dc01d2 commit cd53d9e

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

rules/prefer-math-min-max.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable complexity */
2-
import {isBigIntLiteral, isCallExpression} from './ast/index.js';
2+
import {isBigIntLiteral, isCallExpression, isNewExpression} from './ast/index.js';
33
import {fixSpaceAroundKeyword} from './fix/index.js';
44

55
const MESSAGE_ID = 'prefer-math-min-max';
@@ -58,6 +58,12 @@ const create = context => {
5858
return;
5959
}
6060

61+
const hasDate = [left, right].some(node => isNewExpression(node, {name: 'Date'}));
62+
63+
if (hasDate) {
64+
return;
65+
}
66+
6167
const [leftText, rightText, alternateText, consequentText] = [left, right, alternate, consequent].map(node => getExpressionText(node, context.sourceCode));
6268

6369
const isGreaterOrEqual = operator === '>' || operator === '>=';
@@ -158,6 +164,17 @@ const create = context => {
158164
return;
159165
}
160166

167+
/**
168+
Capture the following statement
169+
170+
```js
171+
var foo = new Date()
172+
```
173+
*/
174+
if (isNewExpression(variableDeclarator.init, {name: 'Date'})) {
175+
return;
176+
}
177+
161178
break;
162179
}
163180

test/prefer-math-min-max.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ test.snapshot({
1313
'foo > 10n ? 10n : foo',
1414
'foo > BigInt(10) ? BigInt(10) : foo',
1515

16+
// Ignore Date objects
17+
'new Date() > foo ? foo : new Date()',
18+
'foo > new Date(0) ? new Date(0) : foo',
19+
outdent`
20+
var now = new Date();
21+
var later = new Date();
22+
var value = now > later ? later : now;
23+
`,
24+
outdent`
25+
const start = new Date();
26+
var value = start > foo ? foo : start;
27+
`,
28+
1629
// Ignore when you know it is a string
1730
outdent`
1831
function foo(a = 'string', b) {
@@ -118,6 +131,16 @@ test.snapshot({
118131
return a > b ? a : b;
119132
}
120133
`,
134+
outdent`
135+
function foo(a: Date, b: Date) {
136+
return a > b ? a : b;
137+
}
138+
`,
139+
outdent`
140+
var foo: Date;
141+
var bar: Date;
142+
var value = foo > bar ? bar : foo;
143+
`,
121144
outdent`
122145
var foo = 10;
123146
var bar = '20';

0 commit comments

Comments
 (0)