Skip to content

Commit

Permalink
[Refactor] add astUtil.isCallExpression predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 30, 2024
1 parent 3b6bacc commit 0170dbe
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 50 deletions.
7 changes: 4 additions & 3 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const values = require('object.values');

const Components = require('../util/Components');
const propsUtil = require('../util/props');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const propWrapperUtil = require('../util/propWrapper');
const report = require('../util/report');
Expand All @@ -26,7 +27,7 @@ const getText = eslintUtil.getText;
function nestedPropTypes(prop) {
return (
prop.type === 'Property'
&& prop.value.type === 'CallExpression'
&& astUtil.isCallExpression(prop.value)
);
}

Expand Down Expand Up @@ -311,7 +312,7 @@ module.exports = {
}
if (
node.value
&& node.value.type === 'CallExpression'
&& astUtil.isCallExpression(node.value)
&& propWrapperUtil.isPropWrapperFunction(
context,
getText(context, node.value.callee)
Expand All @@ -337,7 +338,7 @@ module.exports = {
}
const right = node.parent.right;
if (
right.type === 'CallExpression'
astUtil.isCallExpression(right)
&& propWrapperUtil.isPropWrapperFunction(
context,
getText(context, right.callee)
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ module.exports = {
* @returns {boolean} True if React.forwardRef is nested inside React.memo, false if not.
*/
function isNestedMemo(node) {
return node.type === 'CallExpression'
return astUtil.isCallExpression(node)
&& node.arguments
&& node.arguments[0]
&& node.arguments[0].type === 'CallExpression'
&& astUtil.isCallExpression(node.arguments[0])
&& utils.isPragmaComponentWrapper(node);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module.exports = {
) {
value = value.object;
}
if (value.type === 'CallExpression') {
if (astUtil.isCallExpression(value)) {
if (!isPropTypesPackage(value.callee)) {
return;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ module.exports = {
if (propTypesObject && propTypesObject.properties) {
checkProperties(propTypesObject.properties);
}
} else if (node.type === 'CallExpression') {
} else if (astUtil.isCallExpression(node)) {
const innerNode = node.arguments && node.arguments[0];
if (
propWrapperUtil.isPropWrapperFunction(context, getText(context, node.callee))
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const propName = require('jsx-ast-utils/propName');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const jsxUtil = require('../util/jsx');
const report = require('../util/report');
const getAncestors = require('../util/eslint').getAncestors;
Expand Down Expand Up @@ -83,10 +84,9 @@ module.exports = {
}

function getNodeViolationType(node) {
const nodeType = node.type;
if (
!configuration.allowBind
&& nodeType === 'CallExpression'
&& astUtil.isCallExpression(node)
&& node.callee.type === 'MemberExpression'
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'bind'
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/jsx-no-useless-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const arrayIncludes = require('array-includes');

const pragmaUtil = require('../util/pragma');
const astUtil = require('../util/ast');
const jsxUtil = require('../util/jsx');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -75,8 +76,7 @@ function isKeyedElement(node) {
function containsCallExpression(node) {
return node
&& node.type === 'JSXExpressionContainer'
&& node.expression
&& node.expression.type === 'CallExpression';
&& astUtil.isCallExpression(node.expression);
}

const messages = {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-access-state-in-setstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const report = require('../util/report');
const getScope = require('../util/eslint').getScope;
Expand All @@ -32,7 +33,7 @@ module.exports = {

create(context) {
function isSetStateCall(node) {
return node.type === 'CallExpression'
return astUtil.isCallExpression(node)
&& node.callee.property
&& node.callee.property.name === 'setState'
&& node.callee.object.type === 'ThisExpression';
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-adjacent-inline-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const docsUrl = require('../util/docsUrl');
const isCreateElement = require('../util/isCreateElement');
const report = require('../util/report');
const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -62,7 +63,7 @@ function isInline(node) {
if (node.type === 'JSXElement' && inlineNames.indexOf(node.openingElement.name.name) > -1) {
return true;
}
if (node.type === 'CallExpression' && inlineNames.indexOf(node.arguments[0].value) > -1) {
if (astUtil.isCallExpression(node) && inlineNames.indexOf(node.arguments[0].value) > -1) {
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-array-index-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ module.exports = {
}

if (
node.type === 'CallExpression'
astUtil.isCallExpression(node)
&& node.callee
&& node.callee.type === 'MemberExpression'
&& node.callee.object
Expand All @@ -207,7 +207,7 @@ module.exports = {
}

if (
node.type === 'CallExpression'
astUtil.isCallExpression(node)
&& node.callee
&& node.callee.type === 'Identifier'
&& node.callee.name === 'String'
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-object-type-as-default-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const values = require('object.values');

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const report = require('../util/report');

const FORBIDDEN_TYPES_MAP = {
Expand Down Expand Up @@ -55,7 +56,7 @@ function verifyDefaultPropsDestructuring(context, properties) {
},
});
} else if (
propDefaultValueType === 'CallExpression'
astUtil.isCallExpression(propDefaultValue.right)
&& propDefaultValue.right.callee.type === 'Identifier'
&& propDefaultValue.right.callee.name === 'Symbol'
) {
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const PROP_TYPES = Object.keys(require('prop-types'));
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const report = require('../util/report');
const lifecycleMethods = require('../util/lifecycleMethods');
Expand Down Expand Up @@ -109,11 +110,11 @@ module.exports = {
&& node.property.name !== 'isRequired'
) { // PropTypes.myProp
checkValidPropType(node.property);
} else if (node.object.type === 'CallExpression') {
} else if (astUtil.isCallExpression(node.object)) {
checkValidPropTypeQualifier(node.property);
checkValidCallExpression(node.object);
}
} else if (node.type === 'CallExpression') {
} else if (astUtil.isCallExpression(node)) {
checkValidCallExpression(node);
}
}
Expand Down
15 changes: 3 additions & 12 deletions lib/rules/no-unstable-nested-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const isCreateElement = require('../util/isCreateElement');
const report = require('../util/report');

Expand Down Expand Up @@ -66,8 +67,7 @@ function getClosestMatchingParent(node, context, matcher) {
*/
function isCreateElementMatcher(node, context) {
return (
node
&& node.type === 'CallExpression'
astUtil.isCallExpression(node)
&& isCreateElement(context, node)
);
}
Expand Down Expand Up @@ -117,15 +117,6 @@ function isPropertyOfObjectExpressionMatcher(node) {
);
}

/**
* Matcher used to check whether given node is a `CallExpression`
* @param {ASTNode} node The AST node
* @returns {boolean} True if node is a `CallExpression`, false if not
*/
function isCallExpressionMatcher(node) {
return node && node.type === 'CallExpression';
}

/**
* Check whether given node or its parent is directly inside `map` call
* ```jsx
Expand Down Expand Up @@ -158,7 +149,7 @@ function isReturnStatementOfHook(node, context) {
return false;
}

const callExpression = getClosestMatchingParent(node, context, isCallExpressionMatcher);
const callExpression = getClosestMatchingParent(node, context, astUtil.isCallExpression);
return (
callExpression
&& callExpression.callee
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ module.exports = {
// Otherwise, record that we saw this property being accessed.
addUsedStateField(node.property);
// If we see a `this.state` access in a CallExpression, give up.
} else if (isStateReference(node) && node.parent.type === 'CallExpression') {
} else if (isStateReference(node) && astUtil.isCallExpression(node.parent)) {
classInfo = null;
}
},
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/prefer-exact-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const propsUtil = require('../util/props');
const propWrapperUtil = require('../util/propWrapper');
const variableUtil = require('../util/variable');
Expand Down Expand Up @@ -81,8 +82,7 @@ module.exports = {

function isNonExactPropWrapperFunction(node) {
return (
node
&& node.type === 'CallExpression'
astUtil.isCallExpression(node)
&& !propWrapperUtil.isExactPropWrapperFunction(context, getText(context, node.callee))
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
return (
body.length === 1
&& body[0].type === 'ExpressionStatement'
&& body[0].expression.type === 'CallExpression'
&& astUtil.isCallExpression(body[0].expression)
&& body[0].expression.callee.type === 'Super'
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'use strict';

const astUtil = require('../util/ast');
const variableUtil = require('../util/variable');
const propsUtil = require('../util/props');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -196,7 +197,7 @@ module.exports = {
if (propTypesObject && propTypesObject.properties) {
checkSorted(propTypesObject.properties);
}
} else if (node.type === 'CallExpression') {
} else if (astUtil.isCallExpression(node)) {
const innerNode = node.arguments && node.arguments[0];
if (propWrapperUtil.isPropWrapperFunction(context, node.callee.name) && innerNode) {
checkNode(innerNode);
Expand Down
4 changes: 2 additions & 2 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ function componentRule(rule, context) {
},

isPragmaComponentWrapper(node) {
if (!node || node.type !== 'CallExpression') {
if (!astUtil.isCallExpression(node)) {
return false;
}

Expand Down Expand Up @@ -763,7 +763,7 @@ function componentRule(rule, context) {
* @returns {boolean} True if the node is a call to a React hook
*/
isReactHookCall(node, expectedHookNames) {
if (node.type !== 'CallExpression') {
if (!astUtil.isCallExpression(node)) {
return false;
}

Expand Down
12 changes: 11 additions & 1 deletion lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function traverseReturns(ASTNode, context, onReturn) {
}

/* TODO: properly warn on React.forwardRefs having typo properties
if (nodeType === 'CallExpression') {
if (astUtil.isCallExpression(ASTNode)) {
const callee = ASTNode.callee;
const pragma = pragmaUtil.getFromContext(context);
if (
Expand Down Expand Up @@ -347,6 +347,15 @@ function isTSAsExpression(node) {
return node && node.type === 'TSAsExpression';
}

/**
* Matcher used to check whether given node is a `CallExpression`
* @param {ASTNode} node The AST node
* @returns {boolean} True if node is a `CallExpression`, false if not
*/
function isCallExpression(node) {
return node && node.type === 'CallExpression';
}

/**
* Extracts the expression node that is wrapped inside a TS type assertion
*
Expand Down Expand Up @@ -448,6 +457,7 @@ module.exports = {
getPropertyNameNode,
inConstructor,
isAssignmentLHS,
isCallExpression,
isClass,
isFunction,
isFunctionLike,
Expand Down
2 changes: 1 addition & 1 deletion lib/util/defaultProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function defaultPropsInstructions(context, components, utils) {
return variableUtil.findVariableByName(context, node, node.name);
}
if (
node.type === 'CallExpression'
astUtil.isCallExpression(node)
&& propWrapperUtil.isPropWrapperFunction(context, node.callee.name)
&& node.arguments && node.arguments[0]
) {
Expand Down
6 changes: 4 additions & 2 deletions lib/util/isCreateContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const astUtil = require('./ast');

/**
* Checks if the node is a React.createContext call
* @param {ASTNode} node - The AST node being checked.
Expand All @@ -11,7 +13,7 @@ module.exports = function isCreateContext(node) {
&& node.init.callee
) {
if (
node.init.type === 'CallExpression'
astUtil.isCallExpression(node.init)
&& node.init.callee.name === 'createContext'
) {
return true;
Expand All @@ -30,7 +32,7 @@ module.exports = function isCreateContext(node) {
node.expression
&& node.expression.type === 'AssignmentExpression'
&& node.expression.operator === '='
&& node.expression.right.type === 'CallExpression'
&& astUtil.isCallExpression(node.expression.right)
&& node.expression.right.callee
) {
const right = node.expression.right;
Expand Down
Loading

0 comments on commit 0170dbe

Please sign in to comment.