Skip to content

Commit dea04b6

Browse files
authored
Merge pull request jsx-eslint#1526 from jomasti/clean-up
Code clean up
2 parents 8795fde + 54b738d commit dea04b6

14 files changed

+150
-393
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 4 additions & 29 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,32 +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-
// Special case for class properties
59-
// (babel-eslint does not expose property name so we have to rely on tokens)
60-
if (node && node.type === 'ClassProperty') {
61-
const tokens = context.getFirstTokens(node, 2);
62-
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
63-
return true;
64-
}
65-
// Flow support
66-
if (node.typeAnnotation && node.key.name === 'props') {
67-
return true;
68-
}
69-
return false;
70-
}
71-
72-
return Boolean(
73-
node &&
74-
node.name === 'propTypes'
75-
);
76-
}
77-
7853
/**
7954
* Returns the prop key to ensure we handle the following cases:
8055
* propTypes: {
@@ -169,7 +144,7 @@ module.exports = {
169144

170145
return {
171146
ClassProperty: function(node) {
172-
if (!rule || !isPropTypesDeclaration(node)) {
147+
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
173148
return;
174149
}
175150
if (node.value && node.value.properties) {
@@ -181,7 +156,7 @@ module.exports = {
181156
},
182157

183158
MemberExpression: function(node) {
184-
if (!rule || !isPropTypesDeclaration(node.property)) {
159+
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
185160
return;
186161
}
187162
const component = utils.getRelatedComponent(node);
@@ -198,7 +173,7 @@ module.exports = {
198173

199174
// Search for the proptypes declaration
200175
node.properties.forEach(property => {
201-
if (!isPropTypesDeclaration(property.key)) {
176+
if (!propsUtil.isPropTypesDeclaration(property)) {
202177
return;
203178
}
204179
validatePropNaming(node, property.value.properties);

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

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const has = require('has');
99
const Components = require('../util/Components');
1010
const variableUtil = require('../util/variable');
1111
const annotations = require('../util/annotations');
12+
const astUtil = require('../util/ast');
13+
const propsUtil = require('../util/props');
1214

1315
// ------------------------------------------------------------------------------
1416
// Rule Definition
@@ -38,53 +40,6 @@ module.exports = {
3840
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
3941
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
4042

41-
/**
42-
* Get properties name
43-
* @param {Object} node - Property.
44-
* @returns {String} Property name.
45-
*/
46-
function getPropertyName(node) {
47-
if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
48-
return node.key.name;
49-
} else if (node.type === 'MemberExpression') {
50-
return node.property.name;
51-
// Special case for class properties
52-
// (babel-eslint@5 does not expose property name so we have to rely on tokens)
53-
} else if (node.type === 'ClassProperty') {
54-
const tokens = context.getFirstTokens(node, 2);
55-
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
56-
}
57-
return '';
58-
}
59-
60-
/**
61-
* Checks if the Identifier node passed in looks like a propTypes declaration.
62-
* @param {ASTNode} node The node to check. Must be an Identifier node.
63-
* @returns {Boolean} `true` if the node is a propTypes declaration, `false` if not
64-
*/
65-
function isPropTypesDeclaration(node) {
66-
return getPropertyName(node) === 'propTypes';
67-
}
68-
69-
/**
70-
* Checks if the Identifier node passed in looks like a defaultProps declaration.
71-
* @param {ASTNode} node The node to check. Must be an Identifier node.
72-
* @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not
73-
*/
74-
function isDefaultPropsDeclaration(node) {
75-
const propName = getPropertyName(node);
76-
return (propName === 'defaultProps' || propName === 'getDefaultProps');
77-
}
78-
79-
/**
80-
* Checks if the PropTypes MemberExpression node passed in declares a required propType.
81-
* @param {ASTNode} propTypeExpression node to check. Must be a `PropTypes` MemberExpression.
82-
* @returns {Boolean} `true` if this PropType is required, `false` if not.
83-
*/
84-
function isRequiredPropType(propTypeExpression) {
85-
return propTypeExpression.type === 'MemberExpression' && propTypeExpression.property.name === 'isRequired';
86-
}
87-
8843
/**
8944
* Find a variable by name in the current scope.
9045
* @param {string} name Name of the variable to look for.
@@ -158,7 +113,7 @@ module.exports = {
158113

159114
return props.map(property => ({
160115
name: property.key.name,
161-
isRequired: isRequiredPropType(property.value),
116+
isRequired: propsUtil.isRequiredPropType(property.value),
162117
node: property
163118
}));
164119
}
@@ -345,7 +300,7 @@ module.exports = {
345300
}
346301

347302
function isPropTypeAnnotation(node) {
348-
return (getPropertyName(node) === 'props' && !!node.typeAnnotation);
303+
return (astUtil.getPropertyName(node) === 'props' && !!node.typeAnnotation);
349304
}
350305

351306
function propFromName(propTypes, name) {
@@ -395,8 +350,8 @@ module.exports = {
395350

396351
return {
397352
MemberExpression: function(node) {
398-
const isPropType = isPropTypesDeclaration(node);
399-
const isDefaultProp = isDefaultPropsDeclaration(node);
353+
const isPropType = propsUtil.isPropTypesDeclaration(node);
354+
const isDefaultProp = propsUtil.isDefaultPropsDeclaration(node);
400355

401356
if (!isPropType && !isDefaultProp) {
402357
return;
@@ -446,7 +401,7 @@ module.exports = {
446401
if (isPropType) {
447402
addPropTypesToComponent(component, [{
448403
name: node.parent.property.name,
449-
isRequired: isRequiredPropType(node.parent.parent.right),
404+
isRequired: propsUtil.isRequiredPropType(node.parent.parent.right),
450405
node: node.parent.parent
451406
}]);
452407
} else {
@@ -481,8 +436,8 @@ module.exports = {
481436
return;
482437
}
483438

484-
const isPropType = isPropTypesDeclaration(node);
485-
const isDefaultProp = isDefaultPropsDeclaration(node);
439+
const isPropType = propsUtil.isPropTypesDeclaration(node);
440+
const isDefaultProp = propsUtil.isDefaultPropsDeclaration(node);
486441

487442
if (!isPropType && !isDefaultProp) {
488443
return;
@@ -537,7 +492,7 @@ module.exports = {
537492
return;
538493
}
539494

540-
const propName = getPropertyName(node);
495+
const propName = astUtil.getPropertyName(node);
541496
const isPropType = propName === 'propTypes';
542497
const isDefaultProp = propName === 'defaultProps' || propName === 'getDefaultProps';
543498

@@ -590,8 +545,8 @@ module.exports = {
590545
return;
591546
}
592547

593-
const isPropType = isPropTypesDeclaration(property);
594-
const isDefaultProp = isDefaultPropsDeclaration(property);
548+
const isPropType = propsUtil.isPropTypesDeclaration(property);
549+
const isDefaultProp = propsUtil.isDefaultPropsDeclaration(property);
595550

596551
if (!isPropType && !isDefaultProp) {
597552
return;

lib/rules/display-name.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ module.exports = {
3131
},
3232

3333
create: Components.detect((context, components, utils) => {
34-
const sourceCode = context.getSourceCode();
3534
const config = context.options[0] || {};
3635
const ignoreTranspilerName = config.ignoreTranspilerName || false;
3736

@@ -44,17 +43,8 @@ module.exports = {
4443
*/
4544
function isDisplayNameDeclaration(node) {
4645
switch (node.type) {
47-
// Special case for class properties
48-
// (babel-eslint does not expose property name so we have to rely on tokens)
4946
case 'ClassProperty':
50-
const tokens = sourceCode.getFirstTokens(node, 2);
51-
if (
52-
tokens[0].value === 'displayName' ||
53-
(tokens[1] && tokens[1].value === 'displayName')
54-
) {
55-
return true;
56-
}
57-
return false;
47+
return node.key && node.key.name === 'displayName';
5848
case 'Identifier':
5949
return node.name === 'displayName';
6050
case 'Literal':

lib/rules/forbid-prop-types.js

Lines changed: 4 additions & 25 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,28 +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-
// Special case for class properties
57-
// (babel-eslint does not expose property name so we have to rely on tokens)
58-
if (node.type === 'ClassProperty') {
59-
const tokens = context.getFirstTokens(node, 2);
60-
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
61-
return true;
62-
}
63-
return false;
64-
}
65-
66-
return Boolean(
67-
node &&
68-
node.name === 'propTypes'
69-
);
70-
}
71-
7251
/**
7352
* Find a variable by name in the current scope.
7453
* @param {string} name Name of the variable to look for.
@@ -153,14 +132,14 @@ module.exports = {
153132

154133
return {
155134
ClassProperty: function(node) {
156-
if (!isPropTypesDeclaration(node)) {
135+
if (!propsUtil.isPropTypesDeclaration(node)) {
157136
return;
158137
}
159138
checkNode(node.value);
160139
},
161140

162141
MemberExpression: function(node) {
163-
if (!isPropTypesDeclaration(node.property)) {
142+
if (!propsUtil.isPropTypesDeclaration(node)) {
164143
return;
165144
}
166145

@@ -173,7 +152,7 @@ module.exports = {
173152
return;
174153
}
175154

176-
if (!isPropTypesDeclaration(property.key)) {
155+
if (!propsUtil.isPropTypesDeclaration(property)) {
177156
return;
178157
}
179158
if (property.value.type === 'ObjectExpression') {

lib/rules/no-redundant-should-component-update.js

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

66
const Components = require('../util/Components');
7+
const astUtil = require('../util/ast');
78

89
function errorMessage(node) {
910
return `${node} does not need shouldComponentUpdate when extending React.PureComponent.`;
@@ -24,47 +25,15 @@ module.exports = {
2425
},
2526

2627
create: Components.detect((context, components, utils) => {
27-
/**
28-
* Get properties name
29-
* @param {Object} node - Property.
30-
* @returns {String} Property name.
31-
*/
32-
function getPropertyName(node) {
33-
if (node.key) {
34-
return node.key.name;
35-
} else if (node.type === 'ClassProperty') {
36-
// Special case for class properties
37-
// (babel-eslint does not expose property name so we have to rely on tokens)
38-
const tokens = context.getFirstTokens(node, 2);
39-
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
40-
}
41-
return '';
42-
}
43-
44-
/**
45-
* Get properties for a given AST node
46-
* @param {ASTNode} node The AST node being checked.
47-
* @returns {Array} Properties array.
48-
*/
49-
function getComponentProperties(node) {
50-
switch (node.type) {
51-
case 'ClassExpression':
52-
case 'ClassDeclaration':
53-
return node.body.body;
54-
default:
55-
return [];
56-
}
57-
}
58-
5928
/**
6029
* Checks for shouldComponentUpdate property
6130
* @param {ASTNode} node The AST node being checked.
6231
* @returns {Boolean} Whether or not the property exists.
6332
*/
6433
function hasShouldComponentUpdate(node) {
65-
const properties = getComponentProperties(node);
34+
const properties = astUtil.getComponentProperties(node);
6635
return properties.some(property => {
67-
const name = getPropertyName(property);
36+
const name = astUtil.getPropertyName(property);
6837
return name === 'shouldComponentUpdate';
6938
});
7039
}

0 commit comments

Comments
 (0)