Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify isSimpleCallArgument #15923

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions src/language-js/utils/index.js
Original file line number Diff line number Diff line change
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
Loading