Skip to content

Commit 4adc6d8

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

35 files changed

+328
-230
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,4 +1077,6 @@ function handleThunkCaseReducerDefinition<State>(
10771077
})
10781078
}
10791079

1080-
function noop() {}
1080+
function noop() {
1081+
/** No-Op */
1082+
}

packages/toolkit/src/listenerMiddleware/index.ts

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

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { noop } from '@internal/tests/utils/helpers'
2+
import type { PayloadAction } from '@reduxjs/toolkit'
13
import {
24
configureStore,
35
createAction,
@@ -58,7 +60,6 @@ describe('Saga-style Effects Scenarios', () => {
5860
}
5961

6062
beforeAll(() => {
61-
const noop = () => {}
6263
vi.spyOn(console, 'error').mockImplementation(noop)
6364
})
6465

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/tests/utils/helpers'
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: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
import {
2+
listenerCancelled,
3+
listenerCompleted,
4+
} from '@internal/listenerMiddleware/exceptions'
5+
import type {
6+
AbortSignalWithReason,
7+
AddListenerOverloads,
8+
} from '@internal/listenerMiddleware/types'
9+
import { noop } from '@internal/tests/utils/helpers'
10+
import type {
11+
Action,
12+
ListenerEffect,
13+
ListenerEffectAPI,
14+
PayloadAction,
15+
TypedRemoveListener,
16+
TypedStartListening,
17+
UnknownAction,
18+
} from '@reduxjs/toolkit'
119
import {
220
TaskAbortError,
321
addListener,
@@ -10,28 +28,6 @@ import {
1028
removeListener,
1129
} from '@reduxjs/toolkit'
1230
import type { Mock } from 'vitest'
13-
import { vi } from 'vitest'
14-
15-
import type {
16-
Action,
17-
ListenerEffect,
18-
ListenerEffectAPI,
19-
PayloadAction,
20-
TypedAddListener,
21-
TypedRemoveListener,
22-
TypedStartListening,
23-
UnknownAction,
24-
} from '@reduxjs/toolkit'
25-
26-
import {
27-
listenerCancelled,
28-
listenerCompleted,
29-
} from '@internal/listenerMiddleware/exceptions'
30-
31-
import type {
32-
AbortSignalWithReason,
33-
AddListenerOverloads,
34-
} from '@internal/listenerMiddleware/types'
3531

3632
const middlewareApi = {
3733
getState: expect.any(Function),
@@ -51,8 +47,6 @@ const middlewareApi = {
5147
throwIfCancelled: expect.any(Function),
5248
}
5349

54-
const noop = () => {}
55-
5650
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
5751
export interface Deferred<T> extends Promise<T> {
5852
resolve(value?: T | PromiseLike<T>): void
@@ -176,7 +170,9 @@ describe('createListenerMiddleware', () => {
176170

177171
describe('Subscription and unsubscription', () => {
178172
test('directly subscribing', () => {
179-
const effect = vi.fn((_: TestAction1) => {})
173+
const effect = vi.fn((_: TestAction1) => {
174+
/** No-Op */
175+
})
180176

181177
startListening({
182178
actionCreator: testAction1,
@@ -194,7 +190,9 @@ describe('createListenerMiddleware', () => {
194190
})
195191

196192
test('stopListening returns true if an entry has been unsubscribed, false otherwise', () => {
197-
const effect = vi.fn((_: TestAction1) => {})
193+
const effect = vi.fn((_: TestAction1) => {
194+
/** No-Op */
195+
})
198196

199197
startListening({
200198
actionCreator: testAction1,
@@ -206,7 +204,9 @@ describe('createListenerMiddleware', () => {
206204
})
207205

208206
test('dispatch(removeListener({...})) returns true if an entry has been unsubscribed, false otherwise', () => {
209-
const effect = vi.fn((_: TestAction1) => {})
207+
const effect = vi.fn((_: TestAction1) => {
208+
/** No-Op */
209+
})
210210

211211
startListening({
212212
actionCreator: testAction1,
@@ -232,7 +232,9 @@ describe('createListenerMiddleware', () => {
232232
})
233233

234234
test('can subscribe with a string action type', () => {
235-
const effect = vi.fn((_: UnknownAction) => {})
235+
const effect = vi.fn((_: UnknownAction) => {
236+
/** No-Op */
237+
})
236238

237239
store.dispatch(
238240
addListener({
@@ -251,7 +253,9 @@ describe('createListenerMiddleware', () => {
251253
})
252254

253255
test('can subscribe with a matcher function', () => {
254-
const effect = vi.fn((_: UnknownAction) => {})
256+
const effect = vi.fn((_: UnknownAction) => {
257+
/** No-Op */
258+
})
255259

256260
const isAction1Or2 = isAnyOf(testAction1, testAction2)
257261

@@ -318,7 +322,9 @@ describe('createListenerMiddleware', () => {
318322
})
319323

320324
test('subscribing with the same listener will not make it trigger twice (like EventTarget.addEventListener())', () => {
321-
const effect = vi.fn((_: TestAction1) => {})
325+
const effect = vi.fn((_: TestAction1) => {
326+
/** No-Op */
327+
})
322328

323329
startListening({
324330
actionCreator: testAction1,
@@ -340,7 +346,9 @@ describe('createListenerMiddleware', () => {
340346
})
341347

342348
test('unsubscribing via callback', () => {
343-
const effect = vi.fn((_: TestAction1) => {})
349+
const effect = vi.fn((_: TestAction1) => {
350+
/** No-Op */
351+
})
344352

345353
const unsubscribe = startListening({
346354
actionCreator: testAction1,
@@ -356,7 +364,9 @@ describe('createListenerMiddleware', () => {
356364
})
357365

358366
test('directly unsubscribing', () => {
359-
const effect = vi.fn((_: TestAction1) => {})
367+
const effect = vi.fn((_: TestAction1) => {
368+
/** No-Op */
369+
})
360370

361371
startListening({
362372
actionCreator: testAction1,
@@ -377,7 +387,9 @@ describe('createListenerMiddleware', () => {
377387
})
378388

379389
test('subscribing via action', () => {
380-
const effect = vi.fn((_: TestAction1) => {})
390+
const effect = vi.fn((_: TestAction1) => {
391+
/** No-Op */
392+
})
381393

382394
store.dispatch(
383395
addListener({
@@ -397,7 +409,9 @@ describe('createListenerMiddleware', () => {
397409
})
398410

399411
test('unsubscribing via callback from dispatch', () => {
400-
const effect = vi.fn((_: TestAction1) => {})
412+
const effect = vi.fn((_: TestAction1) => {
413+
/** No-Op */
414+
})
401415

402416
const unsubscribe = store.dispatch(
403417
addListener({
@@ -416,7 +430,9 @@ describe('createListenerMiddleware', () => {
416430
})
417431

418432
test('unsubscribing via action', () => {
419-
const effect = vi.fn((_: TestAction1) => {})
433+
const effect = vi.fn((_: TestAction1) => {
434+
/** No-Op */
435+
})
420436

421437
startListening({
422438
actionCreator: testAction1,
@@ -636,7 +652,7 @@ describe('createListenerMiddleware', () => {
636652
let listenerCancelled = false
637653
let listenerStarted = false
638654
let listenerCompleted = false
639-
let cancelListener: () => void = () => {}
655+
let cancelListener: () => void = noop
640656
let error: TaskAbortError | undefined = undefined
641657

642658
startListening({
@@ -928,7 +944,9 @@ describe('createListenerMiddleware', () => {
928944
test('by default, actions are forwarded to the store', () => {
929945
reducer.mockClear()
930946

931-
const effect = vi.fn((_: TestAction1) => {})
947+
const effect = vi.fn((_: TestAction1) => {
948+
/** No-Op */
949+
})
932950

933951
startListening({
934952
actionCreator: testAction1,
@@ -993,7 +1011,7 @@ describe('createListenerMiddleware', () => {
9931011
},
9941012
})
9951013

996-
const effect = vi.fn(() => {})
1014+
const effect = vi.fn(noop)
9971015
startListening({ matcher, effect })
9981016

9991017
store.dispatch(testAction1('a'))
@@ -1002,8 +1020,8 @@ describe('createListenerMiddleware', () => {
10021020

10031021
test('Continues running other listeners if a predicate raises an error', () => {
10041022
const matcher = (action: any): action is any => true
1005-
const firstListener = vi.fn(() => {})
1006-
const secondListener = vi.fn(() => {})
1023+
const firstListener = vi.fn(noop)
1024+
const secondListener = vi.fn(noop)
10071025

10081026
startListening({
10091027
// @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
@@ -280,7 +280,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
280280
])
281281
// prevent uncaught promise rejections from happening.
282282
// if the original promise is used in any way, that will create a new promise that will throw again
283-
cacheDataLoaded.catch(() => {})
283+
cacheDataLoaded.catch(() => {
284+
/** No-Op */
285+
})
284286
lifecycleMap[queryCacheKey] = lifecycle
285287
const selector = (api.endpoints[endpointName] as any).select(
286288
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
@@ -228,7 +228,9 @@ export const buildQueryLifecycleHandler: InternalHandlerBuilder = ({
228228
})
229229
// prevent uncaught promise rejections from happening.
230230
// if the original promise is used in any way, that will create a new promise that will throw again
231-
queryFulfilled.catch(() => {})
231+
queryFulfilled.catch(() => {
232+
/** No-Op */
233+
})
232234
lifecycleMap[requestId] = lifecycle
233235
const selector = (api.endpoints[endpointName] as any).select(
234236
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<

0 commit comments

Comments
 (0)