forked from jsx-eslint/eslint-plugin-react
/
forbid-foreign-prop-types.js
131 lines (113 loc) · 3.41 KB
/
forbid-foreign-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
/**
* @fileoverview Forbid using another component's propTypes
* @author Ian Christian Myers
*/
'use strict';
const docsUrl = require('../util/docsUrl');
module.exports = {
meta: {
docs: {
description: 'Forbid using another component\'s propTypes',
category: 'Best Practices',
recommended: false,
url: docsUrl('forbid-foreign-prop-types')
},
schema: [
{
type: 'object',
properties: {
allowInPropTypes: {
type: 'boolean'
}
},
additionalProperties: false
}
]
},
create: function(context) {
const config = context.options[0] || {};
const allowInPropTypes = config.allowInPropTypes || false;
// --------------------------------------------------------------------------
// Helpers
// --------------------------------------------------------------------------
function isLeftSideOfAssignment(node) {
return node.parent.type === 'AssignmentExpression' && node.parent.left === node;
}
function findParentAssignmentExpression(node) {
let parent = node.parent;
while (parent && parent.type !== 'Program') {
if (parent.type === 'AssignmentExpression') {
return parent;
}
parent = parent.parent;
}
return null;
}
function findParentClassProperty(node) {
let parent = node.parent;
while (parent && parent.type !== 'Program') {
if (parent.type === 'ClassProperty') {
return parent;
}
parent = parent.parent;
}
return null;
}
function isAllowedAssignment(node) {
if (!allowInPropTypes) {
return false;
}
const assignmentExpression = findParentAssignmentExpression(node);
if (
assignmentExpression &&
assignmentExpression.left &&
assignmentExpression.left.property &&
assignmentExpression.left.property.name === 'propTypes'
) {
return true;
}
const classProperty = findParentClassProperty(node);
if (
classProperty &&
classProperty.key &&
classProperty.key.name === 'propTypes'
) {
return true;
}
return false;
}
return {
MemberExpression: function(node) {
if (
node.property &&
(
!node.computed &&
node.property.type === 'Identifier' &&
node.property.name === 'propTypes' &&
!isLeftSideOfAssignment(node) &&
!isAllowedAssignment(node)
) || (
(node.property.type === 'Literal' || node.property.type === 'JSXText') &&
node.property.value === 'propTypes' &&
!isLeftSideOfAssignment(node) &&
!isAllowedAssignment(node)
)
) {
context.report({
node: node.property,
message: 'Using propTypes from another component is not safe because they may be removed in production builds'
});
}
},
ObjectPattern: function(node) {
const propTypesNode = node.properties.find(property => property.type === 'Property' && property.key.name === 'propTypes');
if (propTypesNode) {
context.report({
node: propTypesNode,
message: 'Using propTypes from another component is not safe because they may be removed in production builds'
});
}
}
};
}
};