From 0e1cb329885e4e47dd08da6d0e1784260fab6dd6 Mon Sep 17 00:00:00 2001 From: EskiMojo14 Date: Mon, 15 Jan 2024 22:50:57 +0000 Subject: [PATCH] Add creationStack, created when createSelector was first called --- src/createSelectorCreator.ts | 14 ++++++++++++-- src/devModeChecks/identityFunctionCheck.ts | 5 +++-- src/devModeChecks/inputStabilityCheck.ts | 6 ++++-- test/identityFunctionCheck.test.ts | 3 ++- test/inputStabilityCheck.spec.ts | 3 ++- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/createSelectorCreator.ts b/src/createSelectorCreator.ts index d98b86723..01f640aed 100644 --- a/src/createSelectorCreator.ts +++ b/src/createSelectorCreator.ts @@ -325,6 +325,14 @@ export function createSelectorCreator< > ] ) => { + let creationStack: string | undefined = undefined + if (process.env.NODE_ENV !== 'production') { + try { + throw new Error() + } catch (error) { + creationStack = (error as Error).stack + } + } let recomputations = 0 let dependencyRecomputations = 0 let lastResult: Result @@ -418,7 +426,8 @@ export function createSelectorCreator< identityFunctionCheck.run( resultFunc as Combiner, inputSelectorResults, - lastResult + lastResult, + creationStack ) } @@ -432,7 +441,8 @@ export function createSelectorCreator< inputStabilityCheck.run( { inputSelectorResults, inputSelectorResultsCopy }, { memoize, memoizeOptions: finalMemoizeOptions }, - arguments + arguments, + creationStack ) } diff --git a/src/devModeChecks/identityFunctionCheck.ts b/src/devModeChecks/identityFunctionCheck.ts index 487a8ece5..afc72b693 100644 --- a/src/devModeChecks/identityFunctionCheck.ts +++ b/src/devModeChecks/identityFunctionCheck.ts @@ -20,7 +20,8 @@ import type { AnyFunction } from '../types' export const runIdentityFunctionCheck = ( resultFunc: AnyFunction, inputSelectorsResults: unknown[], - outputSelectorResult: unknown + outputSelectorResult: unknown, + creationStack: string | undefined ) => { if ( inputSelectorsResults.length === 1 && @@ -46,7 +47,7 @@ export const runIdentityFunctionCheck = ( '\n`createSelector([state => state.todos], todos => todos)`' + '\nThis could lead to inefficient memoization and unnecessary re-renders.' + '\nEnsure transformation logic is in the result function, and extraction logic is in the input selectors.', - { stack } + { stack, creationStack } ) } } diff --git a/src/devModeChecks/inputStabilityCheck.ts b/src/devModeChecks/inputStabilityCheck.ts index 307056935..ed1f42858 100644 --- a/src/devModeChecks/inputStabilityCheck.ts +++ b/src/devModeChecks/inputStabilityCheck.ts @@ -25,7 +25,8 @@ export const runInputStabilityCheck = ( 'memoize' | 'memoizeOptions' > >, - inputSelectorArgs: unknown[] | IArguments + inputSelectorArgs: unknown[] | IArguments, + creationStack: string | undefined ) => { const { memoize, memoizeOptions } = options const { inputSelectorResults, inputSelectorResultsCopy } = @@ -52,7 +53,8 @@ export const runInputStabilityCheck = ( arguments: inputSelectorArgs, firstInputs: inputSelectorResults, secondInputs: inputSelectorResultsCopy, - stack + stack, + creationStack } ) } diff --git a/test/identityFunctionCheck.test.ts b/test/identityFunctionCheck.test.ts index fa7fd225e..27d4436bc 100644 --- a/test/identityFunctionCheck.test.ts +++ b/test/identityFunctionCheck.test.ts @@ -51,7 +51,8 @@ describe('identityFunctionCheck', () => { 'The result function returned its own inputs without modification' ), { - stack: expect.any(String) + stack: expect.any(String), + creationStack: expect.any(String) } ) }) diff --git a/test/inputStabilityCheck.spec.ts b/test/inputStabilityCheck.spec.ts index 92242f557..0b55ad581 100644 --- a/test/inputStabilityCheck.spec.ts +++ b/test/inputStabilityCheck.spec.ts @@ -42,7 +42,8 @@ describe('inputStabilityCheck', () => { secondInputs: expect.arrayContaining([ expect.objectContaining({ a: 1, b: 2 }) ]), - stack: expect.any(String) + stack: expect.any(String), + creationStack: expect.any(String) }) ) })