Skip to content

Commit

Permalink
[Fix] Detect JSX returned by sequential expression
Browse files Browse the repository at this point in the history
Resolves #2800. Implements `isReturnsSequentialJSX()` and uses it in
`isReturningJSX()` (in conjunction with `isReturnsConditionalJSX()` and
`isReturnsLogicalJSX()`).
  • Loading branch information
mikol committed Sep 20, 2020
1 parent 153eac8 commit dec2975
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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

0 comments on commit dec2975

Please sign in to comment.