Skip to content

Commit 3b84c2b

Browse files
committed
Fix problems related to the @typescript-eslint/no-empty-function rule
1 parent 5b081a0 commit 3b84c2b

33 files changed

+304
-237
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,4 +1084,6 @@ function handleThunkCaseReducerDefinition<State>(
10841084
})
10851085
}
10861086

1087-
function noop() {}
1087+
function noop() {
1088+
/** No-Op */
1089+
}

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
@@ -331,7 +331,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
331331
])
332332
// prevent uncaught promise rejections from happening.
333333
// if the original promise is used in any way, that will create a new promise that will throw again
334-
cacheDataLoaded.catch(() => {})
334+
cacheDataLoaded.catch(() => {
335+
/** No-Op */
336+
})
335337
lifecycleMap[queryCacheKey] = lifecycle
336338
const selector = (api.endpoints[endpointName] as any).select(
337339
isAnyQueryDefinition(endpointDefinition) ? originalArgs : queryCacheKey,

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
isAnyQueryDefinition(endpointDefinition) ? originalArgs : requestId,

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,15 @@ const initialSubState: QuerySubState<any> = {
157157
// abuse immer to freeze default states
158158
const defaultQuerySubState = /* @__PURE__ */ createNextState(
159159
initialSubState,
160-
() => {},
160+
() => {
161+
/** No-Op */
162+
},
161163
)
162164
const defaultMutationSubState = /* @__PURE__ */ createNextState(
163165
initialSubState as MutationSubState<any>,
164-
() => {},
166+
() => {
167+
/** No-Op */
168+
},
165169
)
166170

167171
export type AllSelectors = ReturnType<typeof buildSelectors>

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

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
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+
QueryDefinition,
11+
} from '../endpointDefinitions'
12+
import { isInfiniteQueryDefinition } from '../endpointDefinitions'
13+
import type { UnwrapPromise } from '../tsHelpers'
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+
InfiniteQueryDirection,
22+
InfiniteQuerySubState,
23+
InvalidationState,
1924
MutationState,
25+
MutationSubState,
26+
MutationSubstateIdentifier,
27+
QueryCacheKey,
2028
QueryState,
21-
InvalidationState,
29+
QuerySubState,
30+
QuerySubstateIdentifier,
2231
Subscribers,
23-
QueryCacheKey,
2432
SubscriptionState,
25-
ConfigState,
26-
InfiniteQuerySubState,
27-
InfiniteQueryDirection,
2833
} from './apiState'
2934
import { QueryStatus } from './apiState'
35+
import { isUpsertQuery } from './buildInitiate'
3036
import type {
3137
AllQueryKeys,
32-
QueryArgFromAnyQueryDefinition,
3338
DataFromAnyQueryDefinition,
3439
InfiniteQueryThunk,
3540
MutationThunk,
41+
QueryArgFromAnyQueryDefinition,
3642
QueryThunk,
3743
QueryThunkArg,
3844
} from './buildThunks'
3945
import { calculateProvidedByThunk } from './buildThunks'
4046
import {
41-
isInfiniteQueryDefinition,
42-
type AssertTagTypes,
43-
type EndpointDefinitions,
44-
type FullTagDescription,
45-
type QueryDefinition,
46-
} from '../endpointDefinitions'
47-
import type { Patch } from 'immer'
48-
import { isDraft } from 'immer'
49-
import { applyPatches, original } from 'immer'
47+
SHOULD_AUTOBATCH,
48+
combineReducers,
49+
createAction,
50+
createNextState,
51+
createSlice,
52+
isAnyOf,
53+
isFulfilled,
54+
isRejectedWithValue,
55+
nanoid,
56+
prepareAutoBatched,
57+
} from './rtkImports'
5058
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
51-
import {
52-
isDocumentVisible,
53-
isOnline,
54-
copyWithStructuralSharing,
55-
} from '../utils'
56-
import type { ApiContext } from '../apiTypes'
57-
import { isUpsertQuery } from './buildInitiate'
58-
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
59-
import type { UnwrapPromise } from '../tsHelpers'
6059

6160
/**
6261
* A typesafe single entry to be upserted into the cache
@@ -88,7 +87,7 @@ export type ProcessedQueryUpsertEntry = {
8887
* A typesafe representation of a util action creator that accepts cache entry descriptions to upsert
8988
*/
9089
export type UpsertEntries<Definitions extends EndpointDefinitions> = (<
91-
EndpointNames extends Array<AllQueryKeys<Definitions>>,
90+
EndpointNames extends AllQueryKeys<Definitions>[],
9291
>(
9392
entries: [
9493
...{
@@ -649,7 +648,9 @@ export function buildSlice({
649648
) {
650649
// Dummy
651650
},
652-
internal_getRTKQSubscriptions() {},
651+
internal_getRTKQSubscriptions() {
652+
/** No-Op */
653+
},
653654
},
654655
})
655656

0 commit comments

Comments
 (0)