Skip to content

Commit 957782b

Browse files
committed
Fix problems related to the @typescript-eslint/no-empty-function rule
1 parent 3c36417 commit 957782b

33 files changed

+290
-214
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
@@ -325,7 +325,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
325325
])
326326
// prevent uncaught promise rejections from happening.
327327
// if the original promise is used in any way, that will create a new promise that will throw again
328-
cacheDataLoaded.catch(() => {})
328+
cacheDataLoaded.catch(() => {
329+
/** No-Op */
330+
})
329331
lifecycleMap[queryCacheKey] = lifecycle
330332
const selector = (api.endpoints[endpointName] as any).select(
331333
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
@@ -456,7 +456,9 @@ export const buildQueryLifecycleHandler: InternalHandlerBuilder = ({
456456
})
457457
// prevent uncaught promise rejections from happening.
458458
// if the original promise is used in any way, that will create a new promise that will throw again
459-
queryFulfilled.catch(() => {})
459+
queryFulfilled.catch(() => {
460+
/** No-Op */
461+
})
460462
lifecycleMap[requestId] = lifecycle
461463
const selector = (api.endpoints[endpointName] as any).select(
462464
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
@@ -152,11 +152,15 @@ const initialSubState: QuerySubState<any> = {
152152
// abuse immer to freeze default states
153153
const defaultQuerySubState = /* @__PURE__ */ createNextState(
154154
initialSubState,
155-
() => {},
155+
() => {
156+
/** No-Op */
157+
},
156158
)
157159
const defaultMutationSubState = /* @__PURE__ */ createNextState(
158160
initialSubState as MutationSubState<any>,
159-
() => {},
161+
() => {
162+
/** No-Op */
163+
},
160164
)
161165

162166
export type AllSelectors = ReturnType<typeof buildSelectors>

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

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,52 @@
1-
import type { Action, PayloadAction, UnknownAction } from '@reduxjs/toolkit'
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'
6+
import type {
7+
AssertTagTypes,
8+
EndpointDefinitions,
9+
FullTagDescription,
10+
QueryArgFrom,
11+
QueryDefinition,
12+
ResultTypeFrom,
13+
} from '../endpointDefinitions'
214
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'
15+
copyWithStructuralSharing,
16+
isDocumentVisible,
17+
isOnline,
18+
} from '../utils'
1419
import type {
15-
QuerySubstateIdentifier,
16-
QuerySubState,
17-
MutationSubstateIdentifier,
18-
MutationSubState,
20+
ConfigState,
21+
InvalidationState,
1922
MutationState,
23+
MutationSubState,
24+
MutationSubstateIdentifier,
25+
QueryCacheKey,
26+
QueryKeys,
2027
QueryState,
21-
InvalidationState,
28+
QuerySubState,
29+
QuerySubstateIdentifier,
2230
Subscribers,
23-
QueryCacheKey,
2431
SubscriptionState,
25-
ConfigState,
26-
QueryKeys,
27-
InfiniteQuerySubState,
28-
InfiniteQueryDirection,
2932
} from './apiState'
3033
import { QueryStatus } from './apiState'
31-
import type {
32-
AllQueryKeys,
33-
QueryArgFromAnyQueryDefinition,
34-
DataFromAnyQueryDefinition,
35-
InfiniteQueryThunk,
36-
MutationThunk,
37-
QueryThunk,
38-
QueryThunkArg,
39-
RejectedAction,
40-
} from './buildThunks'
34+
import { isUpsertQuery } from './buildInitiate'
35+
import type { MutationThunk, QueryThunk, QueryThunkArg } from './buildThunks'
4136
import { calculateProvidedByThunk } from './buildThunks'
4237
import {
43-
isInfiniteQueryDefinition,
44-
type AssertTagTypes,
45-
type DefinitionType,
46-
type EndpointDefinitions,
47-
type FullTagDescription,
48-
type QueryArgFrom,
49-
type QueryDefinition,
50-
type ResultTypeFrom,
51-
} from '../endpointDefinitions'
52-
import type { Patch } from 'immer'
53-
import { isDraft } from 'immer'
54-
import { applyPatches, original } from 'immer'
38+
SHOULD_AUTOBATCH,
39+
combineReducers,
40+
createAction,
41+
createNextState,
42+
createSlice,
43+
isAnyOf,
44+
isFulfilled,
45+
isRejectedWithValue,
46+
nanoid,
47+
prepareAutoBatched,
48+
} from './rtkImports'
5549
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
56-
import {
57-
isDocumentVisible,
58-
isOnline,
59-
copyWithStructuralSharing,
60-
} from '../utils'
61-
import type { ApiContext } from '../apiTypes'
62-
import { isUpsertQuery } from './buildInitiate'
63-
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
6450

6551
/**
6652
* A typesafe single entry to be upserted into the cache
@@ -608,7 +594,9 @@ export function buildSlice({
608594
) {
609595
// Dummy
610596
},
611-
internal_getRTKQSubscriptions() {},
597+
internal_getRTKQSubscriptions() {
598+
/** No-Op */
599+
},
612600
},
613601
})
614602

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)