Skip to content

Commit

Permalink
[Fix] jsx-indent: avoid checking returns sans jsx
Browse files Browse the repository at this point in the history
Fixes #3218
  • Loading branch information
ljharb committed Feb 25, 2022
1 parent 13d8df6 commit c605fd8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

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

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

## [7.29.0] - 2022.02.24
Expand Down
13 changes: 13 additions & 0 deletions lib/rules/jsx-indent.js
Expand Up @@ -35,6 +35,8 @@ const matchAll = require('string.prototype.matchall');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const reportC = require('../util/report');
const jsxUtil = require('../util/jsx');
const isCreateElement = require('../util/isCreateElement');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -417,6 +419,17 @@ module.exports = {
return;
}

let fn = node.parent;
while (fn && fn.type !== 'FunctionDeclaration' && fn.type !== 'FunctionExpression') {
fn = fn.parent;
}
if (
!fn
|| !jsxUtil.isReturningJSX((n) => isCreateElement(n, context), node, context)
) {
return;
}

const openingIndent = getNodeIndent(node);
const closingIndent = getNodeIndent(node, true);

Expand Down
32 changes: 32 additions & 0 deletions tests/lib/rules/jsx-indent.js
Expand Up @@ -1177,6 +1177,38 @@ const Component = () => (
`,
options: [2],
},
{
code: `
export default class App extends React.Component {
state = {
name: '',
}
componentDidMount() {
this.fetchName()
.then(name => {
this.setState({name})
});
}
fetchName = () => {
const url = 'https://api.github.com/users/job13er'
return fetch(url)
.then(resp => resp.json())
.then(json => json.name)
}
render() {
const {name} = this.state
return (
<h1>Hello, {name}</h1>
)
}
}
`,
features: ['class fields'],
options: [2],
},
]),

invalid: parsers.all([].concat(
Expand Down

0 comments on commit c605fd8

Please sign in to comment.