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

Lines changed: 11 additions & 9 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 16 additions & 20 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 17 additions & 15 deletions
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 8 additions & 13 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 8 additions & 2 deletions
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) {

0 commit comments

Comments
 (0)