fix: Regex to detect function args (#9634)

* fix: Regex to detect function args

* Add Tests for argString to Array

* fix: failed condition

* fix: bug with arrow function regex
This commit is contained in:
Aswath K 2021-12-14 13:28:09 +05:30 committed by GitHub
parent f7e2e079a9
commit c9e1a73db7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 2 deletions

View File

@ -0,0 +1,80 @@
jest.mock("sagas/ActionExecution/NavigateActionSaga", () => ({
__esModule: true,
default: "",
NavigationTargetType: { SAME_WINDOW: "" },
}));
import { argsStringToArray } from "./Fields";
describe("Test argStringToArray", () => {
const cases = [
{ index: 0, input: "", expected: [""] },
{ index: 1, input: "'a'", expected: ["'a'"] },
{ index: 2, input: "a", expected: ["a"] },
{ index: 3, input: "'a,b,c'", expected: ["'a,b,c'"] },
{ index: 4, input: "a,b,c", expected: ["a", "b", "c"] },
{ index: 5, input: "a, b, c", expected: ["a", " b", " c"] },
{ index: 6, input: "a , b , c", expected: ["a ", " b ", " c"] },
{ index: 7, input: "a\n,\nb,\nc", expected: ["a\n", "\nb", "\nc"] },
{ index: 8, input: "[a,b,c]", expected: ["[a,b,c]"] },
{ index: 9, input: "[a, b, c]", expected: ["[a, b, c]"] },
{
index: 10,
input: "[\n\ta,\n\tb,\n\tc\n]",
expected: ["[\n\ta,\n\tb,\n\tc\n]"],
},
{ index: 11, input: "{a:1,b:2,c:3}", expected: ["{a:1,b:2,c:3}"] },
{
index: 12,
input: '{"a":1,"b":2,"c":3}',
expected: ['{"a":1,"b":2,"c":3}'],
},
{
index: 13,
input: "{\n\ta:1,\n\tb:2,\n\tc:3}",
expected: ["{\n\ta:1,\n\tb:2,\n\tc:3}"],
},
{
index: 14,
input: "()=>{}",
expected: ["()=>{}"],
},
{
index: 15,
input: "(a, b)=>{return a+b}",
expected: ["(a, b)=>{return a+b}"],
},
{
index: 16,
input: "(a, b)=>{\n\treturn a+b;\n\t}",
expected: ["(a, b)=>{\n\treturn a+b;\n\t}"],
},
{
index: 17,
input: "(\n\ta,\n\tb\n)=>{\n\treturn a+b;\n\t}",
expected: ["(\n\ta,\n\tb\n)=>{\n\treturn a+b;\n\t}"],
},
{
index: 18,
input: `() => {return 5}`,
expected: ["() => {return 5}"],
},
{
index: 19,
input: `(a) => {return a + 1}`,
expected: ["(a) => {return a + 1}"],
},
{
index: 20,
input: `(a, b) => {return a + b}`,
expected: ["(a, b) => {return a + b}"],
},
];
test.each(cases.map((x) => [x.index, x.input, x.expected]))(
"test case %d",
(_, input, expected) => {
const result = argsStringToArray(input as string);
expect(result).toStrictEqual(expected);
},
);
});

View File

@ -73,7 +73,7 @@ const NAVIGATION_TARGET_FIELD_OPTIONS = [
},
];
export const FUNC_ARGS_REGEX = /((["][^"]*["])|([\[].*[\]])|([\{].*[\}])|(['][^']*['])|([\(].*[\)[=][>][{].*[}])|([^'",][^,"+]*[^'",]*))*/gi;
export const FUNC_ARGS_REGEX = /((["][^"]*["])|([\[][\s\S]*[\]])|([\{][\s\S]*[\}])|(['][^']*['])|([\(][\s\S]*[\)][ ]*=>[ ]*[{][\s\S]*[}])|([^'",][^,"+]*[^'",]*))*/gi;
export const ACTION_TRIGGER_REGEX = /^{{([\s\S]*?)\(([\s\S]*?)\)}}$/g;
//Old Regex:: /\(\) => ([\s\S]*?)(\([\s\S]*?\))/g;
export const ACTION_ANONYMOUS_FUNC_REGEX = /\(\) => (({[\s\S]*?})|([\s\S]*?)(\([\s\S]*?\)))/g;
@ -134,7 +134,7 @@ export const JSToString = (js: string): string => {
.join("");
};
const argsStringToArray = (funcArgs: string): string[] => {
export const argsStringToArray = (funcArgs: string): string[] => {
const argsplitMatches = [...funcArgs.matchAll(FUNC_ARGS_REGEX)];
const arr: string[] = [];
let isPrevUndefined = true;