Skip to content

Commit 12ebf5d

Browse files
authored
chore: capture destructured parameters (#251)
1 parent e2199b0 commit 12ebf5d

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

lib/__fixtures__/curriculum-helpers-javascript.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ const letFunction =
6565

6666
const arrowFunction = `const myFunc = name => console.log("Name")`;
6767

68+
const destructuredArgsFunctionDeclaration = `function printFruits({a, b},c = 1, ...rest) {`;
69+
6870
const testValues = {
6971
jsCodeWithSingleAndMultLineComments,
7072
jsCodeWithSingleAndMultLineCommentsRemoved,
@@ -78,6 +80,7 @@ const testValues = {
7880
constFunction,
7981
letFunction,
8082
arrowFunction,
83+
destructuredArgsFunctionDeclaration,
8184
};
8285

8386
export default testValues;

lib/__tests__/javascript-helper.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import jsTestValues from "../__fixtures__/curriculum-helpers-javascript";
22

33
import { getFunctionParams } from "../index";
44

5-
const { functionDeclaration, constFunction, letFunction, arrowFunction } =
6-
jsTestValues;
5+
const {
6+
functionDeclaration,
7+
constFunction,
8+
letFunction,
9+
arrowFunction,
10+
destructuredArgsFunctionDeclaration,
11+
} = jsTestValues;
712

813
describe("js-help", () => {
914
describe("getFunctionArgs", () => {
@@ -32,5 +37,13 @@ describe("js-help", () => {
3237
const parameters = getFunctionParams(arrowFunction);
3338
expect(parameters[0].name).toBe("name");
3439
});
40+
it("gets arguments from a destructured function declaration", function () {
41+
const parameters = getFunctionParams(destructuredArgsFunctionDeclaration);
42+
expect(parameters[0].name).toBe("a");
43+
expect(parameters[1].name).toBe("b");
44+
expect(parameters[2].name).toBe("c");
45+
expect(parameters[2].defaultValue).toBe("1");
46+
expect(parameters[3].name).toBe("...rest");
47+
});
3548
});
3649
});

lib/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ export class CSSHelp {
425425
/**
426426
* Extracts all function parameters and default values from a function
427427
* @param functionObject A function in string form
428-
* @returns {{name:String,defaultValue: String | undefined}}
428+
* Note: All number parameters will returned as a string,
429+
* @returns {{name:string,defaultValue: string | undefined}}
429430
*/
430431
export function getFunctionParams(code: string) {
431432
// Regular expression to match function declarations, arrow functions, and function expressions
@@ -447,18 +448,22 @@ export function getFunctionParams(code: string) {
447448
const paramString =
448449
paramMatch[1] || paramMatch[2] || paramMatch[3] || paramMatch[4];
449450
// Split the parameter string by commas to get individual parameters
450-
const params = paramString.split(",").map((param: string) => {
451-
// Split each parameter by '=' to separate name and default value
452-
const parts = param.trim().split("=");
453-
// If the parameter has a default value, extract it, otherwise set it to undefined
454-
const defaultValue =
455-
parts.length > 1 ? parts[1].replace(/['"]/g, "").trim() : undefined;
456-
// Return an object with the parameter name and default value
457-
return {
458-
name: parts[0].trim(),
459-
defaultValue: defaultValue,
460-
};
461-
});
451+
const params = paramString
452+
.replace(/[{}[\]]/g, "")
453+
.split(",")
454+
.map((param: string) => {
455+
// Split each parameter by '=' to separate name and default value
456+
const parts = param.trim().split(/[=]/);
457+
// If the parameter has a default value, extract it, otherwise set it to undefined
458+
const defaultValue =
459+
parts.length > 1 ? parts[1].replace(/['"]/g, "").trim() : undefined;
460+
461+
// Return an object with the parameter name and default value
462+
return {
463+
name: parts[0].trim(),
464+
defaultValue: defaultValue,
465+
};
466+
});
462467
return params;
463468
}
464469

0 commit comments

Comments
 (0)