Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/helpers/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ export function getFunctionParams(code: string) {
code.match(functionVariableRegex) ||
code.match(arrowFunctionRegex);

if (paramMatch) {
if (paramMatch && paramMatch.length > 1 && paramMatch[1].trim() !== "") {
// Find the captured group containing the parameters
const paramString =
paramMatch[1] || paramMatch[2] || paramMatch[3] || paramMatch[4];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const jsCodeWithSingleAndMultLineComments = `
function nonMutatingPush(original, newItem) {
/* This is a
multi-line comment
multi-line comment
multi-line comment
that should be removed. */
return original.push(newItem);
}
Expand Down Expand Up @@ -67,6 +68,10 @@ const arrowFunction = `const myFunc = name => console.log("Name")`;

const destructuredArgsFunctionDeclaration = `function printFruits({a, b},c = 1, ...rest) {`;

const noParameterFunctionDeclaration = `function myFunction() {`;

const noParameterFunctionSpacesDeclaration = `function myFunction( ) {`;

const testValues = {
jsCodeWithSingleAndMultLineComments,
jsCodeWithSingleAndMultLineCommentsRemoved,
Expand All @@ -81,6 +86,8 @@ const testValues = {
letFunction,
arrowFunction,
destructuredArgsFunctionDeclaration,
noParameterFunctionDeclaration,
noParameterFunctionSpacesDeclaration,
};

export default testValues;
12 changes: 12 additions & 0 deletions packages/tests/javascript-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const {
letFunction,
arrowFunction,
destructuredArgsFunctionDeclaration,
noParameterFunctionDeclaration,
noParameterFunctionSpacesDeclaration,
} = jsTestValues;

describe("js-help", () => {
Expand Down Expand Up @@ -45,6 +47,16 @@ describe("js-help", () => {
expect(parameters[2].defaultValue).toBe("1");
expect(parameters[3].name).toBe("...rest");
});
it("returns empty parameter array from function declaration with no parameters", () => {
const parameters = getFunctionParams(noParameterFunctionDeclaration);
expect(parameters.length).toBe(0);
});
it("returns empty parameter array from function declaration with no parameters and spaces", () => {
const parameters = getFunctionParams(
noParameterFunctionSpacesDeclaration,
);
expect(parameters.length).toBe(0);
});
});

describe("retryingTest", () => {
Expand Down
Loading