Skip to content

Commit

Permalink
Simplify isSimpleCallArgument (#15923)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 16, 2024
1 parent 10762ee commit be17405
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/language-js/utils/index.js
Expand Up @@ -419,22 +419,6 @@ const isMemberExpression = skipChainExpression(
createTypeCheckFunction(["MemberExpression", "OptionalMemberExpression"]),
);

/**
* Retrieves a property from a node, considering any ChainExpression.
* If the node is a ChainExpression, the property is obtained from its expression.
* Otherwise, the property is obtained directly from the node.
*
* @param {Node} node - The AST node to be processed.
* @param {string} property - The property name to retrieve.
* @returns The property value from the node or its expression.
*/
function getChainProp(node, property) {
if (node.type === "ChainExpression") {
return node.expression[property];
}
return node[property];
}

/**
*
* @param {any} node
Expand Down Expand Up @@ -740,6 +724,10 @@ function isSimpleCallArgument(node, depth = 2) {
return false;
}

if (node.type === "ChainExpression" || node.type === "TSNonNullExpression") {
return isSimpleCallArgument(node.expression, depth);
}

const isChildSimple = (child) => isSimpleCallArgument(child, depth - 1);

if (isRegExpLiteral(node)) {
Expand Down Expand Up @@ -775,7 +763,7 @@ function isSimpleCallArgument(node, depth = 2) {
if (isCallLikeExpression(node)) {
if (
node.type === "ImportExpression" ||
isSimpleCallArgument(getChainProp(node, "callee"), depth)
isSimpleCallArgument(node.callee, depth)
) {
const args = getCallArguments(node);
return args.length <= depth && args.every(isChildSimple);
Expand All @@ -785,8 +773,8 @@ function isSimpleCallArgument(node, depth = 2) {

if (isMemberExpression(node)) {
return (
isSimpleCallArgument(getChainProp(node, "object"), depth) &&
isSimpleCallArgument(getChainProp(node, "property"), depth)
isSimpleCallArgument(node.object, depth) &&
isSimpleCallArgument(node.property, depth)
);
}

Expand All @@ -798,10 +786,6 @@ function isSimpleCallArgument(node, depth = 2) {
return isSimpleCallArgument(node.argument, depth);
}

if (node.type === "TSNonNullExpression") {
return isSimpleCallArgument(node.expression, depth);
}

return false;
}

Expand Down Expand Up @@ -1238,7 +1222,6 @@ export {
createTypeCheckFunction,
getCallArguments,
getCallArgumentSelector,
getChainProp,
getComments,
getFunctionParameters,
getLeftSide,
Expand Down

0 comments on commit be17405

Please sign in to comment.