-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
no-unused-prop-types.js
171 lines (148 loc) · 4.78 KB
/
no-unused-prop-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @fileoverview Prevent definitions of unused prop types
* @author Evgueni Naverniouk
*/
'use strict';
const values = require('object.values');
// As for exceptions for props.children or props.className (and alike) look at
// https://github.com/jsx-eslint/eslint-plugin-react/issues/7
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
/**
* Checks if the component must be validated
* @param {Object} component The component to process
* @returns {boolean} True if the component must be validated, false if not.
*/
function mustBeValidated(component) {
return !!component && !component.ignoreUnusedPropTypesValidation;
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
unusedPropType: '\'{{name}}\' PropType is defined but prop is never used',
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description: 'Disallow definitions of unused propTypes',
category: 'Best Practices',
recommended: false,
url: docsUrl('no-unused-prop-types'),
},
messages,
schema: [{
type: 'object',
properties: {
ignore: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
customValidators: {
type: 'array',
items: {
type: 'string',
},
},
skipShapeProps: {
type: 'boolean',
},
},
additionalProperties: false,
}],
},
create: Components.detect((context, components) => {
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 a prop is used
* @param {ASTNode} node The AST node being checked.
* @param {Object} prop Declared prop object
* @returns {boolean} True if the prop is used, false if not.
*/
function isPropUsed(node, prop) {
const usedPropTypes = node.usedPropTypes || [];
for (let i = 0, l = usedPropTypes.length; i < l; i++) {
const usedProp = usedPropTypes[i];
if (
prop.type === 'shape'
|| prop.type === 'exact'
|| prop.name === '__ANY_KEY__'
|| usedProp.name === prop.name
) {
return true;
}
}
return false;
}
/**
* Used to recursively loop through each declared prop type
* @param {Object} component The component to process
* @param {ASTNode[]|true} props List of props to validate
*/
function reportUnusedPropType(component, props) {
// Skip props that check instances
if (props === true) {
return;
}
Object.keys(props || {}).forEach((key) => {
const prop = props[key];
// Skip props that check instances
if (prop === true) {
return;
}
if ((prop.type === 'shape' || prop.type === 'exact') && configuration.skipShapeProps) {
return;
}
if (prop.node && prop.node.typeAnnotation && prop.node.typeAnnotation.typeAnnotation
&& prop.node.typeAnnotation.typeAnnotation.type === 'TSNeverKeyword') {
return;
}
if (prop.node && !isIgnored(prop.fullName) && !isPropUsed(component, prop)) {
report(context, messages.unusedPropType, 'unusedPropType', {
node: prop.node.key || prop.node,
data: {
name: prop.fullName,
},
});
}
if (prop.children) {
reportUnusedPropType(component, prop.children);
}
});
}
/**
* Reports unused proptypes for a given component
* @param {Object} component The component to process
*/
function reportUnusedPropTypes(component) {
reportUnusedPropType(component, component.declaredPropTypes);
}
// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
return {
'Program:exit'() {
// Report undeclared proptypes for all classes
values(components.list())
.filter((component) => mustBeValidated(component))
.forEach((component) => {
reportUnusedPropTypes(component);
});
},
};
}),
};