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 8dc0215 commit 3b6bacc
Show file tree
Hide file tree
Showing 28 changed files with 300 additions and 255 deletions.
20 changes: 11 additions & 9 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 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
22 changes: 11 additions & 11 deletions lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

const docsUrl = require('../util/docsUrl');
const ast = require('../util/ast');
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const report = require('../util/report');
const getScope = require('../util/eslint').getScope;
Expand Down Expand Up @@ -44,7 +44,7 @@ function getName(node) {
}

function isThisExpression(node) {
return ast.unwrapTSAsExpression(uncast(node)).type === 'ThisExpression';
return astUtil.unwrapTSAsExpression(uncast(node)).type === 'ThisExpression';
}

function getInitialClassInfo() {
Expand All @@ -65,7 +65,7 @@ function getInitialClassInfo() {
}

function isSetStateCall(node) {
const unwrappedCalleeNode = ast.unwrapTSAsExpression(node.callee);
const unwrappedCalleeNode = astUtil.unwrapTSAsExpression(node.callee);

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

switch (left.type) {
case 'Identifier':
Expand Down Expand Up @@ -292,8 +292,8 @@ module.exports = {
return;
}

const unwrappedNode = ast.unwrapTSAsExpression(node);
const unwrappedArgumentNode = ast.unwrapTSAsExpression(unwrappedNode.arguments[0]);
const unwrappedNode = astUtil.unwrapTSAsExpression(node);
const unwrappedArgumentNode = astUtil.unwrapTSAsExpression(unwrappedNode.arguments[0]);

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

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

const name = getName(node.key);
if (
Expand Down Expand Up @@ -452,8 +452,8 @@ module.exports = {
return;
}

const unwrappedLeft = ast.unwrapTSAsExpression(node.left);
const unwrappedRight = ast.unwrapTSAsExpression(node.right);
const unwrappedLeft = astUtil.unwrapTSAsExpression(node.left);
const unwrappedRight = astUtil.unwrapTSAsExpression(node.right);

// Check for assignments like `this.state = {}`
if (
Expand Down Expand Up @@ -493,7 +493,7 @@ module.exports = {
if (!classInfo) {
return;
}
if (isStateReference(ast.unwrapTSAsExpression(node.object))) {
if (isStateReference(astUtil.unwrapTSAsExpression(node.object))) {
// If we see this.state[foo] access, give up.
if (node.computed && node.property.type !== 'Literal') {
classInfo = null;
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ module.exports = {
}
scope = scope.upper;
}
const isRender = blockNode && blockNode.key && blockNode.key.name === 'render';
const isRender = blockNode
&& blockNode.key
&& blockNode.key.name === 'render';
const allowNull = testReactVersion(context, '>= 15.0.0'); // Stateless components can return null since React 15
const isReturningJSX = utils.isReturningJSX(node, !allowNull);
const isReturningNull = node.argument && (node.argument.value === null || node.argument.value === false);
Expand Down
Loading

0 comments on commit 3b6bacc

Please sign in to comment.