Skip to content

Commit 0e1cb32

Browse files
committed
Add creationStack, created when createSelector was first called
1 parent bcfc3cd commit 0e1cb32

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

src/createSelectorCreator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ export function createSelectorCreator<
325325
>
326326
]
327327
) => {
328+
let creationStack: string | undefined = undefined
329+
if (process.env.NODE_ENV !== 'production') {
330+
try {
331+
throw new Error()
332+
} catch (error) {
333+
creationStack = (error as Error).stack
334+
}
335+
}
328336
let recomputations = 0
329337
let dependencyRecomputations = 0
330338
let lastResult: Result
@@ -418,7 +426,8 @@ export function createSelectorCreator<
418426
identityFunctionCheck.run(
419427
resultFunc as Combiner<InputSelectors, Result>,
420428
inputSelectorResults,
421-
lastResult
429+
lastResult,
430+
creationStack
422431
)
423432
}
424433

@@ -432,7 +441,8 @@ export function createSelectorCreator<
432441
inputStabilityCheck.run(
433442
{ inputSelectorResults, inputSelectorResultsCopy },
434443
{ memoize, memoizeOptions: finalMemoizeOptions },
435-
arguments
444+
arguments,
445+
creationStack
436446
)
437447
}
438448

src/devModeChecks/identityFunctionCheck.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import type { AnyFunction } from '../types'
2020
export const runIdentityFunctionCheck = (
2121
resultFunc: AnyFunction,
2222
inputSelectorsResults: unknown[],
23-
outputSelectorResult: unknown
23+
outputSelectorResult: unknown,
24+
creationStack: string | undefined
2425
) => {
2526
if (
2627
inputSelectorsResults.length === 1 &&
@@ -46,7 +47,7 @@ export const runIdentityFunctionCheck = (
4647
'\n`createSelector([state => state.todos], todos => todos)`' +
4748
'\nThis could lead to inefficient memoization and unnecessary re-renders.' +
4849
'\nEnsure transformation logic is in the result function, and extraction logic is in the input selectors.',
49-
{ stack }
50+
{ stack, creationStack }
5051
)
5152
}
5253
}

src/devModeChecks/inputStabilityCheck.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const runInputStabilityCheck = (
2525
'memoize' | 'memoizeOptions'
2626
>
2727
>,
28-
inputSelectorArgs: unknown[] | IArguments
28+
inputSelectorArgs: unknown[] | IArguments,
29+
creationStack: string | undefined
2930
) => {
3031
const { memoize, memoizeOptions } = options
3132
const { inputSelectorResults, inputSelectorResultsCopy } =
@@ -52,7 +53,8 @@ export const runInputStabilityCheck = (
5253
arguments: inputSelectorArgs,
5354
firstInputs: inputSelectorResults,
5455
secondInputs: inputSelectorResultsCopy,
55-
stack
56+
stack,
57+
creationStack
5658
}
5759
)
5860
}

test/identityFunctionCheck.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ describe('identityFunctionCheck', () => {
5151
'The result function returned its own inputs without modification'
5252
),
5353
{
54-
stack: expect.any(String)
54+
stack: expect.any(String),
55+
creationStack: expect.any(String)
5556
}
5657
)
5758
})

test/inputStabilityCheck.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ describe('inputStabilityCheck', () => {
4242
secondInputs: expect.arrayContaining([
4343
expect.objectContaining({ a: 1, b: 2 })
4444
]),
45-
stack: expect.any(String)
45+
stack: expect.any(String),
46+
creationStack: expect.any(String)
4647
})
4748
)
4849
})

0 commit comments

Comments
 (0)