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
5 changes: 5 additions & 0 deletions .changeset/puny-phones-like.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@redux-devtools/utils': patch
---

Fix function parameters detection
2 changes: 0 additions & 2 deletions packages/redux-devtools-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
"@babel/runtime": "^7.28.4",
"@redux-devtools/core": "workspace:^",
"@redux-devtools/serialize": "workspace:^",
"@types/get-params": "^0.1.2",
"get-params": "^0.1.2",
"immutable": "^5.1.4",
"jsan": "^3.1.14",
"nanoid": "^5.1.6",
Expand Down
72 changes: 71 additions & 1 deletion packages/redux-devtools-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getParams from 'get-params';
import jsan from 'jsan';
import { nanoid } from 'nanoid/non-secure';
import { immutableSerialize } from '@redux-devtools/serialize';
Expand All @@ -15,13 +14,84 @@ export interface ActionCreatorObject {
readonly args: readonly string[];
}

function getParams(func: Function) {
if (typeof func !== 'function') return [];

const src = func
.toString()
// remove comments
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/\/\/.*$/gm, '')
.trim();

let paramsSrc = '';

// function foo(a, b)
const fnMatch = src.match(/^[^(]*\(\s*([^)]*)\)/);

// (a, b) => or a =>
const arrowMatch = src.match(/^(?:\(\s*([^)]*)\)|([^\s=]+))\s*=>/);

if (fnMatch) {
paramsSrc = fnMatch[1] ?? '';
} else if (arrowMatch) {
paramsSrc = arrowMatch[1] ?? arrowMatch[2] ?? '';
} else {
return [];
}

if (!paramsSrc) return [];

const params = [];
let current = '';
let depth = 0;

for (let i = 0; i < paramsSrc.length; i++) {
const ch = paramsSrc[i];

if (ch === ',' && depth === 0) {
params.push(current.trim());
current = '';
continue;
}

if (ch === '{' || ch === '[' || ch === '(') depth++;
if (ch === '}' || ch === ']' || ch === ')') depth--;

current += ch;
}

if (current.trim()) {
params.push(current.trim());
}

// remove default values: a = 1 → a
return params.map((p, i) => {
const cleaned = p.replace(/=.*/, '').trim();

// destructured parameter
if (
cleaned.startsWith('{') ||
cleaned.startsWith('[') ||
cleaned.startsWith('...{') ||
cleaned.startsWith('...[')
) {
return `arg_${i}`;
}

return cleaned;
});
}

function flatTree(
obj: { [key: string]: ActionCreator<Action<string>> },
namespace = '',
) {
if (!obj) return [];
let functions: ActionCreatorObject[] = [];
Object.keys(obj).forEach((key) => {
const prop = obj[key];
if (!prop) return;
if (typeof prop === 'function') {
functions.push({
name: namespace + (key || prop.name || 'anonymous'),
Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.