Skip to content

Commit c9b7294

Browse files
Stefan Buckljharb
Stefan Buck
andcommitted
[Fix] jsx-indent: Fix indent handling for closing parentheses
Fixes #618. Co-authored-by: Stefan Buck <stefanb@brandwatch.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
1 parent 27e94ba commit c9b7294

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1717
* [`prefer-exact-props`] improve performance for `Identifier` visitor ([#3190][] @meowtec)
1818
* `propTypes`: Handle TSTypeReference in no-unused-prop-type ([#3195][] @niik)
1919
* [`sort-prop-types`]: avoid repeated warnings of the same node/reason ([#519][] @ljharb)
20+
* [`jsx-indent`]: Fix indent handling for closing parentheses ([#620][] @stefanbuck])
2021

2122
### Changed
2223
* [readme] change [`jsx-runtime`] link from branch to sha ([#3160][] @tatsushitoji)
@@ -40,6 +41,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
4041
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
4142
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
4243
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
44+
[#620]: https://github.com/yannickcr/eslint-plugin-react/pull/620
4345
[#519]: https://github.com/yannickcr/eslint-plugin-react/issues/519
4446

4547
## [7.28.0] - 2021.12.22

lib/rules/jsx-indent.js

+28
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ module.exports = {
114114
};
115115
}
116116

117+
if (node.type === 'ReturnStatement') {
118+
const raw = context.getSourceCode().getText(node);
119+
const lines = raw.split('\n');
120+
if (lines.length > 1) {
121+
return function fix(fixer) {
122+
const lastLineStart = raw.lastIndexOf('\n');
123+
const lastLine = raw.slice(lastLineStart).replace(/^\n[\t ]*(\S)/, (match, p1) => `\n${indent}${p1}`);
124+
return fixer.replaceTextRange(
125+
[node.range[0] + lastLineStart, node.range[1]],
126+
lastLine
127+
);
128+
};
129+
}
130+
}
131+
117132
return function fix(fixer) {
118133
return fixer.replaceTextRange(
119134
[node.range[0] - node.loc.start.column, node.range[0]],
@@ -396,6 +411,19 @@ module.exports = {
396411
},
397412
Literal: handleLiteral,
398413
JSXText: handleLiteral,
414+
415+
ReturnStatement(node) {
416+
if (!node.parent) {
417+
return;
418+
}
419+
420+
const openingIndent = getNodeIndent(node);
421+
const closingIndent = getNodeIndent(node, true);
422+
423+
if (openingIndent !== closingIndent) {
424+
report(node, openingIndent, closingIndent);
425+
}
426+
},
399427
};
400428
},
401429
};

tests/lib/rules/jsx-indent.js

+82
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,28 @@ const Component = () => (
10941094
}
10951095
`,
10961096
},
1097+
{
1098+
code: `
1099+
function App() {
1100+
return (
1101+
<App />
1102+
);
1103+
}
1104+
`,
1105+
options: [2],
1106+
parserOptions,
1107+
},
1108+
{
1109+
code: `
1110+
function App() {
1111+
return <App>
1112+
<Foo />
1113+
</App>;
1114+
}
1115+
`,
1116+
options: [2],
1117+
parserOptions,
1118+
},
10971119
]),
10981120

10991121
invalid: parsers.all([
@@ -1291,6 +1313,17 @@ const Component = () => (
12911313
errors: [
12921314
{
12931315
messageId: 'wrongIndent',
1316+
line: 3,
1317+
data: {
1318+
needed: 10,
1319+
type: 'space',
1320+
characters: 'characters',
1321+
gotten: 17,
1322+
},
1323+
},
1324+
{
1325+
messageId: 'wrongIndent',
1326+
line: 5,
12941327
data: {
12951328
needed: 10,
12961329
type: 'space',
@@ -1319,6 +1352,17 @@ const Component = () => (
13191352
errors: [
13201353
{
13211354
messageId: 'wrongIndent',
1355+
line: 3,
1356+
data: {
1357+
needed: 10,
1358+
type: 'space',
1359+
characters: 'characters',
1360+
gotten: 12,
1361+
},
1362+
},
1363+
{
1364+
messageId: 'wrongIndent',
1365+
line: 5,
13221366
data: {
13231367
needed: 10,
13241368
type: 'space',
@@ -2771,5 +2815,43 @@ const Component = () => (
27712815
},
27722816
],
27732817
},
2818+
{
2819+
code: `
2820+
function App() {
2821+
return (
2822+
<App />
2823+
);
2824+
}
2825+
`,
2826+
output: `
2827+
function App() {
2828+
return (
2829+
<App />
2830+
);
2831+
}
2832+
`,
2833+
options: [2],
2834+
parserOptions,
2835+
errors: [{ message: 'Expected indentation of 10 space characters but found 12.' }],
2836+
},
2837+
{
2838+
code: `
2839+
function App() {
2840+
return (
2841+
<App />
2842+
);
2843+
}
2844+
`,
2845+
output: `
2846+
function App() {
2847+
return (
2848+
<App />
2849+
);
2850+
}
2851+
`,
2852+
options: [2],
2853+
parserOptions,
2854+
errors: [{ message: 'Expected indentation of 10 space characters but found 8.' }],
2855+
},
27742856
]),
27752857
});

0 commit comments

Comments
 (0)