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

[Feature Request] use variable name for anonymous functions assigned to a variable/const #4

Open
jxn-30 opened this issue Feb 3, 2024 · 0 comments

Comments

@jxn-30
Copy link

jxn-30 commented Feb 3, 2024

Problem description

Having for example the following code:

// complexity.ts
const iAmAnArrowFunction = (a: number, b: number): number => {
    return a + b;
};

function iAmAFunction(a: number, b: number): number {
    return a + b;
}

(() => {
    const a = 1;
    const b = 2;
    console.log(iAmAnArrowFunction(a, b));
    console.log(iAmAFunction(a, b));
})();

then npx cyclomatic-complexity complexity.ts will output:

complexity.ts: 4 (OK)
  anonymous: 2 (OK)
  anonymous: 1 (OK)
  iAmAFunction: 1 (OK)
  global: 0 (OK)
No issues found

Technically this is correct, as the functions are indeed anonymous functions, however it is difficult to determine which of the complexities applies to which function, especially if your file consists of many anonymous functions.

Feature Request / Idea for solving above mentioned problem

This feature requests asks to name anonymous functions assigned to variables/consts (such as iAmAnArrowFunction) by the name of this variable.
The output of npx cyclomatic-complexity complexity.ts would then look like this:

complexity.ts: 4 (OK)
  anonymous: 2 (OK)
  iAmAnArrowFunction: 1 (OK)
  iAmAFunction: 1 (OK)
  global: 0 (OK)
No issues found

One could argue to add a note that this function is anonymous, for example by adding a (anonymous) to the function name. However I don't think, this is quite necessary:

complexity.ts: 4 (OK)
  anonymous: 2 (OK)
  iAmAnArrowFunction (anonymous): 1 (OK)
  iAmAFunction: 1 (OK)
  global: 0 (OK)
No issues found

Possible difficulties

I am not into Esprima, so I am not sure, if one could easily adjust the following line or if a more complex logic would have to be introduced (such as remembering if this is a variable/const assignment and if yes, remember the variable/const name):

const functionName = node.callee.name;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant