Skip to content

Commit

Permalink
[New] no-unused-prop-types: add ignore option
Browse files Browse the repository at this point in the history
  • Loading branch information
Geraint White committed Apr 21, 2021
1 parent 495a4cf commit 25b1ba5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
3 changes: 2 additions & 1 deletion docs/rules/no-unused-prop-types.md
Expand Up @@ -75,11 +75,12 @@ This rule can take one argument to ignore some specific props during validation.

```js
...
"react/no-unused-prop-types": [<enabled>, { customValidators: <customValidator>, skipShapeProps: <skipShapeProps> }]
"react/no-unused-prop-types": [<enabled>, { ignore: <ignore>, customValidators: <customValidator>, skipShapeProps: <skipShapeProps> }]
...
```

* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
* `ignore`: optional array of props name to ignore during validation.
* `customValidators`: optional array of validators used for propTypes validation.
* `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).

Expand Down
19 changes: 17 additions & 2 deletions lib/rules/no-unused-prop-types.js
Expand Up @@ -31,6 +31,12 @@ module.exports = {
schema: [{
type: 'object',
properties: {
ignore: {
type: 'array',
items: {
type: 'string'
}
},
customValidators: {
type: 'array',
items: {
Expand All @@ -46,9 +52,18 @@ module.exports = {
},

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

/**
* Checks if the prop is ignored
* @param {String} name Name of the prop to check.
* @returns {Boolean} True if the prop is ignored, false if not.
*/
function isIgnored(name) {
return configuration.ignore.indexOf(name) !== -1;
}

/**
* Checks if the component must be validated
* @param {Object} component The component to process
Expand Down Expand Up @@ -111,7 +126,7 @@ module.exports = {
return;
}

if (prop.node && !isPropUsed(component, prop)) {
if (prop.node && !isIgnored(prop.fullName) && !isPropUsed(component, prop)) {
context.report({
node: prop.node.key || prop.node,
messageId: 'unusedPropType',
Expand Down
42 changes: 36 additions & 6 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -3202,6 +3202,20 @@ ruleTester.run('no-unused-prop-types', rule, {
}
`,
parser: parsers.BABEL_ESLINT
}, {
code: `
type Props = {
used: string,
foo: string,
}
class Hello extends React.Component<Props> {
render() {
return <div>{this.props.used}</div>;
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{ignore: ['foo']}]
}, {
code: `
export default class Foo extends React.Component {
Expand Down Expand Up @@ -5488,6 +5502,24 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [{
message: '\'lastname\' PropType is defined but prop is never used'
}]
}, {
code: `
type Props = {
firstname: string,
lastname: string,
foo: string,
}
class MyComponent extends React.Component<Props> {
render() {
return <div>Hello {this.props.firstname}</div>
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{ignore: ['foo']}],
errors: [{
message: '\'lastname\' PropType is defined but prop is never used'
}]
}, {
code: `
type Person = string;
Expand Down Expand Up @@ -6941,11 +6973,9 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [{
message: '\'prop1\' PropType is defined but prop is never used'
}]
}])

/* , {
// Enable this when the following issue is fixed
// https://github.com/yannickcr/eslint-plugin-react/issues/296
}]),
// Issue: #296
{
code: [
'function Foo(props) {',
' const { bar: { nope } } = props;',
Expand All @@ -6963,6 +6993,6 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [{
message: '\'foo\' PropType is defined but prop is never used'
}]
} */
}
)
});

0 comments on commit 25b1ba5

Please sign in to comment.