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

Handle no-props-unused-props in custom validators #1518

Merged
merged 1 commit into from
Nov 7, 2017
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
7 changes: 7 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,13 @@ module.exports = {
types.name = key;
types.node = value;
declaredPropTypes.push(types);
// Handle custom prop validators using props inside
if (
value.type === 'ArrowFunctionExpression'
|| value.type === 'FunctionExpression'
) {
markPropTypesAsUsed(value);
}
});
break;
case 'MemberExpression':
Expand Down
102 changes: 87 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,78 @@ 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}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a test case for value({options, value, validate}) {?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Added the test case.

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({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 +2868,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 +2890,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 +2905,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 +2925,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