Skip to content
Myr

fix(devtools): support Firefox/Safari stack format in findCallerName (#3469)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(devtools): support Firefox/Safari stack format in findCallerName The previous regex only matched V8-style frames ("at name (source)"). SpiderMonkey (Firefox) and JavaScriptCore (Safari) use "name@source" with no spaces, causing the regex to fail and fall back to "anonymous". Now tries V8 regex first, then Gecko/JSC regex. Regexes extracted to module-level constants to avoid recompilation on every setState call. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * fix(devtools): keep findCallerName internal; test Gecko stack via Error stub - Use function declaration without export so helpers stay private - Drop redundant || undefined after regex fallback chain - Replace direct findCallerName import with integration test that stubs Error.stack to SpiderMonkey shape and asserts devtools send payload - Avoids pulling src into old-TypeScript test:types and widening public API ---------

authored by Siniša Bolšec · committed by GitHub · parent 4b96f4e · Browse files · 2 files+63 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
  • Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

Diff stats

2 files+63 added−2 removed

  • tests:+53 added−0 removed
  • other:+10 added−2 removed

2 files+63 added−2 removed

modified:src/middleware/devtools.ts+10 added-2 removed
164 unmodified lines
165 trackedConnections.delete(name)166 }167}168169const findCallerName = (stack: string | undefined) => {169// V8 (Chrome/Edge/Node): "at <name> (<source>)"170const v8StackLineRe = /.+ (.+) .+/171// SpiderMonkey (Firefox) / JavaScriptCore (Safari): "<name>@<source>"172const geckoStackLineRe = /^([^@]+)@/173174function findCallerName(stack: string | undefined) {175 if (!stack) return undefined176 const traceLines = stack.split('\n')177 const apiSetStateLineIndex = traceLines.findIndex((traceLine) =>178 traceLine.includes('api.setState'),179 )180 if (apiSetStateLineIndex < 0) return undefined181 const callerLine = traceLines[apiSetStateLineIndex + 1]?.trim() || ''177 return /.+ (.+) .+/.exec(callerLine)?.[1]182 return (183 v8StackLineRe.exec(callerLine)?.[1] ||184 geckoStackLineRe.exec(callerLine)?.[1]185 )186}187188const devtoolsImpl: DevtoolsImpl =189 (fn, devtoolsOptions = {}) =>
249 unmodified lines
modified:tests/devtools.test.tsx+53 added
220 unmodified lines
221 { type: expect.stringMatching(/^(Object\.setCount|anonymous)$/) },222 { count: 10, setCount: expect.any(Function) },223 )224 })225226 it('infers caller name from Firefox/SpiderMonkey stack format', () => {227 const geckoStack = [228 'api.setState@http://localhost/devtools.ts:200:5',229 'setCount@http://localhost/app.ts:10:5',230 ].join('\n')231232 const OriginalError = Error233 function ErrorWithGeckoStack(234 ...args: ConstructorParameters<typeof OriginalError>235 ) {236 const err = new OriginalError(...args)237 Object.defineProperty(err, 'stack', {238 value: geckoStack,239 configurable: true,240 writable: true,241 })242 return err243 }244 ErrorWithGeckoStack.prototype = OriginalError.prototype245 globalThis.Error = ErrorWithGeckoStack as unknown as typeof Error246247 try {248 const options = {249 name: 'testOptionsName',250 enabled: true,251 }252253 const api = createStore<{254 count: number255 setCount: (count: number) => void256 }>()(257 devtools(258 (set) => ({259 count: 0,260 setCount: (newCount: number) => {261 set({ count: newCount })262 },263 }),264 options,265 ),266 )267268 api.getState().setCount(10)269 const [connection] = getNamedConnectionApis(options.name)270 expect(connection.send).toHaveBeenLastCalledWith(271 { type: 'setCount' },272 { count: 10, setCount: expect.any(Function) },273 )274 } finally {275 globalThis.Error = OriginalError276 }277 })278})279280describe('when it receives a message of type...', () => {281 describe('ACTION...', () => {
2314 unmodified lines