Skip to content

Commit c605fd8

Browse files
committed
[Fix] jsx-indent: avoid checking returns sans jsx
Fixes #3218
1 parent 13d8df6 commit c605fd8

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

88
### Fixed
99
* [`jsx-key`]: prevent false "missing array key" warning ([#3215][] @ljharb)
10+
* [`jsx-indent`]: avoid checking returns sans jsx ([#3218][] @ljharb)
1011

12+
[#3218]: https://github.com/yannickcr/eslint-plugin-react/issues/3218
1113
[#3215]: https://github.com/yannickcr/eslint-plugin-react/issues/3215
1214

1315
## [7.29.0] - 2022.02.24

lib/rules/jsx-indent.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const matchAll = require('string.prototype.matchall');
3535
const astUtil = require('../util/ast');
3636
const docsUrl = require('../util/docsUrl');
3737
const reportC = require('../util/report');
38+
const jsxUtil = require('../util/jsx');
39+
const isCreateElement = require('../util/isCreateElement');
3840

3941
// ------------------------------------------------------------------------------
4042
// Rule Definition
@@ -417,6 +419,17 @@ module.exports = {
417419
return;
418420
}
419421

422+
let fn = node.parent;
423+
while (fn && fn.type !== 'FunctionDeclaration' && fn.type !== 'FunctionExpression') {
424+
fn = fn.parent;
425+
}
426+
if (
427+
!fn
428+
|| !jsxUtil.isReturningJSX((n) => isCreateElement(n, context), node, context)
429+
) {
430+
return;
431+
}
432+
420433
const openingIndent = getNodeIndent(node);
421434
const closingIndent = getNodeIndent(node, true);
422435

tests/lib/rules/jsx-indent.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,38 @@ const Component = () => (
11771177
`,
11781178
options: [2],
11791179
},
1180+
{
1181+
code: `
1182+
export default class App extends React.Component {
1183+
state = {
1184+
name: '',
1185+
}
1186+
1187+
componentDidMount() {
1188+
this.fetchName()
1189+
.then(name => {
1190+
this.setState({name})
1191+
});
1192+
}
1193+
1194+
fetchName = () => {
1195+
const url = 'https://api.github.com/users/job13er'
1196+
return fetch(url)
1197+
.then(resp => resp.json())
1198+
.then(json => json.name)
1199+
}
1200+
1201+
render() {
1202+
const {name} = this.state
1203+
return (
1204+
<h1>Hello, {name}</h1>
1205+
)
1206+
}
1207+
}
1208+
`,
1209+
features: ['class fields'],
1210+
options: [2],
1211+
},
11801212
]),
11811213

11821214
invalid: parsers.all([].concat(

0 commit comments

Comments
 (0)