Skip to content

Commit 48de8a4

Browse files
committed
Fix edge case in prefer-math-min-max rule
1 parent 87815e9 commit 48de8a4

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

rules/prefer-math-min-max.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@ function reportPreferMathMinOrMax(context, node, left, right, method) {
1919
data: {
2020
replacement: `${method}()`,
2121
},
22-
fix: fixer => fixer.replaceText(node, `${method}(${sourceCode.getText(left)}, ${sourceCode.getText(right)})`),
22+
* fix(fixer) {
23+
/**
24+
* ```js
25+
* function a() {
26+
* return+foo > 10 ? 10 : +foo
27+
* }
28+
* ```
29+
*/
30+
if (node.parent.type === 'ReturnStatement' && node.parent.argument === node && node.parent.start + 'return'.length === node.start) {
31+
// If there is no space between ReturnStatement and ConditionalExpression, add a space.
32+
yield fixer.insertTextBefore(node, ' ');
33+
}
34+
35+
yield fixer.replaceText(node, `${method}(${sourceCode.getText(left)}, ${sourceCode.getText(right)})`);
36+
},
2337
});
2438
}
2539

@@ -36,7 +50,7 @@ const create = context => ({
3650
const {sourceCode} = context;
3751
const {operator, left, right} = test;
3852

39-
const checkTypes = new Set(['Literal', 'Identifier', 'MemberExpression', 'CallExpression']);
53+
const checkTypes = new Set(['Literal', 'Identifier', 'MemberExpression', 'CallExpression', 'UnaryExpression']);
4054

4155
if ([left, right, alternate, consequent].some(n => !checkTypes.has(n.type))) {
4256
return;

test/prefer-math-min-max.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {outdent} from 'outdent';
12
import {getTester} from './utils/test.mjs';
23

34
const {test} = getTester(import.meta);
@@ -32,5 +33,17 @@ test.snapshot({
3233
// Prefer `Math.max()`
3334
'height > maxHeight ? height : maxHeight',
3435
'height < maxHeight ? maxHeight : height',
36+
37+
// Edge test when there is no space between ReturnStatement and ConditionalExpression
38+
outdent`
39+
function a() {
40+
return +foo > 10 ? 10 : +foo
41+
}
42+
`,
43+
outdent`
44+
function a() {
45+
return+foo > 10 ? 10 : +foo
46+
}
47+
`,
3548
],
3649
});

test/snapshots/prefer-math-min-max.mjs.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,57 @@ Generated by [AVA](https://avajs.dev).
297297
> 1 | height < maxHeight ? maxHeight : height␊
298298
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`Math.max()\` to simplify ternary expressions.␊
299299
`
300+
301+
## invalid(15): function a() { return +foo > 10 ? 10 : +foo }
302+
303+
> Input
304+
305+
`␊
306+
1 | function a() {␊
307+
2 | return +foo > 10 ? 10 : +foo␊
308+
3 | }␊
309+
`
310+
311+
> Output
312+
313+
`␊
314+
1 | function a() {␊
315+
2 | return Math.min(+foo, 10)␊
316+
3 | }␊
317+
`
318+
319+
> Error 1/1
320+
321+
`␊
322+
1 | function a() {␊
323+
> 2 | return +foo > 10 ? 10 : +foo␊
324+
| ^^^^^^^^^^^^^^^^^^^^^ Prefer \`Math.min()\` to simplify ternary expressions.␊
325+
3 | }␊
326+
`
327+
328+
## invalid(16): function a() { return+foo > 10 ? 10 : +foo }
329+
330+
> Input
331+
332+
`␊
333+
1 | function a() {␊
334+
2 | return+foo > 10 ? 10 : +foo␊
335+
3 | }␊
336+
`
337+
338+
> Output
339+
340+
`␊
341+
1 | function a() {␊
342+
2 | return Math.min(+foo, 10)␊
343+
3 | }␊
344+
`
345+
346+
> Error 1/1
347+
348+
`␊
349+
1 | function a() {␊
350+
> 2 | return+foo > 10 ? 10 : +foo␊
351+
| ^^^^^^^^^^^^^^^^^^^^^ Prefer \`Math.min()\` to simplify ternary expressions.␊
352+
3 | }␊
353+
`
146 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)