Skip to content

Commit

Permalink
Handle no-props-unused-props in custom validators
Browse files Browse the repository at this point in the history
When a custom prop validator is used mark props used inside the
validator as used.

Fixes #1068
  • Loading branch information
petersendidit committed Nov 6, 2017
1 parent 409515f commit 72d3d67
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
6 changes: 6 additions & 0 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,12 @@ module.exports = {
types.name = key;
types.node = value;
declaredPropTypes.push(types);
if (
value.type === 'ArrowFunctionExpression'
|| value.type === 'FunctionExpression'
) {
markPropTypesAsUsed(value);
}
});
break;
case 'MemberExpression':
Expand Down
78 changes: 63 additions & 15 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,10 @@ ruleTester.run('no-unused-prop-types', rule, {
type PropsA = { a: string }
type PropsB = { b: string }
type Props = PropsA & PropsB;
class MyComponent extends React.Component {
props: Props;
render() {
return <div>{this.props.a} - {this.props.b}</div>
}
Expand All @@ -848,7 +848,7 @@ ruleTester.run('no-unused-prop-types', rule, {
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
}
Expand All @@ -864,7 +864,7 @@ ruleTester.run('no-unused-prop-types', rule, {
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
}
Expand All @@ -876,12 +876,12 @@ ruleTester.run('no-unused-prop-types', rule, {
type PropsB = { foo: string };
type PropsC = { bar: string };
type Props = PropsB & {
zap: string
zap: string
};
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
}
Expand All @@ -893,12 +893,12 @@ ruleTester.run('no-unused-prop-types', rule, {
type PropsB = { foo: string };
type PropsC = { bar: string };
type Props = {
zap: string
zap: string
} & PropsB;
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
}
Expand Down Expand Up @@ -2208,6 +2208,54 @@ ruleTester.run('no-unused-prop-types', rule, {
`,
settings: {react: {flowVersion: '0.53'}},
parser: 'babel-eslint'
}, {
// Issue #1068
code: `
class MyComponent extends Component {
static propTypes = {
validate: PropTypes.bool,
options: PropTypes.array,
value: ({options, value, validate}) => {
if (!validate) return;
if (options.indexOf(value) < 0)
throw new Errow('oops');
}
}
render() {
return <ul>
{this.props.options.map(option =>
<li className={this.props.value == option && "active"}>{option}</li>
)}
</ul>
}
}
`,
parser: 'babel-eslint'
}, {
// Issue #1068
code: `
class MyComponent extends Component {
static propTypes = {
validate: PropTypes.bool,
options: PropTypes.array,
value: function ({options, value, validate}) {
if (!validate) return;
if (options.indexOf(value) < 0)
throw new Errow('oops');
}
}
render() {
return <ul>
{this.props.options.map(option =>
<li className={this.props.value == option && "active"}>{option}</li>
)}
</ul>
}
}
`,
parser: 'babel-eslint'
}
],

Expand Down Expand Up @@ -2796,10 +2844,10 @@ ruleTester.run('no-unused-prop-types', rule, {
type PropsA = { a: string }
type PropsB = { b: string }
type Props = PropsA & PropsB;
class MyComponent extends React.Component {
props: Props;
render() {
return <div>{this.props.a}</div>
}
Expand All @@ -2818,7 +2866,7 @@ ruleTester.run('no-unused-prop-types', rule, {
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar}</div>
}
Expand All @@ -2833,12 +2881,12 @@ ruleTester.run('no-unused-prop-types', rule, {
type PropsB = { foo: string };
type PropsC = { bar: string };
type Props = PropsB & {
zap: string
zap: string
};
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar}</div>
}
Expand All @@ -2853,12 +2901,12 @@ ruleTester.run('no-unused-prop-types', rule, {
type PropsB = { foo: string };
type PropsC = { bar: string };
type Props = {
zap: string
zap: string
} & PropsB;
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.foo} - {this.props.bar}</div>
}
Expand Down

0 comments on commit 72d3d67

Please sign in to comment.