Skip to content

Commit 56a5272

Browse files
fix: adds function expression to functionRegex function (#549)
* fix: add check for js function expressions in functionRegex * fix: add tests * fix: lint errors * change expression name
1 parent ef4f847 commit 56a5272

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

packages/helpers/lib/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,18 @@ export function functionRegex(
264264
const arrowFuncRegEx = includeBody
265265
? `${arrowFuncREHead}${arrowFuncREBody}`
266266
: `${arrowFuncREHead}`;
267-
return new RegExp(`(${capture ? "" : "?:"}${funcRegEx}|${arrowFuncRegEx})`);
267+
268+
const anonymousFunctionName = funcName
269+
? `(let|const|var)?\\s?${escapeRegExp(funcName)}\\s*=\\s*function\\s*`
270+
: "";
271+
const anonymousFuncREHead = `${anonymousFunctionName}\\(\\s*${params}\\s*\\)\\s*\\{`;
272+
const anonymousFuncRegEx = includeBody
273+
? `${anonymousFuncREHead}${funcREBody}`
274+
: `${anonymousFuncREHead}`;
275+
276+
return new RegExp(
277+
`(${capture ? "" : "?:"}${funcRegEx}|${arrowFuncRegEx}|${anonymousFuncRegEx})`,
278+
);
268279
}
269280

270281
function _permutations(permutation: (string | RegExp)[]) {

packages/tests/curriculum-helper.test.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,51 @@ describe("functionRegex", () => {
670670
expect(funcRE.test(code)).toBe(true);
671671
expect(code.match(funcRE)![0]).toBe("const naomi = (love) => ");
672672
});
673+
674+
it("matches named anonymous function", () => {
675+
const funcName = "myFunc";
676+
const regEx = functionRegex(funcName, ["arg1"]);
677+
expect(regEx.test("myFunc = function(arg1){}")).toBe(true);
678+
});
679+
it("matches named anonymous function without parameters", () => {
680+
const funcName = "myFunc";
681+
const regEx = functionRegex(funcName);
682+
expect(regEx.test("myFunc = function(){ }")).toBe(true);
683+
});
684+
it("does not match named anonymous function with different name", () => {
685+
const funcName = "myFunc";
686+
const regEx = functionRegex(funcName);
687+
expect(regEx.test("notMyFunc = function(arg1){ }")).toBe(false);
688+
});
689+
690+
it("matches named anonymous function with arguments and a body", () => {
691+
const funcName = "myFunc";
692+
const regEx = functionRegex(funcName, ["arg1", "arg2"]);
693+
expect(
694+
regEx.test("myFunc = function(arg1, arg2){\n console.log()\n}"),
695+
).toBe(true);
696+
});
697+
698+
it("does not match named anonymous function with different arguments", () => {
699+
const funcName = "myFunc";
700+
const regEx = functionRegex(funcName, ["arg1", "arg2"]);
701+
expect(regEx.test("myFunc = function(arg1, arg3){}")).toBe(false);
702+
});
703+
it("matches const named anonymous function declarations if they are present", () => {
704+
const regEx = functionRegex("myFunc", ["arg1", "arg2"]);
705+
const func = "const myFunc = function(arg1, arg2) {}";
706+
const match = func.match(regEx);
707+
expect(match).not.toBeNull();
708+
expect(match![0]).toBe(func);
709+
});
710+
it("can capture named anonymous function", () => {
711+
const funcName = "myFunc";
712+
const regEx = functionRegex(funcName, ["arg1", "arg2"], { capture: true });
713+
const func = "myFunc = function(arg1, arg2){return arg1 + arg2}";
714+
const match = func.match(regEx);
715+
expect(match).not.toBeNull();
716+
expect(match![1]).toBe(func);
717+
});
673718
});
674719

675720
describe("prepTestComponent", () => {

0 commit comments

Comments
 (0)