From 3b6bacc18344204bd587f721fcf4ba71e41d530b Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 29 Jul 2024 22:26:08 -0700 Subject: [PATCH] [Refactor] general cleanup --- lib/rules/boolean-prop-naming.js | 20 ++-- lib/rules/display-name.js | 8 +- lib/rules/forbid-prop-types.js | 36 +++--- lib/rules/jsx-no-bind.js | 8 +- lib/rules/no-array-index-key.js | 32 +++--- lib/rules/no-find-dom-node.js | 7 +- lib/rules/no-set-state.js | 3 +- lib/rules/no-string-refs.js | 21 ++-- lib/rules/no-typos.js | 3 +- lib/rules/no-unknown-property.js | 10 +- lib/rules/no-unsafe.js | 9 +- lib/rules/no-unstable-nested-components.js | 2 +- lib/rules/no-unused-state.js | 22 ++-- lib/rules/prefer-stateless-function.js | 4 +- lib/rules/require-optimization.js | 5 +- lib/rules/sort-prop-types.js | 32 +++--- lib/util/Components.js | 45 +++++--- lib/util/ast.js | 128 +++++++++++---------- lib/util/componentUtil.js | 4 +- lib/util/isCreateContext.js | 51 ++++---- lib/util/isCreateElement.js | 11 +- lib/util/isDestructuredFromPragmaImport.js | 22 ++-- lib/util/isFirstLetterCapitalized.js | 6 +- lib/util/jsx.js | 2 +- lib/util/propTypes.js | 23 +++- lib/util/propTypesSort.js | 8 +- lib/util/props.js | 3 +- lib/util/usedPropTypes.js | 30 +++-- 28 files changed, 300 insertions(+), 255 deletions(-) diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index e856a17967..2c48b5a7bd 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -180,15 +180,17 @@ module.exports = { * @param {Function} addInvalidProp callback to run for each error */ function runCheck(proptypes, addInvalidProp) { - (proptypes || []).forEach((prop) => { - if (config.validateNested && nestedPropTypes(prop)) { - runCheck(prop.value.arguments[0].properties, addInvalidProp); - return; - } - if (flowCheck(prop) || regularCheck(prop) || tsCheck(prop)) { - addInvalidProp(prop); - } - }); + if (proptypes) { + proptypes.forEach((prop) => { + if (config.validateNested && nestedPropTypes(prop)) { + runCheck(prop.value.arguments[0].properties, addInvalidProp); + return; + } + if (flowCheck(prop) || regularCheck(prop) || tsCheck(prop)) { + addInvalidProp(prop); + } + }); + } } /** diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 5e5b8b6980..027882a461 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -76,9 +76,11 @@ module.exports = { * @returns {boolean} True if React.forwardRef is nested inside React.memo, false if not. */ function isNestedMemo(node) { - const argumentIsCallExpression = node.arguments && node.arguments[0] && node.arguments[0].type === 'CallExpression'; - - return node.type === 'CallExpression' && argumentIsCallExpression && utils.isPragmaComponentWrapper(node); + return node.type === 'CallExpression' + && node.arguments + && node.arguments[0] + && node.arguments[0].type === 'CallExpression' + && utils.isPragmaComponentWrapper(node); } /** diff --git a/lib/rules/forbid-prop-types.js b/lib/rules/forbid-prop-types.js index b561b26da1..581c8969b9 100644 --- a/lib/rules/forbid-prop-types.js +++ b/lib/rules/forbid-prop-types.js @@ -158,29 +158,25 @@ module.exports = { } function checkNode(node) { - switch (node && node.type) { - case 'ObjectExpression': - checkProperties(node.properties); - break; - case 'Identifier': { - const propTypesObject = variableUtil.findVariableByName(context, node, node.name); - if (propTypesObject && propTypesObject.properties) { - checkProperties(propTypesObject.properties); - } - break; + if (!node) { + return; + } + + if (node.type === 'ObjectExpression') { + checkProperties(node.properties); + } else if (node.type === 'Identifier') { + const propTypesObject = variableUtil.findVariableByName(context, node, node.name); + if (propTypesObject && propTypesObject.properties) { + checkProperties(propTypesObject.properties); } - case 'CallExpression': { - const innerNode = node.arguments && node.arguments[0]; - if ( - propWrapperUtil.isPropWrapperFunction(context, getText(context, node.callee)) + } else if (node.type === 'CallExpression') { + const innerNode = node.arguments && node.arguments[0]; + if ( + propWrapperUtil.isPropWrapperFunction(context, getText(context, node.callee)) && innerNode - ) { - checkNode(innerNode); - } - break; + ) { + checkNode(innerNode); } - default: - break; } } diff --git a/lib/rules/jsx-no-bind.js b/lib/rules/jsx-no-bind.js index 4d6e349d25..588ff0b03f 100644 --- a/lib/rules/jsx-no-bind.js +++ b/lib/rules/jsx-no-bind.js @@ -93,21 +93,21 @@ module.exports = { ) { return 'bindCall'; } - if (nodeType === 'ConditionalExpression') { + if (node.type === 'ConditionalExpression') { return getNodeViolationType(node.test) || getNodeViolationType(node.consequent) || getNodeViolationType(node.alternate); } - if (!configuration.allowArrowFunctions && nodeType === 'ArrowFunctionExpression') { + if (!configuration.allowArrowFunctions && node.type === 'ArrowFunctionExpression') { return 'arrowFunc'; } if ( !configuration.allowFunctions - && (nodeType === 'FunctionExpression' || nodeType === 'FunctionDeclaration') + && (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') ) { return 'func'; } - if (!configuration.allowBind && nodeType === 'BindExpression') { + if (!configuration.allowBind && node.type === 'BindExpression') { return 'bindExpression'; } diff --git a/lib/rules/no-array-index-key.js b/lib/rules/no-array-index-key.js index 2a0ae41e6f..ffd5c927b4 100644 --- a/lib/rules/no-array-index-key.js +++ b/lib/rules/no-array-index-key.js @@ -189,14 +189,15 @@ module.exports = { return; } - if (node.type === 'CallExpression' - && node.callee - && node.callee.type === 'MemberExpression' - && node.callee.object - && isArrayIndex(node.callee.object) - && node.callee.property - && node.callee.property.type === 'Identifier' - && node.callee.property.name === 'toString' + if ( + node.type === 'CallExpression' + && node.callee + && node.callee.type === 'MemberExpression' + && node.callee.object + && isArrayIndex(node.callee.object) + && node.callee.property + && node.callee.property.type === 'Identifier' + && node.callee.property.name === 'toString' ) { // key={bar.toString()} report(context, messages.noArrayIndex, 'noArrayIndex', { @@ -205,13 +206,14 @@ module.exports = { return; } - if (node.type === 'CallExpression' - && node.callee - && node.callee.type === 'Identifier' - && node.callee.name === 'String' - && Array.isArray(node.arguments) - && node.arguments.length > 0 - && isArrayIndex(node.arguments[0]) + if ( + node.type === 'CallExpression' + && node.callee + && node.callee.type === 'Identifier' + && node.callee.name === 'String' + && Array.isArray(node.arguments) + && node.arguments.length > 0 + && isArrayIndex(node.arguments[0]) ) { // key={String(bar)} report(context, messages.noArrayIndex, 'noArrayIndex', { diff --git a/lib/rules/no-find-dom-node.js b/lib/rules/no-find-dom-node.js index 0dae59da43..3d9ef52ef8 100644 --- a/lib/rules/no-find-dom-node.js +++ b/lib/rules/no-find-dom-node.js @@ -35,8 +35,11 @@ module.exports = { CallExpression(node) { const callee = node.callee; - const isfindDOMNode = (callee.name === 'findDOMNode') - || (callee.property && callee.property.name === 'findDOMNode'); + const isfindDOMNode = callee.name === 'findDOMNode' || ( + callee.property + && callee.property.name === 'findDOMNode' + ); + if (!isfindDOMNode) { return; } diff --git a/lib/rules/no-set-state.js b/lib/rules/no-set-state.js index 0866552141..44967bf03c 100644 --- a/lib/rules/no-set-state.js +++ b/lib/rules/no-set-state.js @@ -49,9 +49,8 @@ module.exports = { * @param {Object} component The component to process */ function reportSetStateUsages(component) { - let setStateUsage; for (let i = 0, j = component.setStateUsages.length; i < j; i++) { - setStateUsage = component.setStateUsages[i]; + const setStateUsage = component.setStateUsages[i]; report(context, messages.noSetState, 'noSetState', { node: setStateUsage, }); diff --git a/lib/rules/no-string-refs.js b/lib/rules/no-string-refs.js index c5c3b29166..bef6440441 100644 --- a/lib/rules/no-string-refs.js +++ b/lib/rules/no-string-refs.js @@ -62,11 +62,9 @@ module.exports = { * @returns {boolean} True if we are using a ref attribute, false if not. */ function isRefAttribute(node) { - return !!( - node.type === 'JSXAttribute' - && node.name - && node.name.name === 'ref' - ); + return node.type === 'JSXAttribute' + && !!node.name + && node.name.name === 'ref'; } /** @@ -75,11 +73,9 @@ module.exports = { * @returns {boolean} True if the node contains a string value, false if not. */ function containsStringLiteral(node) { - return !!( - node.value + return !!node.value && node.value.type === 'Literal' - && typeof node.value.value === 'string' - ); + && typeof node.value.value === 'string'; } /** @@ -88,13 +84,11 @@ module.exports = { * @returns {boolean} True if the node contains a string value within a jsx expression, false if not. */ function containsStringExpressionContainer(node) { - return !!( - node.value + return !!node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression && ((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string') - || (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals)) - ); + || (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals)); } return { @@ -105,6 +99,7 @@ module.exports = { }); } }, + JSXAttribute(node) { if ( isRefAttribute(node) diff --git a/lib/rules/no-typos.js b/lib/rules/no-typos.js index 3d79d40b18..d60d2e7cf2 100644 --- a/lib/rules/no-typos.js +++ b/lib/rules/no-typos.js @@ -194,7 +194,8 @@ module.exports = { } if (node.specifiers.length >= 1) { const propTypesSpecifier = node.specifiers.find((specifier) => ( - specifier.imported && specifier.imported.name === 'PropTypes' + specifier.imported + && specifier.imported.name === 'PropTypes' )); if (propTypesSpecifier) { propTypesPackageName = propTypesSpecifier.local.name; diff --git a/lib/rules/no-unknown-property.js b/lib/rules/no-unknown-property.js index 61ca8ad809..50134264d1 100644 --- a/lib/rules/no-unknown-property.js +++ b/lib/rules/no-unknown-property.js @@ -463,7 +463,11 @@ function isValidAriaAttribute(name) { * @returns {string | null} tag name */ function getTagName(node) { - if (node && node.parent && node.parent.name && node.parent.name) { + if ( + node + && node.parent + && node.parent.name + ) { return node.parent.name.name; } return null; @@ -592,7 +596,9 @@ module.exports = { // Let's dive deeper into tags that are HTML/DOM elements (`