Skip to content

Commit

Permalink
Fix edge case in prefer-math-min-max rule
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Aug 21, 2024
1 parent 87815e9 commit 48de8a4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
18 changes: 16 additions & 2 deletions rules/prefer-math-min-max.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ function reportPreferMathMinOrMax(context, node, left, right, method) {
data: {
replacement: `${method}()`,
},
fix: fixer => fixer.replaceText(node, `${method}(${sourceCode.getText(left)}, ${sourceCode.getText(right)})`),
* fix(fixer) {
/**
* ```js
* function a() {
* return+foo > 10 ? 10 : +foo
* }
* ```
*/
if (node.parent.type === 'ReturnStatement' && node.parent.argument === node && node.parent.start + 'return'.length === node.start) {
// If there is no space between ReturnStatement and ConditionalExpression, add a space.
yield fixer.insertTextBefore(node, ' ');
}

yield fixer.replaceText(node, `${method}(${sourceCode.getText(left)}, ${sourceCode.getText(right)})`);
},
});
}

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

const checkTypes = new Set(['Literal', 'Identifier', 'MemberExpression', 'CallExpression']);
const checkTypes = new Set(['Literal', 'Identifier', 'MemberExpression', 'CallExpression', 'UnaryExpression']);

if ([left, right, alternate, consequent].some(n => !checkTypes.has(n.type))) {
return;
Expand Down
13 changes: 13 additions & 0 deletions test/prefer-math-min-max.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {outdent} from 'outdent';
import {getTester} from './utils/test.mjs';

const {test} = getTester(import.meta);
Expand Down Expand Up @@ -32,5 +33,17 @@ test.snapshot({
// Prefer `Math.max()`
'height > maxHeight ? height : maxHeight',
'height < maxHeight ? maxHeight : height',

// Edge test when there is no space between ReturnStatement and ConditionalExpression
outdent`
function a() {
return +foo > 10 ? 10 : +foo
}
`,
outdent`
function a() {
return+foo > 10 ? 10 : +foo
}
`,
],
});
54 changes: 54 additions & 0 deletions test/snapshots/prefer-math-min-max.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,57 @@ Generated by [AVA](https://avajs.dev).
> 1 | height < maxHeight ? maxHeight : height␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`Math.max()\` to simplify ternary expressions.␊
`

## invalid(15): function a() { return +foo > 10 ? 10 : +foo }

> Input
`␊
1 | function a() {␊
2 | return +foo > 10 ? 10 : +foo␊
3 | }␊
`

> Output
`␊
1 | function a() {␊
2 | return Math.min(+foo, 10)␊
3 | }␊
`

> Error 1/1
`␊
1 | function a() {␊
> 2 | return +foo > 10 ? 10 : +foo␊
| ^^^^^^^^^^^^^^^^^^^^^ Prefer \`Math.min()\` to simplify ternary expressions.␊
3 | }␊
`

## invalid(16): function a() { return+foo > 10 ? 10 : +foo }

> Input
`␊
1 | function a() {␊
2 | return+foo > 10 ? 10 : +foo␊
3 | }␊
`

> Output
`␊
1 | function a() {␊
2 | return Math.min(+foo, 10)␊
3 | }␊
`

> Error 1/1
`␊
1 | function a() {␊
> 2 | return+foo > 10 ? 10 : +foo␊
| ^^^^^^^^^^^^^^^^^^^^^ Prefer \`Math.min()\` to simplify ternary expressions.␊
3 | }␊
`
Binary file modified test/snapshots/prefer-math-min-max.mjs.snap
Binary file not shown.

0 comments on commit 48de8a4

Please sign in to comment.