Skip to content

Commit

Permalink
feat(getCallExpressionIdentifier): add resolveCallExpression option
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Dec 22, 2023
1 parent 5787de9 commit e112028
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
14 changes: 11 additions & 3 deletions workspaces/estree-ast-utils/src/getCallExpressionIdentifier.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
// Import Internal Dependencies
import { getMemberExpressionIdentifier } from "./getMemberExpressionIdentifier.js";
import { VariableTracer } from "./utils/VariableTracer.js";

/**
* @param {any} node
* @param {object} options
* @param {VariableTracer} [options.tracer=null]
* @param {boolean} [options.resolveCallExpression=true]
* @returns {string | null}
*/
export function getCallExpressionIdentifier(node) {
export function getCallExpressionIdentifier(node, options = {}) {
if (node.type !== "CallExpression") {
return null;
}
const { tracer = null, resolveCallExpression = true } = options;

if (node.callee.type === "Identifier") {
return node.callee.name;
}
if (node.callee.type === "MemberExpression") {
return [...getMemberExpressionIdentifier(node.callee)].join(".");
return [
...getMemberExpressionIdentifier(node.callee, { tracer })
].join(".");
}

return getCallExpressionIdentifier(node.callee);
return resolveCallExpression ?
getCallExpressionIdentifier(node.callee, { tracer }) : null;
}
2 changes: 1 addition & 1 deletion workspaces/estree-ast-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getCallExpressionArguments(
): string[] | null;

export function getCallExpressionIdentifier(
node: any
node: any, options?: { tracer?: VariableTracer, resolveCallExpression?: boolean }
): string | null;

export function getMemberExpressionIdentifier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,54 @@ test("given a JavaScript eval CallExpression then it must return eval", (tape) =
tape.end();
});

test("given a JavaScript Function() CallExpression then it must return Function", (tape) => {
test("given a Function(`...`)() Double CallExpression then it must return the Function literal identifier", (tape) => {
const [astNode] = codeToAst("Function(\"return this\")();");
const nodeIdentifier = getCallExpressionIdentifier(getExpressionFromStatement(astNode));

tape.strictEqual(nodeIdentifier, "Function");
tape.end();
});

test(`given a Function("...")() Double CallExpression with resolveCallExpression options disabled
then it must return null`, (tape) => {
const [astNode] = codeToAst("Function(\"return this\")();");
const nodeIdentifier = getCallExpressionIdentifier(
getExpressionFromStatement(astNode),
{ resolveCallExpression: false }
);

tape.strictEqual(nodeIdentifier, null);
tape.end();
});

test("given a JavaScript AssignmentExpression then it must return null", (tape) => {
const [astNode] = codeToAst("foo = 10;");
const nodeIdentifier = getCallExpressionIdentifier(getExpressionFromStatement(astNode));

tape.strictEqual(nodeIdentifier, null);
tape.end();
});

test(`given a require statement immediatly invoked with resolveCallExpression options enabled
then it must return require literal identifier`, (tape) => {
const [astNode] = codeToAst("require('foo')();");
const nodeIdentifier = getCallExpressionIdentifier(
getExpressionFromStatement(astNode),
{ resolveCallExpression: true }
);

tape.strictEqual(nodeIdentifier, "require");
tape.end();
});

test(`given a require statement immediatly invoked with resolveCallExpression options disabled
then it must return null`, (tape) => {
const [astNode] = codeToAst("require('foo')();");
const nodeIdentifier = getCallExpressionIdentifier(
getExpressionFromStatement(astNode),
{ resolveCallExpression: false }
);

tape.strictEqual(nodeIdentifier, null);
tape.end();
});

0 comments on commit e112028

Please sign in to comment.