Skip to content

Commit 54b738d

Browse files
committed
Move common props functions to util
1 parent a9f5fab commit 54b738d

8 files changed

+86
-172
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const has = require('has');
88
const Components = require('../util/Components');
9+
const propsUtil = require('../util/props');
910

1011
// ------------------------------------------------------------------------------
1112
// Rule Definition
@@ -49,26 +50,6 @@ module.exports = {
4950
// Remembers all Flowtype object definitions
5051
const objectTypeAnnotations = new Map();
5152

52-
/**
53-
* Checks if node is `propTypes` declaration
54-
* @param {ASTNode} node The AST node being checked.
55-
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
56-
*/
57-
function isPropTypesDeclaration(node) {
58-
if (node && node.type === 'ClassProperty') {
59-
// Flow support
60-
if (node.typeAnnotation && node.key.name === 'props') {
61-
return true;
62-
}
63-
node = node.key;
64-
}
65-
66-
return Boolean(
67-
node &&
68-
node.name === 'propTypes'
69-
);
70-
}
71-
7253
/**
7354
* Returns the prop key to ensure we handle the following cases:
7455
* propTypes: {
@@ -163,7 +144,7 @@ module.exports = {
163144

164145
return {
165146
ClassProperty: function(node) {
166-
if (!rule || !isPropTypesDeclaration(node)) {
147+
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
167148
return;
168149
}
169150
if (node.value && node.value.properties) {
@@ -175,7 +156,7 @@ module.exports = {
175156
},
176157

177158
MemberExpression: function(node) {
178-
if (!rule || !isPropTypesDeclaration(node.property)) {
159+
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
179160
return;
180161
}
181162
const component = utils.getRelatedComponent(node);
@@ -192,7 +173,7 @@ module.exports = {
192173

193174
// Search for the proptypes declaration
194175
node.properties.forEach(property => {
195-
if (!isPropTypesDeclaration(property.key)) {
176+
if (!propsUtil.isPropTypesDeclaration(property)) {
196177
return;
197178
}
198179
validatePropNaming(node, property.value.properties);

lib/rules/default-props-match-prop-types.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Components = require('../util/Components');
1010
const variableUtil = require('../util/variable');
1111
const annotations = require('../util/annotations');
1212
const astUtil = require('../util/ast');
13+
const propsUtil = require('../util/props');
1314

1415
// ------------------------------------------------------------------------------
1516
// Rule Definition
@@ -39,34 +40,6 @@ module.exports = {
3940
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
4041
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
4142

42-
/**
43-
* Checks if the Identifier node passed in looks like a propTypes declaration.
44-
* @param {ASTNode} node The node to check. Must be an Identifier node.
45-
* @returns {Boolean} `true` if the node is a propTypes declaration, `false` if not
46-
*/
47-
function isPropTypesDeclaration(node) {
48-
return astUtil.getPropertyName(node) === 'propTypes';
49-
}
50-
51-
/**
52-
* Checks if the Identifier node passed in looks like a defaultProps declaration.
53-
* @param {ASTNode} node The node to check. Must be an Identifier node.
54-
* @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not
55-
*/
56-
function isDefaultPropsDeclaration(node) {
57-
const propName = astUtil.getPropertyName(node);
58-
return (propName === 'defaultProps' || propName === 'getDefaultProps');
59-
}
60-
61-
/**
62-
* Checks if the PropTypes MemberExpression node passed in declares a required propType.
63-
* @param {ASTNode} propTypeExpression node to check. Must be a `PropTypes` MemberExpression.
64-
* @returns {Boolean} `true` if this PropType is required, `false` if not.
65-
*/
66-
function isRequiredPropType(propTypeExpression) {
67-
return propTypeExpression.type === 'MemberExpression' && propTypeExpression.property.name === 'isRequired';
68-
}
69-
7043
/**
7144
* Find a variable by name in the current scope.
7245
* @param {string} name Name of the variable to look for.
@@ -140,7 +113,7 @@ module.exports = {
140113

141114
return props.map(property => ({
142115
name: property.key.name,
143-
isRequired: isRequiredPropType(property.value),
116+
isRequired: propsUtil.isRequiredPropType(property.value),
144117
node: property
145118
}));
146119
}
@@ -373,8 +346,8 @@ module.exports = {
373346

374347
return {
375348
MemberExpression: function(node) {
376-
const isPropType = isPropTypesDeclaration(node);
377-
const isDefaultProp = isDefaultPropsDeclaration(node);
349+
const isPropType = propsUtil.isPropTypesDeclaration(node);
350+
const isDefaultProp = propsUtil.isDefaultPropsDeclaration(node);
378351

379352
if (!isPropType && !isDefaultProp) {
380353
return;
@@ -424,7 +397,7 @@ module.exports = {
424397
if (isPropType) {
425398
addPropTypesToComponent(component, [{
426399
name: node.parent.property.name,
427-
isRequired: isRequiredPropType(node.parent.parent.right),
400+
isRequired: propsUtil.isRequiredPropType(node.parent.parent.right),
428401
node: node.parent.parent
429402
}]);
430403
} else {
@@ -459,8 +432,8 @@ module.exports = {
459432
return;
460433
}
461434

462-
const isPropType = isPropTypesDeclaration(node);
463-
const isDefaultProp = isDefaultPropsDeclaration(node);
435+
const isPropType = propsUtil.isPropTypesDeclaration(node);
436+
const isDefaultProp = propsUtil.isDefaultPropsDeclaration(node);
464437

465438
if (!isPropType && !isDefaultProp) {
466439
return;
@@ -568,8 +541,8 @@ module.exports = {
568541
return;
569542
}
570543

571-
const isPropType = isPropTypesDeclaration(property);
572-
const isDefaultProp = isDefaultPropsDeclaration(property);
544+
const isPropType = propsUtil.isPropTypesDeclaration(property);
545+
const isDefaultProp = propsUtil.isDefaultPropsDeclaration(property);
573546

574547
if (!isPropType && !isDefaultProp) {
575548
return;

lib/rules/forbid-prop-types.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'use strict';
55

66
const variableUtil = require('../util/variable');
7+
const propsUtil = require('../util/props');
78

89
// ------------------------------------------------------------------------------
910
// Constants
@@ -47,22 +48,6 @@ module.exports = {
4748
return forbid.indexOf(type) >= 0;
4849
}
4950

50-
/**
51-
* Checks if node is `propTypes` declaration
52-
* @param {ASTNode} node The AST node being checked.
53-
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
54-
*/
55-
function isPropTypesDeclaration(node) {
56-
if (node.type === 'ClassProperty') {
57-
node = node.key;
58-
}
59-
60-
return Boolean(
61-
node &&
62-
node.name === 'propTypes'
63-
);
64-
}
65-
6651
/**
6752
* Find a variable by name in the current scope.
6853
* @param {string} name Name of the variable to look for.
@@ -147,14 +132,14 @@ module.exports = {
147132

148133
return {
149134
ClassProperty: function(node) {
150-
if (!isPropTypesDeclaration(node)) {
135+
if (!propsUtil.isPropTypesDeclaration(node)) {
151136
return;
152137
}
153138
checkNode(node.value);
154139
},
155140

156141
MemberExpression: function(node) {
157-
if (!isPropTypesDeclaration(node.property)) {
142+
if (!propsUtil.isPropTypesDeclaration(node)) {
158143
return;
159144
}
160145

@@ -167,7 +152,7 @@ module.exports = {
167152
return;
168153
}
169154

170-
if (!isPropTypesDeclaration(property.key)) {
155+
if (!propsUtil.isPropTypesDeclaration(property)) {
171156
return;
172157
}
173158
if (property.value.type === 'ObjectExpression') {

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Components = require('../util/Components');
1212
const variable = require('../util/variable');
1313
const annotations = require('../util/annotations');
1414
const versionUtil = require('../util/version');
15+
const propsUtil = require('../util/props');
1516

1617
// ------------------------------------------------------------------------------
1718
// Constants
@@ -176,22 +177,6 @@ module.exports = {
176177
return false;
177178
}
178179

179-
/**
180-
* Checks if we are declaring a prop
181-
* @param {ASTNode} node The AST node being checked.
182-
* @returns {Boolean} True if we are declaring a prop, false if not.
183-
*/
184-
function isPropTypesDeclaration(node) {
185-
if (node && node.type === 'ClassProperty') {
186-
node = node.key;
187-
}
188-
189-
return Boolean(
190-
node &&
191-
node.name === 'propTypes'
192-
);
193-
}
194-
195180
/**
196181
* Checks if prop should be validated by plugin-react-proptypes
197182
* @param {String} validator Name of validator to check.
@@ -936,7 +921,7 @@ module.exports = {
936921
ClassProperty: function(node) {
937922
if (isAnnotatedClassPropsDeclaration(node)) {
938923
markPropTypesAsDeclared(node, resolveTypeAnnotation(node));
939-
} else if (isPropTypesDeclaration(node)) {
924+
} else if (propsUtil.isPropTypesDeclaration(node)) {
940925
markPropTypesAsDeclared(node, node.value);
941926
}
942927
},
@@ -967,7 +952,7 @@ module.exports = {
967952
let type;
968953
if (isPropTypesUsage(node)) {
969954
type = 'usage';
970-
} else if (isPropTypesDeclaration(node.property)) {
955+
} else if (propsUtil.isPropTypesDeclaration(node)) {
971956
type = 'declaration';
972957
}
973958

@@ -995,7 +980,7 @@ module.exports = {
995980
},
996981

997982
MethodDefinition: function(node) {
998-
if (!isPropTypesDeclaration(node.key)) {
983+
if (!propsUtil.isPropTypesDeclaration(node)) {
999984
return;
1000985
}
1001986

@@ -1026,7 +1011,7 @@ module.exports = {
10261011
ObjectExpression: function(node) {
10271012
// Search for the proptypes declaration
10281013
node.properties.forEach(property => {
1029-
if (!isPropTypesDeclaration(property.key)) {
1014+
if (!propsUtil.isPropTypesDeclaration(property)) {
10301015
return;
10311016
}
10321017
markPropTypesAsDeclared(node, property.value);

lib/rules/prop-types.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Components = require('../util/Components');
1212
const variable = require('../util/variable');
1313
const annotations = require('../util/annotations');
1414
const versionUtil = require('../util/version');
15+
const propsUtil = require('../util/props');
1516

1617
// ------------------------------------------------------------------------------
1718
// Constants
@@ -182,22 +183,6 @@ module.exports = {
182183
return false;
183184
}
184185

185-
/**
186-
* Checks if we are declaring a prop
187-
* @param {ASTNode} node The AST node being checked.
188-
* @returns {Boolean} True if we are declaring a prop, false if not.
189-
*/
190-
function isPropTypesDeclaration(node) {
191-
if (node && node.type === 'ClassProperty') {
192-
node = node.key;
193-
}
194-
195-
return Boolean(
196-
node &&
197-
node.name === 'propTypes'
198-
);
199-
}
200-
201186
/**
202187
* Checks if the prop is ignored
203188
* @param {String} name Name of the prop to check.
@@ -969,7 +954,7 @@ module.exports = {
969954
ClassProperty: function(node) {
970955
if (isAnnotatedClassPropsDeclaration(node)) {
971956
markPropTypesAsDeclared(node, resolveTypeAnnotation(node));
972-
} else if (isPropTypesDeclaration(node)) {
957+
} else if (propsUtil.isPropTypesDeclaration(node)) {
973958
markPropTypesAsDeclared(node, node.value);
974959
}
975960
},
@@ -1006,7 +991,7 @@ module.exports = {
1006991
let type;
1007992
if (isPropTypesUsage(node)) {
1008993
type = 'usage';
1009-
} else if (isPropTypesDeclaration(node.property)) {
994+
} else if (propsUtil.isPropTypesDeclaration(node)) {
1010995
type = 'declaration';
1011996
}
1012997

@@ -1027,7 +1012,7 @@ module.exports = {
10271012
},
10281013

10291014
MethodDefinition: function(node) {
1030-
if (!node.static || node.kind !== 'get' || !isPropTypesDeclaration(node.key)) {
1015+
if (!node.static || node.kind !== 'get' || !propsUtil.isPropTypesDeclaration(node)) {
10311016
return;
10321017
}
10331018

@@ -1046,7 +1031,7 @@ module.exports = {
10461031
ObjectExpression: function(node) {
10471032
// Search for the proptypes declaration
10481033
node.properties.forEach(property => {
1049-
if (!isPropTypesDeclaration(property.key)) {
1034+
if (!propsUtil.isPropTypesDeclaration(property)) {
10501035
return;
10511036
}
10521037
markPropTypesAsDeclared(node, property.value);

0 commit comments

Comments
 (0)