Skip to content

Commit 3b6bacc

Browse files
committed
[Refactor] general cleanup
1 parent 8dc0215 commit 3b6bacc

28 files changed

+300
-255
lines changed

lib/rules/boolean-prop-naming.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,17 @@ module.exports = {
180180
* @param {Function} addInvalidProp callback to run for each error
181181
*/
182182
function runCheck(proptypes, addInvalidProp) {
183-
(proptypes || []).forEach((prop) => {
184-
if (config.validateNested && nestedPropTypes(prop)) {
185-
runCheck(prop.value.arguments[0].properties, addInvalidProp);
186-
return;
187-
}
188-
if (flowCheck(prop) || regularCheck(prop) || tsCheck(prop)) {
189-
addInvalidProp(prop);
190-
}
191-
});
183+
if (proptypes) {
184+
proptypes.forEach((prop) => {
185+
if (config.validateNested && nestedPropTypes(prop)) {
186+
runCheck(prop.value.arguments[0].properties, addInvalidProp);
187+
return;
188+
}
189+
if (flowCheck(prop) || regularCheck(prop) || tsCheck(prop)) {
190+
addInvalidProp(prop);
191+
}
192+
});
193+
}
192194
}
193195

194196
/**

lib/rules/display-name.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ module.exports = {
7676
* @returns {boolean} True if React.forwardRef is nested inside React.memo, false if not.
7777
*/
7878
function isNestedMemo(node) {
79-
const argumentIsCallExpression = node.arguments && node.arguments[0] && node.arguments[0].type === 'CallExpression';
80-
81-
return node.type === 'CallExpression' && argumentIsCallExpression && utils.isPragmaComponentWrapper(node);
79+
return node.type === 'CallExpression'
80+
&& node.arguments
81+
&& node.arguments[0]
82+
&& node.arguments[0].type === 'CallExpression'
83+
&& utils.isPragmaComponentWrapper(node);
8284
}
8385

8486
/**

lib/rules/forbid-prop-types.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -158,29 +158,25 @@ module.exports = {
158158
}
159159

160160
function checkNode(node) {
161-
switch (node && node.type) {
162-
case 'ObjectExpression':
163-
checkProperties(node.properties);
164-
break;
165-
case 'Identifier': {
166-
const propTypesObject = variableUtil.findVariableByName(context, node, node.name);
167-
if (propTypesObject && propTypesObject.properties) {
168-
checkProperties(propTypesObject.properties);
169-
}
170-
break;
161+
if (!node) {
162+
return;
163+
}
164+
165+
if (node.type === 'ObjectExpression') {
166+
checkProperties(node.properties);
167+
} else if (node.type === 'Identifier') {
168+
const propTypesObject = variableUtil.findVariableByName(context, node, node.name);
169+
if (propTypesObject && propTypesObject.properties) {
170+
checkProperties(propTypesObject.properties);
171171
}
172-
case 'CallExpression': {
173-
const innerNode = node.arguments && node.arguments[0];
174-
if (
175-
propWrapperUtil.isPropWrapperFunction(context, getText(context, node.callee))
172+
} else if (node.type === 'CallExpression') {
173+
const innerNode = node.arguments && node.arguments[0];
174+
if (
175+
propWrapperUtil.isPropWrapperFunction(context, getText(context, node.callee))
176176
&& innerNode
177-
) {
178-
checkNode(innerNode);
179-
}
180-
break;
177+
) {
178+
checkNode(innerNode);
181179
}
182-
default:
183-
break;
184180
}
185181
}
186182

lib/rules/jsx-no-bind.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@ module.exports = {
9393
) {
9494
return 'bindCall';
9595
}
96-
if (nodeType === 'ConditionalExpression') {
96+
if (node.type === 'ConditionalExpression') {
9797
return getNodeViolationType(node.test)
9898
|| getNodeViolationType(node.consequent)
9999
|| getNodeViolationType(node.alternate);
100100
}
101-
if (!configuration.allowArrowFunctions && nodeType === 'ArrowFunctionExpression') {
101+
if (!configuration.allowArrowFunctions && node.type === 'ArrowFunctionExpression') {
102102
return 'arrowFunc';
103103
}
104104
if (
105105
!configuration.allowFunctions
106-
&& (nodeType === 'FunctionExpression' || nodeType === 'FunctionDeclaration')
106+
&& (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration')
107107
) {
108108
return 'func';
109109
}
110-
if (!configuration.allowBind && nodeType === 'BindExpression') {
110+
if (!configuration.allowBind && node.type === 'BindExpression') {
111111
return 'bindExpression';
112112
}
113113

lib/rules/no-array-index-key.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,15 @@ module.exports = {
189189
return;
190190
}
191191

192-
if (node.type === 'CallExpression'
193-
&& node.callee
194-
&& node.callee.type === 'MemberExpression'
195-
&& node.callee.object
196-
&& isArrayIndex(node.callee.object)
197-
&& node.callee.property
198-
&& node.callee.property.type === 'Identifier'
199-
&& node.callee.property.name === 'toString'
192+
if (
193+
node.type === 'CallExpression'
194+
&& node.callee
195+
&& node.callee.type === 'MemberExpression'
196+
&& node.callee.object
197+
&& isArrayIndex(node.callee.object)
198+
&& node.callee.property
199+
&& node.callee.property.type === 'Identifier'
200+
&& node.callee.property.name === 'toString'
200201
) {
201202
// key={bar.toString()}
202203
report(context, messages.noArrayIndex, 'noArrayIndex', {
@@ -205,13 +206,14 @@ module.exports = {
205206
return;
206207
}
207208

208-
if (node.type === 'CallExpression'
209-
&& node.callee
210-
&& node.callee.type === 'Identifier'
211-
&& node.callee.name === 'String'
212-
&& Array.isArray(node.arguments)
213-
&& node.arguments.length > 0
214-
&& isArrayIndex(node.arguments[0])
209+
if (
210+
node.type === 'CallExpression'
211+
&& node.callee
212+
&& node.callee.type === 'Identifier'
213+
&& node.callee.name === 'String'
214+
&& Array.isArray(node.arguments)
215+
&& node.arguments.length > 0
216+
&& isArrayIndex(node.arguments[0])
215217
) {
216218
// key={String(bar)}
217219
report(context, messages.noArrayIndex, 'noArrayIndex', {

lib/rules/no-find-dom-node.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ module.exports = {
3535
CallExpression(node) {
3636
const callee = node.callee;
3737

38-
const isfindDOMNode = (callee.name === 'findDOMNode')
39-
|| (callee.property && callee.property.name === 'findDOMNode');
38+
const isfindDOMNode = callee.name === 'findDOMNode' || (
39+
callee.property
40+
&& callee.property.name === 'findDOMNode'
41+
);
42+
4043
if (!isfindDOMNode) {
4144
return;
4245
}

lib/rules/no-set-state.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ module.exports = {
4949
* @param {Object} component The component to process
5050
*/
5151
function reportSetStateUsages(component) {
52-
let setStateUsage;
5352
for (let i = 0, j = component.setStateUsages.length; i < j; i++) {
54-
setStateUsage = component.setStateUsages[i];
53+
const setStateUsage = component.setStateUsages[i];
5554
report(context, messages.noSetState, 'noSetState', {
5655
node: setStateUsage,
5756
});

lib/rules/no-string-refs.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ module.exports = {
6262
* @returns {boolean} True if we are using a ref attribute, false if not.
6363
*/
6464
function isRefAttribute(node) {
65-
return !!(
66-
node.type === 'JSXAttribute'
67-
&& node.name
68-
&& node.name.name === 'ref'
69-
);
65+
return node.type === 'JSXAttribute'
66+
&& !!node.name
67+
&& node.name.name === 'ref';
7068
}
7169

7270
/**
@@ -75,11 +73,9 @@ module.exports = {
7573
* @returns {boolean} True if the node contains a string value, false if not.
7674
*/
7775
function containsStringLiteral(node) {
78-
return !!(
79-
node.value
76+
return !!node.value
8077
&& node.value.type === 'Literal'
81-
&& typeof node.value.value === 'string'
82-
);
78+
&& typeof node.value.value === 'string';
8379
}
8480

8581
/**
@@ -88,13 +84,11 @@ module.exports = {
8884
* @returns {boolean} True if the node contains a string value within a jsx expression, false if not.
8985
*/
9086
function containsStringExpressionContainer(node) {
91-
return !!(
92-
node.value
87+
return !!node.value
9388
&& node.value.type === 'JSXExpressionContainer'
9489
&& node.value.expression
9590
&& ((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string')
96-
|| (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals))
97-
);
91+
|| (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals));
9892
}
9993

10094
return {
@@ -105,6 +99,7 @@ module.exports = {
10599
});
106100
}
107101
},
102+
108103
JSXAttribute(node) {
109104
if (
110105
isRefAttribute(node)

lib/rules/no-typos.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ module.exports = {
194194
}
195195
if (node.specifiers.length >= 1) {
196196
const propTypesSpecifier = node.specifiers.find((specifier) => (
197-
specifier.imported && specifier.imported.name === 'PropTypes'
197+
specifier.imported
198+
&& specifier.imported.name === 'PropTypes'
198199
));
199200
if (propTypesSpecifier) {
200201
propTypesPackageName = propTypesSpecifier.local.name;

lib/rules/no-unknown-property.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ function isValidAriaAttribute(name) {
463463
* @returns {string | null} tag name
464464
*/
465465
function getTagName(node) {
466-
if (node && node.parent && node.parent.name && node.parent.name) {
466+
if (
467+
node
468+
&& node.parent
469+
&& node.parent.name
470+
) {
467471
return node.parent.name.name;
468472
}
469473
return null;
@@ -592,7 +596,9 @@ module.exports = {
592596
// Let's dive deeper into tags that are HTML/DOM elements (`<button>`), and not React components (`<Button />`)
593597

594598
// Some attributes are allowed on some tags only
595-
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name) ? ATTRIBUTE_TAGS_MAP[/** @type {keyof ATTRIBUTE_TAGS_MAP} */ (name)] : null;
599+
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name)
600+
? ATTRIBUTE_TAGS_MAP[/** @type {keyof ATTRIBUTE_TAGS_MAP} */ (name)]
601+
: null;
596602
if (tagName && allowedTags) {
597603
// Scenario 1A: Allowed attribute found where not supposed to, report it
598604
if (allowedTags.indexOf(tagName) === -1) {

lib/rules/no-unsafe.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,15 @@ module.exports = {
5757
const unsafe = {
5858
UNSAFE_componentWillMount: {
5959
newMethod: 'componentDidMount',
60-
details:
61-
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
60+
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
6261
},
6362
UNSAFE_componentWillReceiveProps: {
6463
newMethod: 'getDerivedStateFromProps',
65-
details:
66-
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
64+
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
6765
},
6866
UNSAFE_componentWillUpdate: {
6967
newMethod: 'componentDidUpdate',
70-
details:
71-
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
68+
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
7269
},
7370
};
7471
if (checkAliases) {

lib/rules/no-unstable-nested-components.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function generateErrorMessageWithParentName(parentName) {
3636
* @returns {boolean}
3737
*/
3838
function startsWithRender(text) {
39-
return (text || '').startsWith('render');
39+
return typeof text === 'string' && text.startsWith('render');
4040
}
4141

4242
/**

lib/rules/no-unused-state.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const docsUrl = require('../util/docsUrl');
13-
const ast = require('../util/ast');
13+
const astUtil = require('../util/ast');
1414
const componentUtil = require('../util/componentUtil');
1515
const report = require('../util/report');
1616
const getScope = require('../util/eslint').getScope;
@@ -44,7 +44,7 @@ function getName(node) {
4444
}
4545

4646
function isThisExpression(node) {
47-
return ast.unwrapTSAsExpression(uncast(node)).type === 'ThisExpression';
47+
return astUtil.unwrapTSAsExpression(uncast(node)).type === 'ThisExpression';
4848
}
4949

5050
function getInitialClassInfo() {
@@ -65,7 +65,7 @@ function getInitialClassInfo() {
6565
}
6666

6767
function isSetStateCall(node) {
68-
const unwrappedCalleeNode = ast.unwrapTSAsExpression(node.callee);
68+
const unwrappedCalleeNode = astUtil.unwrapTSAsExpression(node.callee);
6969

7070
return (
7171
unwrappedCalleeNode.type === 'MemberExpression'
@@ -189,7 +189,7 @@ module.exports = {
189189
// Used to record used state fields and new aliases for both
190190
// AssignmentExpressions and VariableDeclarators.
191191
function handleAssignment(left, right) {
192-
const unwrappedRight = ast.unwrapTSAsExpression(right);
192+
const unwrappedRight = astUtil.unwrapTSAsExpression(right);
193193

194194
switch (left.type) {
195195
case 'Identifier':
@@ -292,8 +292,8 @@ module.exports = {
292292
return;
293293
}
294294

295-
const unwrappedNode = ast.unwrapTSAsExpression(node);
296-
const unwrappedArgumentNode = ast.unwrapTSAsExpression(unwrappedNode.arguments[0]);
295+
const unwrappedNode = astUtil.unwrapTSAsExpression(node);
296+
const unwrappedArgumentNode = astUtil.unwrapTSAsExpression(unwrappedNode.arguments[0]);
297297

298298
// If we're looking at a `this.setState({})` invocation, record all the
299299
// properties as state fields.
@@ -308,7 +308,7 @@ module.exports = {
308308
&& unwrappedNode.arguments.length > 0
309309
&& unwrappedArgumentNode.type === 'ArrowFunctionExpression'
310310
) {
311-
const unwrappedBodyNode = ast.unwrapTSAsExpression(unwrappedArgumentNode.body);
311+
const unwrappedBodyNode = astUtil.unwrapTSAsExpression(unwrappedArgumentNode.body);
312312

313313
if (unwrappedBodyNode.type === 'ObjectExpression') {
314314
addStateFields(unwrappedBodyNode);
@@ -330,7 +330,7 @@ module.exports = {
330330
}
331331
// If we see state being assigned as a class property using an object
332332
// expression, record all the fields of that object as state fields.
333-
const unwrappedValueNode = ast.unwrapTSAsExpression(node.value);
333+
const unwrappedValueNode = astUtil.unwrapTSAsExpression(node.value);
334334

335335
const name = getName(node.key);
336336
if (
@@ -452,8 +452,8 @@ module.exports = {
452452
return;
453453
}
454454

455-
const unwrappedLeft = ast.unwrapTSAsExpression(node.left);
456-
const unwrappedRight = ast.unwrapTSAsExpression(node.right);
455+
const unwrappedLeft = astUtil.unwrapTSAsExpression(node.left);
456+
const unwrappedRight = astUtil.unwrapTSAsExpression(node.right);
457457

458458
// Check for assignments like `this.state = {}`
459459
if (
@@ -493,7 +493,7 @@ module.exports = {
493493
if (!classInfo) {
494494
return;
495495
}
496-
if (isStateReference(ast.unwrapTSAsExpression(node.object))) {
496+
if (isStateReference(astUtil.unwrapTSAsExpression(node.object))) {
497497
// If we see this.state[foo] access, give up.
498498
if (node.computed && node.property.type !== 'Literal') {
499499
classInfo = null;

lib/rules/prefer-stateless-function.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ module.exports = {
354354
}
355355
scope = scope.upper;
356356
}
357-
const isRender = blockNode && blockNode.key && blockNode.key.name === 'render';
357+
const isRender = blockNode
358+
&& blockNode.key
359+
&& blockNode.key.name === 'render';
358360
const allowNull = testReactVersion(context, '>= 15.0.0'); // Stateless components can return null since React 15
359361
const isReturningJSX = utils.isReturningJSX(node, !allowNull);
360362
const isReturningNull = node.argument && (node.argument.value === null || node.argument.value === false);

0 commit comments

Comments
 (0)