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

getParameters for func 'a => console.log(a)' error #4158

Closed
AimWhy opened this issue Mar 19, 2025 · 3 comments
Closed

getParameters for func 'a => console.log(a)' error #4158

AimWhy opened this issue Mar 19, 2025 · 3 comments
Labels
inactive Issue has not had recent required feedback needs-more-info Needs further information, steps, details, etc.

Comments

@AimWhy
Copy link

AimWhy commented Mar 19, 2025

Description

const comma = ',';
const equals = '=';
const openBrace = '{';
const openParen = '(';
const closeParen = ')';
const arrow = '=>';

const fnBodyRegex = /(([\s\S]))/;
const fnBodyStripCommentsRegex = /(/*([\s\S]
?)*/|([^:]|^)//(.)$)/gm;
const fnBodyStripParamDefaultValueRegex = /\s?=.
$/;

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function getParameters(fn: Function): string[] {
if (typeof fn !== 'function') {
throw new Error('Not supported');
}

if (fn.length === 0) {
    return [];
}

let fnBody: string = Function.prototype.toString.call(fn);
fnBody = fnBody.replace(fnBodyStripCommentsRegex, '') || fnBody;
fnBody = fnBody.slice(0, fnBody.includes(openBrace) ? fnBody.indexOf(openBrace) : void 0);
fnBody = fnBody.slice(0, fnBody.includes(arrow) ? fnBody.indexOf(arrow) : void 0);

let open = fnBody.indexOf(openParen);
let close: number | undefined = fnBody.indexOf(closeParen);

open = open >= 0 ? open + 1 : 0;
close = close > 0 ? close : (fnBody.includes(equals) ? fnBody.indexOf(equals) : void 0);

fnBody = fnBody.slice(open, close);
fnBody = `(${fnBody})`;

const match = fnBodyRegex.exec(fnBody);
return match != null
    ? match[1].split(comma).map(param => param.trim().replace(fnBodyStripParamDefaultValueRegex, ''))
    : [];

}

GitLens Version

main

VS Code Version

No response

Git Version

No response

Logs, Screenshots, Screen Captures, etc

No response

@AimWhy AimWhy added the triage Needs to be looked at label Mar 19, 2025
@AimWhy AimWhy changed the title getParameters for func 'a => a' error getParameters for func 'a => console.log(a)' error Mar 19, 2025
@d13
Copy link
Member

d13 commented Mar 19, 2025

How did you run into this error?

@d13 d13 added needs-more-info Needs further information, steps, details, etc. and removed triage Needs to be looked at labels Mar 19, 2025
Copy link

This issue needs more information and has not had recent activity. Please provide the missing information or it will be closed in 7 days. Thanks!

@github-actions github-actions bot added the inactive Issue has not had recent required feedback label Mar 27, 2025
Copy link

github-actions bot commented Apr 4, 2025

Closing this issue because it needs more information and has not had recent activity. Please re-open this issue if more details can be provided. Thanks!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inactive Issue has not had recent required feedback needs-more-info Needs further information, steps, details, etc.
Projects
None yet
Development

No branches or pull requests

2 participants