Skip to content

Commit 4c6eb85

Browse files
committed
Fix problems related to the @typescript-eslint/no-empty-function rule
1 parent db2065f commit 4c6eb85

33 files changed

+288
-205
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,4 +1072,6 @@ function handleThunkCaseReducerDefinition<State>(
10721072
})
10731073
}
10741074

1075-
function noop() {}
1075+
function noop() {
1076+
/** No-Op */
1077+
}

packages/toolkit/src/listenerMiddleware/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ const createTakePattern = <S>(
145145
validateActive(signal)
146146

147147
// Placeholder unsubscribe function until the listener is added
148-
let unsubscribe: UnsubscribeListener = () => {}
148+
let unsubscribe: UnsubscribeListener = () => {
149+
/** No-Op */
150+
}
149151

150152
const tuplePromise = new Promise<[Action, S, S]>((resolve, reject) => {
151153
// Inside the Promise, we synchronously add the listener.

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.test-d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createListenerEntry } from '@internal/listenerMiddleware'
2+
import { noop } from '@internal/listenerMiddleware/utils'
23
import type {
34
Action,
45
PayloadAction,
@@ -89,7 +90,7 @@ describe('type tests', () => {
8990
const unsubscribe = store.dispatch(
9091
addListener({
9192
actionCreator: testAction1,
92-
effect: () => {},
93+
effect: noop,
9394
}),
9495
)
9596

@@ -168,7 +169,9 @@ describe('type tests', () => {
168169

169170
return true
170171
},
171-
effect: (action, listenerApi) => {},
172+
effect: (action, listenerApi) => {
173+
/** No-Op */
174+
},
172175
})
173176

174177
startListening({

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.test.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ describe('createListenerMiddleware', () => {
177177

178178
describe('Subscription and unsubscription', () => {
179179
test('directly subscribing', () => {
180-
const effect = vi.fn((_: TestAction1) => {})
180+
const effect = vi.fn((_: TestAction1) => {
181+
/** No-Op */
182+
})
181183

182184
startListening({
183185
actionCreator: testAction1,
@@ -195,7 +197,9 @@ describe('createListenerMiddleware', () => {
195197
})
196198

197199
test('stopListening returns true if an entry has been unsubscribed, false otherwise', () => {
198-
const effect = vi.fn((_: TestAction1) => {})
200+
const effect = vi.fn((_: TestAction1) => {
201+
/** No-Op */
202+
})
199203

200204
startListening({
201205
actionCreator: testAction1,
@@ -207,7 +211,9 @@ describe('createListenerMiddleware', () => {
207211
})
208212

209213
test('dispatch(removeListener({...})) returns true if an entry has been unsubscribed, false otherwise', () => {
210-
const effect = vi.fn((_: TestAction1) => {})
214+
const effect = vi.fn((_: TestAction1) => {
215+
/** No-Op */
216+
})
211217

212218
startListening({
213219
actionCreator: testAction1,
@@ -233,7 +239,9 @@ describe('createListenerMiddleware', () => {
233239
})
234240

235241
test('can subscribe with a string action type', () => {
236-
const effect = vi.fn((_: UnknownAction) => {})
242+
const effect = vi.fn((_: UnknownAction) => {
243+
/** No-Op */
244+
})
237245

238246
store.dispatch(
239247
addListener({
@@ -252,7 +260,9 @@ describe('createListenerMiddleware', () => {
252260
})
253261

254262
test('can subscribe with a matcher function', () => {
255-
const effect = vi.fn((_: UnknownAction) => {})
263+
const effect = vi.fn((_: UnknownAction) => {
264+
/** No-Op */
265+
})
256266

257267
const isAction1Or2 = isAnyOf(testAction1, testAction2)
258268

@@ -319,7 +329,9 @@ describe('createListenerMiddleware', () => {
319329
})
320330

321331
test('subscribing with the same listener will not make it trigger twice (like EventTarget.addEventListener())', () => {
322-
const effect = vi.fn((_: TestAction1) => {})
332+
const effect = vi.fn((_: TestAction1) => {
333+
/** No-Op */
334+
})
323335

324336
startListening({
325337
actionCreator: testAction1,
@@ -341,7 +353,9 @@ describe('createListenerMiddleware', () => {
341353
})
342354

343355
test('subscribing with the same effect but different predicate is allowed', () => {
344-
const effect = vi.fn((_: TestAction1 | TestAction2) => {})
356+
const effect = vi.fn((_: TestAction1 | TestAction2) => {
357+
/** No-Op */
358+
})
345359

346360
startListening({
347361
actionCreator: testAction1,
@@ -362,7 +376,9 @@ describe('createListenerMiddleware', () => {
362376
})
363377

364378
test('unsubscribing via callback', () => {
365-
const effect = vi.fn((_: TestAction1) => {})
379+
const effect = vi.fn((_: TestAction1) => {
380+
/** No-Op */
381+
})
366382

367383
const unsubscribe = startListening({
368384
actionCreator: testAction1,
@@ -378,7 +394,9 @@ describe('createListenerMiddleware', () => {
378394
})
379395

380396
test('directly unsubscribing', () => {
381-
const effect = vi.fn((_: TestAction1) => {})
397+
const effect = vi.fn((_: TestAction1) => {
398+
/** No-Op */
399+
})
382400

383401
startListening({
384402
actionCreator: testAction1,
@@ -399,7 +417,9 @@ describe('createListenerMiddleware', () => {
399417
})
400418

401419
test('subscribing via action', () => {
402-
const effect = vi.fn((_: TestAction1) => {})
420+
const effect = vi.fn((_: TestAction1) => {
421+
/** No-Op */
422+
})
403423

404424
store.dispatch(
405425
addListener({
@@ -419,7 +439,9 @@ describe('createListenerMiddleware', () => {
419439
})
420440

421441
test('unsubscribing via callback from dispatch', () => {
422-
const effect = vi.fn((_: TestAction1) => {})
442+
const effect = vi.fn((_: TestAction1) => {
443+
/** No-Op */
444+
})
423445

424446
const unsubscribe = store.dispatch(
425447
addListener({
@@ -438,7 +460,9 @@ describe('createListenerMiddleware', () => {
438460
})
439461

440462
test('unsubscribing via action', () => {
441-
const effect = vi.fn((_: TestAction1) => {})
463+
const effect = vi.fn((_: TestAction1) => {
464+
/** No-Op */
465+
})
442466

443467
startListening({
444468
actionCreator: testAction1,
@@ -658,7 +682,7 @@ describe('createListenerMiddleware', () => {
658682
let listenerCancelled = false
659683
let listenerStarted = false
660684
let listenerCompleted = false
661-
let cancelListener: () => void = () => {}
685+
let cancelListener: () => void = noop
662686
let error: TaskAbortError | undefined = undefined
663687

664688
startListening({
@@ -950,7 +974,9 @@ describe('createListenerMiddleware', () => {
950974
test('by default, actions are forwarded to the store', () => {
951975
reducer.mockClear()
952976

953-
const effect = vi.fn((_: TestAction1) => {})
977+
const effect = vi.fn((_: TestAction1) => {
978+
/** No-Op */
979+
})
954980

955981
startListening({
956982
actionCreator: testAction1,
@@ -1015,7 +1041,7 @@ describe('createListenerMiddleware', () => {
10151041
},
10161042
})
10171043

1018-
const effect = vi.fn(() => {})
1044+
const effect = vi.fn(noop)
10191045
startListening({ matcher, effect })
10201046

10211047
store.dispatch(testAction1('a'))
@@ -1024,8 +1050,8 @@ describe('createListenerMiddleware', () => {
10241050

10251051
test('Continues running other listeners if a predicate raises an error', () => {
10261052
const matcher = (action: any): action is any => true
1027-
const firstListener = vi.fn(() => {})
1028-
const secondListener = vi.fn(() => {})
1053+
const firstListener = vi.fn(noop)
1054+
const secondListener = vi.fn(noop)
10291055

10301056
startListening({
10311057
// @ts-expect-error

packages/toolkit/src/listenerMiddleware/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const assertFunction: (
1212
}
1313
}
1414

15-
export const noop = () => {}
15+
export const noop = () => {
16+
/** No-Op */
17+
}
1618

1719
export const catchRejection = <T>(
1820
promise: Promise<T>,

packages/toolkit/src/query/core/buildMiddleware/cacheLifecycle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
312312
])
313313
// prevent uncaught promise rejections from happening.
314314
// if the original promise is used in any way, that will create a new promise that will throw again
315-
cacheDataLoaded.catch(() => {})
315+
cacheDataLoaded.catch(() => {
316+
/** No-Op */
317+
})
316318
lifecycleMap[queryCacheKey] = lifecycle
317319
const selector = (api.endpoints[endpointName] as any).select(
318320
endpointDefinition.type === DefinitionType.query

packages/toolkit/src/query/core/buildMiddleware/queryLifecycle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ export const buildQueryLifecycleHandler: InternalHandlerBuilder = ({
444444
})
445445
// prevent uncaught promise rejections from happening.
446446
// if the original promise is used in any way, that will create a new promise that will throw again
447-
queryFulfilled.catch(() => {})
447+
queryFulfilled.catch(() => {
448+
/** No-Op */
449+
})
448450
lifecycleMap[requestId] = lifecycle
449451
const selector = (api.endpoints[endpointName] as any).select(
450452
endpointDefinition.type === DefinitionType.query

packages/toolkit/src/query/core/buildSelectors.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ const initialSubState: QuerySubState<any> = {
109109
// abuse immer to freeze default states
110110
const defaultQuerySubState = /* @__PURE__ */ createNextState(
111111
initialSubState,
112-
() => {},
112+
() => {
113+
/** No-Op */
114+
},
113115
)
114116
const defaultMutationSubState = /* @__PURE__ */ createNextState(
115117
initialSubState as MutationSubState<any>,
116-
() => {},
118+
() => {
119+
/** No-Op */
120+
},
117121
)
118122

119123
export function buildSelectors<

packages/toolkit/src/query/core/buildSlice.ts

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,52 @@
1-
import type { Action, PayloadAction, UnknownAction } from '@reduxjs/toolkit'
2-
import {
3-
combineReducers,
4-
createAction,
5-
createSlice,
6-
isAnyOf,
7-
isFulfilled,
8-
isRejectedWithValue,
9-
createNextState,
10-
prepareAutoBatched,
11-
SHOULD_AUTOBATCH,
12-
nanoid,
13-
} from './rtkImports'
14-
import type {
15-
QuerySubstateIdentifier,
16-
QuerySubState,
17-
MutationSubstateIdentifier,
18-
MutationSubState,
19-
MutationState,
20-
QueryState,
21-
InvalidationState,
22-
Subscribers,
23-
QueryCacheKey,
24-
SubscriptionState,
25-
ConfigState,
26-
QueryKeys,
27-
} from './apiState'
28-
import { QueryStatus } from './apiState'
29-
import type {
30-
MutationThunk,
31-
QueryThunk,
32-
QueryThunkArg,
33-
RejectedAction,
34-
} from './buildThunks'
35-
import { calculateProvidedByThunk } from './buildThunks'
1+
import type { PayloadAction } from '@reduxjs/toolkit'
2+
import type { Patch } from 'immer'
3+
import { applyPatches, isDraft, original } from 'immer'
4+
import type { ApiContext } from '../apiTypes'
5+
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
366
import type {
377
AssertTagTypes,
38-
DefinitionType,
398
EndpointDefinitions,
409
FullTagDescription,
4110
QueryArgFrom,
4211
QueryDefinition,
4312
ResultTypeFrom,
4413
} from '../endpointDefinitions'
45-
import type { Patch } from 'immer'
46-
import { isDraft } from 'immer'
47-
import { applyPatches, original } from 'immer'
48-
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
4914
import {
15+
copyWithStructuralSharing,
5016
isDocumentVisible,
5117
isOnline,
52-
copyWithStructuralSharing,
5318
} from '../utils'
54-
import type { ApiContext } from '../apiTypes'
19+
import type {
20+
ConfigState,
21+
InvalidationState,
22+
MutationState,
23+
MutationSubState,
24+
MutationSubstateIdentifier,
25+
QueryCacheKey,
26+
QueryKeys,
27+
QueryState,
28+
QuerySubState,
29+
QuerySubstateIdentifier,
30+
Subscribers,
31+
SubscriptionState,
32+
} from './apiState'
33+
import { QueryStatus } from './apiState'
5534
import { isUpsertQuery } from './buildInitiate'
56-
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
35+
import type { MutationThunk, QueryThunk, QueryThunkArg } from './buildThunks'
36+
import { calculateProvidedByThunk } from './buildThunks'
37+
import {
38+
SHOULD_AUTOBATCH,
39+
combineReducers,
40+
createAction,
41+
createNextState,
42+
createSlice,
43+
isAnyOf,
44+
isFulfilled,
45+
isRejectedWithValue,
46+
nanoid,
47+
prepareAutoBatched,
48+
} from './rtkImports'
49+
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
5750

5851
/**
5952
* A typesafe single entry to be upserted into the cache
@@ -595,7 +588,9 @@ export function buildSlice({
595588
) {
596589
// Dummy
597590
},
598-
internal_getRTKQSubscriptions() {},
591+
internal_getRTKQSubscriptions() {
592+
/** No-Op */
593+
},
599594
},
600595
})
601596

packages/toolkit/src/query/tests/apiProvider.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { noop } from '@internal/listenerMiddleware/utils'
12
import { configureStore } from '@reduxjs/toolkit'
23
import {
34
ApiProvider,
@@ -77,7 +78,7 @@ describe('ApiProvider', () => {
7778
})
7879
test('ApiProvider throws if nested inside a Redux context', () => {
7980
// Intentionally swallow the "unhandled error" message
80-
vi.spyOn(console, 'error').mockImplementation(() => {})
81+
vi.spyOn(console, 'error').mockImplementation(noop)
8182
expect(() =>
8283
render(
8384
<Provider store={configureStore({ reducer: () => null })}>

0 commit comments

Comments
 (0)