Skip to content

Commit 2ad580e

Browse files
committed
Inline fixed get-params + handle falsy values in flatTree
1 parent 8f5cf1b commit 2ad580e

3 files changed

Lines changed: 71 additions & 19 deletions

File tree

packages/redux-devtools-utils/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
"@babel/runtime": "^7.28.4",
3636
"@redux-devtools/core": "workspace:^",
3737
"@redux-devtools/serialize": "workspace:^",
38-
"@types/get-params": "^0.1.2",
39-
"get-params": "^0.1.2",
4038
"immutable": "^5.1.4",
4139
"jsan": "^3.1.14",
4240
"nanoid": "^5.1.6",

packages/redux-devtools-utils/src/index.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import getParams from 'get-params';
21
import jsan from 'jsan';
32
import { nanoid } from 'nanoid/non-secure';
43
import { immutableSerialize } from '@redux-devtools/serialize';
@@ -15,13 +14,84 @@ export interface ActionCreatorObject {
1514
readonly args: readonly string[];
1615
}
1716

17+
function getParams(func: Function) {
18+
if (typeof func !== 'function') return [];
19+
20+
const src = func
21+
.toString()
22+
// remove comments
23+
.replace(/\/\*[\s\S]*?\*\//g, '')
24+
.replace(/\/\/.*$/gm, '')
25+
.trim();
26+
27+
let paramsSrc = '';
28+
29+
// function foo(a, b)
30+
const fnMatch = src.match(/^[^(]*\(\s*([^)]*)\)/);
31+
32+
// (a, b) => or a =>
33+
const arrowMatch = src.match(/^(?:\(\s*([^)]*)\)|([^\s=]+))\s*=>/);
34+
35+
if (fnMatch) {
36+
paramsSrc = fnMatch[1] ?? '';
37+
} else if (arrowMatch) {
38+
paramsSrc = arrowMatch[1] ?? arrowMatch[2] ?? '';
39+
} else {
40+
return [];
41+
}
42+
43+
if (!paramsSrc) return [];
44+
45+
const params = [];
46+
let current = '';
47+
let depth = 0;
48+
49+
for (let i = 0; i < paramsSrc.length; i++) {
50+
const ch = paramsSrc[i];
51+
52+
if (ch === ',' && depth === 0) {
53+
params.push(current.trim());
54+
current = '';
55+
continue;
56+
}
57+
58+
if (ch === '{' || ch === '[' || ch === '(') depth++;
59+
if (ch === '}' || ch === ']' || ch === ')') depth--;
60+
61+
current += ch;
62+
}
63+
64+
if (current.trim()) {
65+
params.push(current.trim());
66+
}
67+
68+
// remove default values: a = 1 → a
69+
return params.map((p, i) => {
70+
const cleaned = p.replace(/=.*/, '').trim();
71+
72+
// destructured parameter
73+
if (
74+
cleaned.startsWith('{') ||
75+
cleaned.startsWith('[') ||
76+
cleaned.startsWith('...{') ||
77+
cleaned.startsWith('...[')
78+
) {
79+
return `arg_${i}`;
80+
}
81+
82+
return cleaned;
83+
});
84+
}
85+
1886
function flatTree(
1987
obj: { [key: string]: ActionCreator<Action<string>> },
2088
namespace = '',
2189
) {
90+
if (!obj) return [];
2291
let functions: ActionCreatorObject[] = [];
2392
Object.keys(obj).forEach((key) => {
2493
const prop = obj[key];
94+
if (!prop) return;
2595
if (typeof prop === 'function') {
2696
functions.push({
2797
name: namespace + (key || prop.name || 'anonymous'),

pnpm-lock.yaml

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)