Skip to content
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

[Fix] Detect JSX returned by sequential expression #2801

Merged
merged 1 commit into from Sep 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -18,7 +18,8 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`prop-types`]: Detect TypeScript types for destructured default prop values ([#2780][] @sunghyunjo)
* [`jsx-pascal-case`]: Handle single character namespaced component ([#2791][] @daviferreira)
* [`jsx-closing-bracket-location`]: In `tag-aligned`, made a distinction between tabs and spaces ([#2796][] @Moong0122)
* [`jsx-handler-names`]: false positive when handler name begins with number ([#2801][] @mikol)
* [`jsx-handler-names`]: false positive when handler name begins with number ([#1689][] @jsphstls)
* [`prop-types`]: Detect JSX returned by sequential expression ([#2801][] @mikol)

[#2801]: https://github.com/yannickcr/eslint-plugin-react/pull/2801
[#2796]: https://github.com/yannickcr/eslint-plugin-react/pull/2796
Expand All @@ -33,6 +34,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
[#2767]: https://github.com/yannickcr/eslint-plugin-react/pull/2767
[#2761]: https://github.com/yannickcr/eslint-plugin-react/pull/2761
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748
[#1689]: https://github.com/yannickcr/eslint-plugin-react/pull/1689

## [7.20.6] - 2020.08.12

Expand Down
8 changes: 8 additions & 0 deletions lib/util/Components.js
Expand Up @@ -71,6 +71,12 @@ function isReturnsLogicalJSX(node, property, strict) {
: (returnsLogicalJSXLeft || returnsLogicalJSXRight);
}

function isReturnsSequentialJSX(node, property) {
return node[property]
&& node[property].type === 'SequenceExpression'
&& jsxUtil.isJSX(node[property].expressions[node[property].expressions.length - 1]);
}

const Lists = new WeakMap();

/**
Expand Down Expand Up @@ -457,13 +463,15 @@ function componentRule(rule, context) {

const returnsConditionalJSX = isReturnsConditionalJSX(node, property, strict);
const returnsLogicalJSX = isReturnsLogicalJSX(node, property, strict);
const returnsSequentialJSX = isReturnsSequentialJSX(node, property);

const returnsJSX = node[property] && jsxUtil.isJSX(node[property]);
const returnsPragmaCreateElement = this.isCreateElement(node[property]);

return !!(
returnsConditionalJSX
|| returnsLogicalJSX
|| returnsSequentialJSX
|| returnsJSX
|| returnsPragmaCreateElement
);
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -2501,6 +2501,20 @@ ruleTester.run('prop-types', rule, {
export default function() {}
`
},
{
code: `
function Component(props) {
return 0,
<div>
Hello, { props.name }!
</div>
}

Component.propTypes = {
name: PropTypes.string.isRequired
}
`
},
parsers.TS([
{
code: `
Expand Down Expand Up @@ -5591,6 +5605,19 @@ ruleTester.run('prop-types', rule, {
message: '\'foo.baz\' is missing in props validation'
}]
},
{
code: `
function Component(props) {
return 0,
<div>
Hello, { props.name }!
</div>
}
`,
errors: [{
message: '\'name\' is missing in props validation'
}]
},
parsers.TS([
{
code: `
Expand Down