Skip to content

[New] jsx-props-no-multi-spaces: Add automatic fix for no line gap between props #3930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Added
* [`jsx-props-no-multi-spaces`]: improve autofix for multi-line ([#3930][] @justisb)

### Fixed
* [`no-unknown-property`]: allow `onLoad` on `body` ([#3923][] @DerekStapleton)

[#3930]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3930
[#3923]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3923

## [7.37.5] - 2025.04.03
Expand Down
16 changes: 16 additions & 0 deletions lib/rules/jsx-props-no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
});
}

Expand Down
69 changes: 59 additions & 10 deletions tests/lib/rules/jsx-props-no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 8"
type="button"
/>
`,
},
{
code: `
<button
title="Some button"
title="Some button 8"
onClick={(value) => {
console.log(value);
}}
Expand All @@ -104,7 +104,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 2"
// this is a comment
onClick={(value) => {
console.log(value);
Expand All @@ -116,7 +116,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 2"
// this is a comment
// this is a second comment
onClick={(value) => {
Expand All @@ -129,7 +129,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<App
foo="Some button" // comment
foo="Some button 3" // comment
// comment
bar=""
/>
Expand All @@ -138,7 +138,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 3"
/* this is a multiline comment
...
... */
Expand Down Expand Up @@ -263,6 +263,12 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`,
output: `
<button
title='Some button'${semver.satisfies(eslintPkg.version, '> 3') ? '' : '\n'}
type="button"
/>
`,
errors: [
{
messageId: 'noLineGap',
Expand All @@ -273,7 +279,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 4"

onClick={(value) => {
console.log(value);
Expand All @@ -282,6 +288,15 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`,
output: `
<button
title="Some button 4"${semver.satisfies(eslintPkg.version, '> 3') ? '' : '\n'}
onClick={(value) => {
console.log(value);
}}${semver.satisfies(eslintPkg.version, '> 3') ? '' : '\n'}
type="button"
/>
`,
errors: [
{
messageId: 'noLineGap',
Expand All @@ -297,7 +312,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 5"
// this is a comment
onClick={(value) => {
console.log(value);
Expand All @@ -306,6 +321,16 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`,
output: `
<button
title="Some button 5"
// this is a comment
onClick={(value) => {
console.log(value);
}}
type="button"
/>
`,
errors: [
{
messageId: 'noLineGap',
Expand All @@ -316,7 +341,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 6"
// this is a comment
// second comment

Expand All @@ -327,6 +352,17 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`,
output: `
<button
title="Some button 6"
// this is a comment
// second comment
onClick={(value) => {
console.log(value);
}}
type="button"
/>
`,
errors: [
{
messageId: 'noLineGap',
Expand All @@ -341,7 +377,7 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
{
code: `
<button
title="Some button"
title="Some button 7"
/*this is a
multiline
comment
Expand All @@ -354,6 +390,19 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`,
output: `
<button
title="Some button 7"
/*this is a
multiline
comment
*/
onClick={(value) => {
console.log(value);
}}
type="button"
/>
`,
errors: [
{
messageId: 'noLineGap',
Expand Down