From b6601433bb8f17afdb4de266624c4b4d709e0030 Mon Sep 17 00:00:00 2001 From: Justis Blasco Date: Mon, 2 Jun 2025 18:42:36 -0400 Subject: [PATCH] Add autocorrect for multi-line jsx-props-no-multi-spaces rule --- lib/rules/jsx-props-no-multi-spaces.js | 16 +++++++ tests/lib/rules/jsx-props-no-multi-spaces.js | 49 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/lib/rules/jsx-props-no-multi-spaces.js b/lib/rules/jsx-props-no-multi-spaces.js index 8402c6d4ff..5786506d11 100644 --- a/lib/rules/jsx-props-no-multi-spaces.js +++ b/lib/rules/jsx-props-no-multi-spaces.js @@ -80,6 +80,22 @@ module.exports = { prop1: getPropName(prev), prop2: getPropName(node), }, + fix(fixer) { + const comments = sourceCode.getCommentsBefore ? sourceCode.getCommentsBefore(node) : []; + const nodes = [].concat(prev, comments, node); + const fixes = []; + + for (let i = 1; i < nodes.length; i += 1) { + const prevNode = nodes[i - 1]; + const currNode = nodes[i]; + if (currNode.loc.start.line - prevNode.loc.end.line >= 2) { + const indent = ' '.repeat(currNode.loc.start.column); + fixes.push(fixer.replaceTextRange([prevNode.range[1], currNode.range[0]], `\n${indent}`)); + } + } + + return fixes; + }, }); } diff --git a/tests/lib/rules/jsx-props-no-multi-spaces.js b/tests/lib/rules/jsx-props-no-multi-spaces.js index d85573702f..c82487d1e1 100644 --- a/tests/lib/rules/jsx-props-no-multi-spaces.js +++ b/tests/lib/rules/jsx-props-no-multi-spaces.js @@ -263,6 +263,12 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, { type="button" /> `, + output: ` +