Skip to content

Commit f53d8b1

Browse files
authored
chore: eslint no-unnecessary-condition (#3344)
* chore(eslint): turn on no-unnecessary-condition rule * chore(eslint): turn on no-unnecessary-condition rule remove unnecessary optional chainings in tests * chore(eslint): keep the matchMedia check even though it can never be undefined, except in tests, and I couldn't figure out how to mock that properly * chore(eslint): remove unnecessary checks in devtools * chore(eslint): addEventListener should exist on window if window is defined, which is checked by isServer * chore(eslint): assign default object to options instead of re-assigning it. In any case, the optional chaining is unnecessary * chore(eslint): action.type should always be defined * chore(eslint): keep the fallback for console * chore(eslint): one rule always complains so up-casting false to boolean * chore(eslint): if we have a behaviour, we also have na onFetch * chore(eslint): parseFilters always returns an object as it falls back to an empty object internally, so the falsy check didn't do anything * chore(eslint): upcast previous result to be potentially undefined to make the optinal chains necessary * fix issues after updating to alpha
1 parent ead3d69 commit f53d8b1

19 files changed

+61
-63
lines changed

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"es6": true
1313
},
1414
"parserOptions": {
15+
"project": "./tsconfig.json",
1516
"sourceType": "module"
1617
},
1718
"rules": {
@@ -21,6 +22,7 @@
2122
"@typescript-eslint/no-empty-interface": "off",
2223
"@typescript-eslint/no-explicit-any": "off",
2324
"@typescript-eslint/no-non-null-assertion": "off",
25+
"@typescript-eslint/no-unnecessary-condition": "error",
2426
"@typescript-eslint/no-inferrable-types": [
2527
"error",
2628
{

src/broadcastQueryClient-experimental/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@ export function broadcastQueryClient({
2424
const queryCache = queryClient.getQueryCache()
2525

2626
queryClient.getQueryCache().subscribe(queryEvent => {
27-
if (transaction || !queryEvent?.query) {
27+
if (transaction) {
2828
return
2929
}
3030

3131
const {
3232
query: { queryHash, queryKey, state },
3333
} = queryEvent
3434

35-
if (
36-
queryEvent.type === 'updated' &&
37-
queryEvent.action?.type === 'success'
38-
) {
35+
if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {
3936
channel.postMessage({
4037
type: 'updated',
4138
queryHash,

src/core/focusManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class FocusManager extends Subscribable {
1414
constructor() {
1515
super()
1616
this.setup = onFocus => {
17-
if (!isServer && window?.addEventListener) {
17+
if (!isServer) {
1818
const listener = () => onFocus()
1919
// Listen to visibillitychange and focus
2020
window.addEventListener('visibilitychange', listener, false)

src/core/hydration.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ function defaultShouldDehydrateQuery(query: Query) {
7575

7676
export function dehydrate(
7777
client: QueryClient,
78-
options?: DehydrateOptions
78+
options: DehydrateOptions = {}
7979
): DehydratedState {
80-
options = options || {}
81-
8280
const mutations: DehydratedMutation[] = []
8381
const queries: DehydratedQuery[] = []
8482

85-
if (options?.dehydrateMutations !== false) {
83+
if (options.dehydrateMutations !== false) {
8684
const shouldDehydrateMutation =
8785
options.shouldDehydrateMutation || defaultShouldDehydrateMutation
8886

@@ -96,7 +94,7 @@ export function dehydrate(
9694
})
9795
}
9896

99-
if (options?.dehydrateQueries !== false) {
97+
if (options.dehydrateQueries !== false) {
10098
const shouldDehydrateQuery =
10199
options.shouldDehydrateQuery || defaultShouldDehydrateQuery
102100

@@ -125,7 +123,9 @@ export function hydrate(
125123
const mutationCache = client.getMutationCache()
126124
const queryCache = client.getQueryCache()
127125

126+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
128127
const mutations = (dehydratedState as DehydratedState).mutations || []
128+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
129129
const queries = (dehydratedState as DehydratedState).queries || []
130130

131131
mutations.forEach(dehydratedMutation => {

src/core/onlineManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class OnlineManager extends Subscribable {
1414
constructor() {
1515
super()
1616
this.setup = onOnline => {
17-
if (!isServer && window?.addEventListener) {
17+
if (!isServer) {
1818
const listener = () => onOnline()
1919
// Listen to online
2020
window.addEventListener('online', listener, false)

src/core/query.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,7 @@ export class Query<
420420

421421
addSignalProperty(context)
422422

423-
if (this.options.behavior?.onFetch) {
424-
this.options.behavior?.onFetch(context)
425-
}
423+
this.options.behavior?.onFetch(context)
426424

427425
// Store state in case the current fetch needs to be reverted
428426
this.revertState = this.state
@@ -463,7 +461,7 @@ export class Query<
463461
// Try to fetch the data
464462
this.retryer = createRetryer({
465463
fn: context.fetchFn as () => TData,
466-
abort: abortController?.abort?.bind(abortController),
464+
abort: abortController?.abort.bind(abortController),
467465
onSuccess: data => {
468466
if (typeof data === 'undefined') {
469467
onError(new Error('Query data cannot be undefined') as any)

src/core/queryClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ export class QueryClient {
267267
query.invalidate()
268268
})
269269

270-
if (filters?.refetchType === 'none') {
270+
if (filters.refetchType === 'none') {
271271
return Promise.resolve()
272272
}
273273
const refetchFilters: RefetchQueryFilters = {
274274
...filters,
275-
type: filters?.refetchType ?? filters?.type ?? 'active',
275+
type: filters.refetchType ?? filters.type ?? 'active',
276276
}
277277
return this.refetchQueries(refetchFilters, options)
278278
})
@@ -302,7 +302,7 @@ export class QueryClient {
302302
query.fetch(undefined, {
303303
...options,
304304
cancelRefetch: options?.cancelRefetch ?? true,
305-
meta: { refetchPage: filters?.refetchPage },
305+
meta: { refetchPage: filters.refetchPage },
306306
})
307307
)
308308
)

src/core/queryObserver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,9 @@ export class QueryObserver<
399399
): QueryObserverResult<TData, TError> {
400400
const prevQuery = this.currentQuery
401401
const prevOptions = this.options
402-
const prevResult = this.currentResult
402+
const prevResult = this.currentResult as
403+
| QueryObserverResult<TData, TError>
404+
| undefined
403405
const prevResultState = this.currentResultState
404406
const prevResultOptions = this.currentResultOptions
405407
const queryChange = query !== prevQuery
@@ -622,7 +624,9 @@ export class QueryObserver<
622624
return
623625
}
624626

625-
const prevQuery = this.currentQuery
627+
const prevQuery = this.currentQuery as
628+
| Query<TQueryFnData, TError, TQueryData, TQueryKey>
629+
| undefined
626630
this.currentQuery = query
627631
this.currentQueryInitialState = query.state
628632
this.previousQueryResult = this.currentResult

src/core/tests/focusManager.test.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { sleep } from '../utils'
22
import { FocusManager } from '../focusManager'
3+
import { setIsServer } from '../../reactjs/tests/utils'
34

45
describe('focusManager', () => {
56
let focusManager: FocusManager
67
beforeEach(() => {
8+
jest.resetModules()
79
focusManager = new FocusManager()
810
})
911

@@ -61,17 +63,14 @@ describe('focusManager', () => {
6163
globalThis.document = document
6264
})
6365

64-
test('cleanup should still be undefined if window.addEventListener is not defined', async () => {
65-
const { addEventListener } = globalThis.window
66-
67-
// @ts-expect-error
68-
globalThis.window.addEventListener = undefined
66+
test('cleanup should still be undefined if window is not defined', async () => {
67+
const restoreIsServer = setIsServer(true)
6968

7069
const unsubscribe = focusManager.subscribe(() => undefined)
7170
expect(focusManager['cleanup']).toBeUndefined()
7271

7372
unsubscribe()
74-
globalThis.window.addEventListener = addEventListener
73+
restoreIsServer()
7574
})
7675

7776
it('should replace default window listener when a new event listener is set', async () => {

src/core/tests/hydration.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ describe('dehydration and rehydration', () => {
196196

197197
// This is testing implementation details that can change and are not
198198
// part of the public API, but is important for keeping the payload small
199-
const dehydratedQuery = dehydrated?.queries.find(
200-
query => query?.queryKey[0] === 'string'
199+
const dehydratedQuery = dehydrated.queries.find(
200+
query => query.queryKey[0] === 'string'
201201
)
202202
expect(dehydratedQuery).toBeUndefined()
203203

0 commit comments

Comments
 (0)