Skip to content

Commit 6cf3812

Browse files
Geraint Whiteljharb
authored andcommitted
[New] no-unused-prop-types: add ignore option
1 parent 2ddedd5 commit 6cf3812

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77

88
### Added
99
* component detection: add componentWrapperFunctions setting ([#2713][] @@jzabala @LandonSchropp)
10+
* [`no-unused-prop-types`]: add ignore option ([#2972][] @grit96)
1011

1112
### Fixed
1213
* [`jsx-handler-names`]: properly substitute value into message ([#2975][] @G-Rath)
@@ -16,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1617

1718
[#2975]: https://github.com/yannickcr/eslint-plugin-react/pull/2975
1819
[#2974]: https://github.com/yannickcr/eslint-plugin-react/pull/2974
20+
[#2972]: https://github.com/yannickcr/eslint-plugin-react/pull/2972
1921
[#2713]: https://github.com/yannickcr/eslint-plugin-react/pull/2713
2022

2123
## [7.23.2] - 2021.04.08

docs/rules/no-unused-prop-types.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ This rule can take one argument to ignore some specific props during validation.
7575

7676
```js
7777
...
78-
"react/no-unused-prop-types": [<enabled>, { customValidators: <customValidator>, skipShapeProps: <skipShapeProps> }]
78+
"react/no-unused-prop-types": [<enabled>, { ignore: <ignore>, customValidators: <customValidator>, skipShapeProps: <skipShapeProps> }]
7979
...
8080
```
8181

8282
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
83+
* `ignore`: optional array of props name to ignore during validation.
8384
* `customValidators`: optional array of validators used for propTypes validation.
8485
* `skipShapeProps`: In some cases it is impossible to accurately detect whether or not a `PropTypes.shape`'s values are being used. Setting this option to `true` will skip validation of `PropTypes.shape` (`true` by default).
8586

lib/rules/no-unused-prop-types.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ module.exports = {
3131
schema: [{
3232
type: 'object',
3333
properties: {
34+
ignore: {
35+
type: 'array',
36+
items: {
37+
type: 'string'
38+
},
39+
uniqueItems: true
40+
},
3441
customValidators: {
3542
type: 'array',
3643
items: {
@@ -46,9 +53,18 @@ module.exports = {
4653
},
4754

4855
create: Components.detect((context, components) => {
49-
const defaults = {skipShapeProps: true, customValidators: []};
56+
const defaults = {skipShapeProps: true, customValidators: [], ignore: []};
5057
const configuration = Object.assign({}, defaults, context.options[0] || {});
5158

59+
/**
60+
* Checks if the prop is ignored
61+
* @param {String} name Name of the prop to check.
62+
* @returns {Boolean} True if the prop is ignored, false if not.
63+
*/
64+
function isIgnored(name) {
65+
return configuration.ignore.indexOf(name) !== -1;
66+
}
67+
5268
/**
5369
* Checks if the component must be validated
5470
* @param {Object} component The component to process
@@ -111,7 +127,7 @@ module.exports = {
111127
return;
112128
}
113129

114-
if (prop.node && !isPropUsed(component, prop)) {
130+
if (prop.node && !isIgnored(prop.fullName) && !isPropUsed(component, prop)) {
115131
context.report({
116132
node: prop.node.key || prop.node,
117133
messageId: 'unusedPropType',

tests/lib/rules/no-unused-prop-types.js

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,33 @@ ruleTester.run('no-unused-prop-types', rule, {
32023202
}
32033203
`,
32043204
parser: parsers.BABEL_ESLINT
3205+
}, {
3206+
code: `
3207+
class Hello extends React.Component {
3208+
render() {
3209+
return <div>{this.props.used}</div>;
3210+
}
3211+
}
3212+
Hello.propTypes = {
3213+
used: PropTypes.string,
3214+
foo: PropTypes.string
3215+
};
3216+
`,
3217+
options: [{ignore: ['foo']}]
3218+
}, {
3219+
code: `
3220+
type Props = {
3221+
used: string,
3222+
foo: string,
3223+
}
3224+
class Hello extends React.Component<Props> {
3225+
render() {
3226+
return <div>{this.props.used}</div>;
3227+
}
3228+
}
3229+
`,
3230+
parser: parsers.BABEL_ESLINT,
3231+
options: [{ignore: ['foo']}]
32053232
}, {
32063233
code: `
32073234
export default class Foo extends React.Component {
@@ -5488,6 +5515,41 @@ ruleTester.run('no-unused-prop-types', rule, {
54885515
errors: [{
54895516
message: '\'lastname\' PropType is defined but prop is never used'
54905517
}]
5518+
}, {
5519+
code: `
5520+
class MyComponent extends React.Component {
5521+
render() {
5522+
return <div>Hello {this.props.firstname}</div>
5523+
}
5524+
}
5525+
MyComponent.propTypes = {
5526+
firstname: PropTypes.string,
5527+
lastname: PropTypes.string,
5528+
foo: PropTypes.string,
5529+
};
5530+
`,
5531+
options: [{ignore: ['foo']}],
5532+
errors: [{
5533+
message: '\'lastname\' PropType is defined but prop is never used'
5534+
}]
5535+
}, {
5536+
code: `
5537+
type Props = {
5538+
firstname: string,
5539+
lastname: string,
5540+
foo: string,
5541+
}
5542+
class MyComponent extends React.Component<Props> {
5543+
render() {
5544+
return <div>Hello {this.props.firstname}</div>
5545+
}
5546+
}
5547+
`,
5548+
parser: parsers.BABEL_ESLINT,
5549+
options: [{ignore: ['foo']}],
5550+
errors: [{
5551+
message: '\'lastname\' PropType is defined but prop is never used'
5552+
}]
54915553
}, {
54925554
code: `
54935555
type Person = string;
@@ -6941,11 +7003,9 @@ ruleTester.run('no-unused-prop-types', rule, {
69417003
errors: [{
69427004
message: '\'prop1\' PropType is defined but prop is never used'
69437005
}]
6944-
}])
6945-
6946-
/* , {
6947-
// Enable this when the following issue is fixed
6948-
// https://github.com/yannickcr/eslint-plugin-react/issues/296
7006+
}]),
7007+
// Issue: #296
7008+
{
69497009
code: [
69507010
'function Foo(props) {',
69517011
' const { bar: { nope } } = props;',
@@ -6963,6 +7023,6 @@ ruleTester.run('no-unused-prop-types', rule, {
69637023
errors: [{
69647024
message: '\'foo\' PropType is defined but prop is never used'
69657025
}]
6966-
} */
7026+
}
69677027
)
69687028
});

0 commit comments

Comments
 (0)