diff --git a/rules/prefer-math-min-max.js b/rules/prefer-math-min-max.js index 059a246fe5..e2bd0d2694 100644 --- a/rules/prefer-math-min-max.js +++ b/rules/prefer-math-min-max.js @@ -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)})`); + }, }); } @@ -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; diff --git a/test/prefer-math-min-max.mjs b/test/prefer-math-min-max.mjs index c3db318cd1..5f5b6b59d3 100644 --- a/test/prefer-math-min-max.mjs +++ b/test/prefer-math-min-max.mjs @@ -1,3 +1,4 @@ +import {outdent} from 'outdent'; import {getTester} from './utils/test.mjs'; const {test} = getTester(import.meta); @@ -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 + } + `, ], }); diff --git a/test/snapshots/prefer-math-min-max.mjs.md b/test/snapshots/prefer-math-min-max.mjs.md index 0ff8b033ee..03fa42dd24 100644 --- a/test/snapshots/prefer-math-min-max.mjs.md +++ b/test/snapshots/prefer-math-min-max.mjs.md @@ -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 | }␊ + ` diff --git a/test/snapshots/prefer-math-min-max.mjs.snap b/test/snapshots/prefer-math-min-max.mjs.snap index f765ee1743..fc4d52b974 100644 Binary files a/test/snapshots/prefer-math-min-max.mjs.snap and b/test/snapshots/prefer-math-min-max.mjs.snap differ