Skip to content

Commit

Permalink
[Refactor] general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 30, 2024
1 parent 0f89918 commit bfa325a
Show file tree
Hide file tree
Showing 28 changed files with 314 additions and 265 deletions.
44 changes: 25 additions & 19 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}

/**
Expand All @@ -197,16 +199,20 @@ module.exports = {
* @param {Array} proptypes A list of Property object (for each proptype defined)
*/
function validatePropNaming(node, proptypes) {
const component = components.get(node) || node;
const invalidProps = component.invalidProps || [];

runCheck(proptypes, (prop) => {
invalidProps.push(prop);
});

components.set(node, {
invalidProps,
});
const component = components.get(node);
if (component) {
const invalidProps = component.invalidProps;

if (invalidProps) {
runCheck(proptypes, (prop) => {
invalidProps.push(prop);
});

components.set(node, {
invalidProps,
});
}
}
}

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
36 changes: 16 additions & 20 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
32 changes: 17 additions & 15 deletions lib/rules/no-array-index-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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', {
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/no-find-dom-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/no-set-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
21 changes: 8 additions & 13 deletions lib/rules/no-string-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand All @@ -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';
}

/**
Expand All @@ -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 {
Expand All @@ -105,6 +99,7 @@ module.exports = {
});
}
},

JSXAttribute(node) {
if (
isRefAttribute(node)
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -592,7 +596,9 @@ module.exports = {
// Let's dive deeper into tags that are HTML/DOM elements (`<button>`), and not React components (`<Button />`)

// Some attributes are allowed on some tags only
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name) ? ATTRIBUTE_TAGS_MAP[/** @type {keyof ATTRIBUTE_TAGS_MAP} */ (name)] : null;
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name)
? ATTRIBUTE_TAGS_MAP[/** @type {keyof ATTRIBUTE_TAGS_MAP} */ (name)]
: null;
if (tagName && allowedTags) {
// Scenario 1A: Allowed attribute found where not supposed to, report it
if (allowedTags.indexOf(tagName) === -1) {
Expand Down
9 changes: 3 additions & 6 deletions lib/rules/no-unsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,15 @@ module.exports = {
const unsafe = {
UNSAFE_componentWillMount: {
newMethod: 'componentDidMount',
details:
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
},
UNSAFE_componentWillReceiveProps: {
newMethod: 'getDerivedStateFromProps',
details:
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
},
UNSAFE_componentWillUpdate: {
newMethod: 'componentDidUpdate',
details:
'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
},
};
if (checkAliases) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unstable-nested-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function generateErrorMessageWithParentName(parentName) {
* @returns {boolean}
*/
function startsWithRender(text) {
return (text || '').startsWith('render');
return typeof text === 'string' && text.startsWith('render');
}

/**
Expand Down
Loading

0 comments on commit bfa325a

Please sign in to comment.