Skip to content

Commit 5d29f8f

Browse files
webproTkDodoDamianOsipiuk
authored
chore: introduce Knip as dead-code removal tool (#6368)
* Add knip with inital config * Rename vite.config.js → vite.config.js (`__dirname` not in ESM scope) * chore: upgrade to knip v3 * chore(knip): remove duplicate exports * chore(knip): remove unused types from query devtools * chore(knip): do not export unused types * chore(knip): run knip in ci * chore(knip): ignore certain .d.ts files * chore(knip): remove unused file * chore(knip): use devtools in next integration so the dependency doesn't show up as unused * chore(knip): remove unused dependencies * chore(knip): remove unused exports * chore(knip): add unlisted dependencies * chore(knip): remove unnecessary scope-manager dependency * Update knip to 3.3.0 * Update knip to 3.3.1 * chore: ignore custom deps, remove unneeded file * chore: remove unused util * chore: remove esbuild dep as it's not directly used * chore: bump tsup to match esbuild types with the plugin * chore: remove unused peer deps * chore: remove unneeded deps --------- Co-authored-by: Dominik Dorfmeister <[email protected]> Co-authored-by: Damian Osipiuk <[email protected]>
1 parent ac92fc9 commit 5d29f8f

File tree

33 files changed

+849
-813
lines changed

33 files changed

+849
-813
lines changed

.github/workflows/pr.yml

+21
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,24 @@ jobs:
100100
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
101101
- name: Run prettier
102102
run: pnpm run test:format
103+
knip:
104+
name: Knip
105+
runs-on: ubuntu-latest
106+
steps:
107+
- uses: actions/checkout@v4
108+
with:
109+
fetch-depth: 0
110+
- name: Setup pnpm
111+
uses: pnpm/action-setup@v2
112+
with:
113+
version: 8
114+
- name: Setup Node
115+
uses: actions/setup-node@v3
116+
with:
117+
node-version-file: .nvmrc
118+
cache: pnpm
119+
cache-dependency-path: pnpm-lock.yaml
120+
- name: Install dependencies
121+
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
122+
- name: Run Knip
123+
run: pnpm knip

integrations/react-cra4/src/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from '@tanstack/react-query'
22

3-
export const App = () => {
3+
const App = () => {
44
const query = useQuery({
55
queryKey: ['test'],
66
queryFn: async () => {

integrations/react-cra5/src/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from '@tanstack/react-query'
22

3-
export const App = () => {
3+
const App = () => {
44
const query = useQuery({
55
queryKey: ['test'],
66
queryFn: async () => {
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
'use client'
22
import * as React from 'react'
33
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
45

56
export default function Providers({ children }: { children: React.ReactNode }) {
67
const [queryClient] = React.useState(() => new QueryClient())
78

89
return (
9-
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
10+
<QueryClientProvider client={queryClient}>
11+
{children}
12+
<ReactQueryDevtools />
13+
</QueryClientProvider>
1014
)
1115
}

integrations/react-vite4/src/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from '@tanstack/react-query'
22

3-
export const App = () => {
3+
const App = () => {
44
const query = useQuery({
55
queryKey: ['test'],
66
queryFn: async () => {

integrations/react-vite5/src/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from '@tanstack/react-query'
22

3-
export const App = () => {
3+
const App = () => {
44
const query = useQuery({
55
queryKey: ['test'],
66
queryFn: async () => {

integrations/solid-vite/src/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Match, Switch } from 'solid-js'
22
import { createQuery } from '@tanstack/solid-query'
33

4-
export const App = () => {
4+
const App = () => {
55
const query = createQuery(() => ({
66
queryKey: ['test'],
77
queryFn: async () => {

knip.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// These aren't actual compilers, but performant & avoids root dependencies
2+
const svelteCompiler = (text: string) => text.matchAll(/import[^]*?'.*?'\n/gs)
3+
const vueCompiler = /<script\b[^>]*>([\s\S]*?)<\/script>/gm
4+
5+
export default {
6+
$schema: 'https://unpkg.com/knip@2/schema.json',
7+
ignoreWorkspaces: ['examples/**'],
8+
ignore: ['**/react-app-env.d.ts', '**/vite-env.d.ts'],
9+
workspaces: {
10+
'packages/codemods': {
11+
entry: ['src/v4/*.js', 'src/v5/*/*.js'],
12+
ignore: ['**/__testfixtures__/**'],
13+
},
14+
'packages/vue-query': {
15+
ignore: ['**/__mocks__/**'],
16+
ignoreDependencies: ['vue2', 'vue2.7'],
17+
},
18+
},
19+
compilers: {
20+
svelte: (text: string) => [...svelteCompiler(text)].join('\n'),
21+
vue: (text) => {
22+
const scripts = []
23+
let match
24+
while ((match = vueCompiler.exec(text))) scripts.push(match[1])
25+
return scripts.join(';')
26+
},
27+
},
28+
}

package.json

+3-8
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
"@solidjs/testing-library": "^0.5.1",
4242
"@testing-library/jest-dom": "^6.1.4",
4343
"@testing-library/react": "^14.0.0",
44-
"@testing-library/react-hooks": "^8.0.1",
45-
"@testing-library/user-event": "^14.5.1",
4644
"@types/current-git-branch": "^1.1.5",
4745
"@types/eslint": "^8.44.0",
4846
"@types/git-log-parser": "^1.2.2",
@@ -58,11 +56,9 @@
5856
"@vitest/coverage-istanbul": "^0.33.0",
5957
"axios": "^1.5.1",
6058
"chalk": "^5.3.0",
61-
"concurrently": "^8.2.2",
6259
"cpy-cli": "^5.0.0",
6360
"cross-env": "^7.0.3",
6461
"current-git-branch": "^1.1.0",
65-
"esbuild": "^0.18.20",
6662
"esbuild-plugin-file-path-extensions": "^1.0.0",
6763
"eslint": "^8.34.0",
6864
"eslint-config-prettier": "^8.8.0",
@@ -73,6 +69,7 @@
7369
"git-log-parser": "^1.2.0",
7470
"jsdom": "^22.1.0",
7571
"jsonfile": "^6.1.0",
72+
"knip": "^3.3.1",
7673
"luxon": "^3.4.3",
7774
"nx": "^16.5.2",
7875
"nx-cloud": "^16.5.2",
@@ -86,13 +83,11 @@
8683
"sherif": "^0.5.0",
8784
"solid-js": "^1.8.1",
8885
"stream-to-array": "^2.3.0",
89-
"ts-node": "^10.7.0",
90-
"tsup": "^7.2.0",
86+
"tsup": "^7.3.0",
9187
"type-fest": "^4.5.0",
9288
"typescript": "5.1.6",
9389
"vite": "^4.4.11",
94-
"vitest": "^0.33.0",
95-
"vue": "^3.3.0"
90+
"vitest": "^0.33.0"
9691
},
9792
"pnpm": {
9893
"overrides": {

packages/eslint-plugin-query/src/utils/ast-utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
22
import { uniqueBy } from './unique-by'
33
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
4-
import type TSESLintScopeManager from '@typescript-eslint/scope-manager'
54
import type { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint'
65

76
export const ASTUtils = {
@@ -143,7 +142,7 @@ export const ASTUtils = {
143142
},
144143
isDeclaredInNode(params: {
145144
functionNode: TSESTree.Node
146-
reference: TSESLintScopeManager.Reference
145+
reference: TSESLint.Scope.Reference
147146
scopeManager: TSESLint.Scope.ScopeManager
148147
}) {
149148
const { functionNode, reference, scopeManager } = params

packages/query-core/src/tests/utils.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { vi } from 'vitest'
2-
import { act } from '@testing-library/react'
32
import { QueryClient, onlineManager } from '..'
43
import * as utils from '../utils'
54
import type { SpyInstance } from 'vitest'
@@ -33,14 +32,6 @@ export function sleep(timeout: number): Promise<void> {
3332
})
3433
}
3534

36-
export function setActTimeout(fn: () => void, ms?: number) {
37-
return setTimeout(() => {
38-
act(() => {
39-
fn()
40-
})
41-
}, ms)
42-
}
43-
4435
export const executeMutation = <TVariables>(
4536
queryClient: QueryClient,
4637
options: MutationOptions<any, any, TVariables, any>,

packages/query-devtools/src/Devtools.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ const [selectedMutationId, setSelectedMutationId] = createSignal<number | null>(
106106
)
107107
const [panelWidth, setPanelWidth] = createSignal(0)
108108

109-
export const DevtoolsComponent: Component<QueryDevtoolsProps> = (props) => {
109+
export type DevtoolsComponentType = Component<QueryDevtoolsProps>
110+
111+
const DevtoolsComponent: DevtoolsComponentType = (props) => {
110112
const [localStore, setLocalStore] = createLocalStorage({
111113
prefix: 'TanstackQueryDevtools',
112114
})
@@ -133,7 +135,7 @@ export const DevtoolsComponent: Component<QueryDevtoolsProps> = (props) => {
133135

134136
export default DevtoolsComponent
135137

136-
export const Devtools: Component<DevtoolsPanelProps> = (props) => {
138+
const Devtools: Component<DevtoolsPanelProps> = (props) => {
137139
loadFonts()
138140

139141
const theme = useTheme()
@@ -265,7 +267,7 @@ export const Devtools: Component<DevtoolsPanelProps> = (props) => {
265267
)
266268
}
267269

268-
export const DevtoolsPanel: Component<DevtoolsPanelProps> = (props) => {
270+
const DevtoolsPanel: Component<DevtoolsPanelProps> = (props) => {
269271
const theme = useTheme()
270272
const styles = createMemo(() => {
271273
return theme() === 'dark' ? darkStyles : lightStyles
@@ -1037,7 +1039,7 @@ const ContentView: Component<DevtoolsPanelProps> = (props) => {
10371039
)
10381040
}
10391041

1040-
export const QueryRow: Component<{ query: Query }> = (props) => {
1042+
const QueryRow: Component<{ query: Query }> = (props) => {
10411043
const theme = useTheme()
10421044
const styles = createMemo(() => {
10431045
return theme() === 'dark' ? darkStyles : lightStyles
@@ -1137,7 +1139,7 @@ export const QueryRow: Component<{ query: Query }> = (props) => {
11371139
)
11381140
}
11391141

1140-
export const MutationRow: Component<{ mutation: Mutation }> = (props) => {
1142+
const MutationRow: Component<{ mutation: Mutation }> = (props) => {
11411143
const theme = useTheme()
11421144
const styles = createMemo(() => {
11431145
return theme() === 'dark' ? darkStyles : lightStyles
@@ -1245,7 +1247,7 @@ export const MutationRow: Component<{ mutation: Mutation }> = (props) => {
12451247
)
12461248
}
12471249

1248-
export const QueryStatusCount: Component = () => {
1250+
const QueryStatusCount: Component = () => {
12491251
const stale = createSubscribeToQueryCacheBatcher(
12501252
(queryCache) =>
12511253
queryCache()
@@ -1299,7 +1301,7 @@ export const QueryStatusCount: Component = () => {
12991301
)
13001302
}
13011303

1302-
export const MutationStatusCount: Component = () => {
1304+
const MutationStatusCount: Component = () => {
13031305
const success = createSubscribeToMutationCacheBatcher(
13041306
(mutationCache) =>
13051307
mutationCache()
@@ -1369,7 +1371,7 @@ export const MutationStatusCount: Component = () => {
13691371
)
13701372
}
13711373

1372-
export const QueryStatus: Component<QueryStatusProps> = (props) => {
1374+
const QueryStatus: Component<QueryStatusProps> = (props) => {
13731375
const theme = useTheme()
13741376
const styles = createMemo(() => {
13751377
return theme() === 'dark' ? darkStyles : lightStyles

packages/query-devtools/src/Explorer.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { Query } from '@tanstack/query-core'
2222
* @example
2323
* chunkArray(['a','b', 'c', 'd', 'e'], 2) // returns [['a','b'], ['c', 'd'], ['e']]
2424
*/
25-
export function chunkArray<T extends { label: string; value: unknown }>(
25+
function chunkArray<T extends { label: string; value: unknown }>(
2626
array: Array<T>,
2727
size: number,
2828
): Array<Array<T>> {

packages/query-devtools/src/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
QueryClient,
66
onlineManager as TonlineManager,
77
} from '@tanstack/query-core'
8-
import type { DevtoolsComponent } from './Devtools'
8+
import type { DevtoolsComponentType } from './Devtools'
99
import type {
1010
DevToolsErrorType,
1111
DevtoolsButtonPosition,
@@ -30,7 +30,7 @@ class TanstackQueryDevtools {
3030
#position: Signal<DevtoolsPosition | undefined>
3131
#initialIsOpen: Signal<boolean | undefined>
3232
#errorTypes: Signal<Array<DevToolsErrorType> | undefined>
33-
#Component: typeof DevtoolsComponent | undefined
33+
#Component: DevtoolsComponentType | undefined
3434
#dispose?: () => void
3535

3636
constructor(config: TanstackQueryDevtoolsConfig) {
@@ -86,7 +86,7 @@ class TanstackQueryDevtools {
8686
const [isOpen] = this.#initialIsOpen
8787
const [errors] = this.#errorTypes
8888
const [queryClient] = this.#client
89-
let Devtools: typeof DevtoolsComponent
89+
let Devtools: DevtoolsComponentType
9090

9191
if (this.#Component) {
9292
Devtools = this.#Component

packages/query-devtools/src/theme.ts

-12
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,3 @@ export const tokens = {
309309
tooltip: 1800,
310310
},
311311
}
312-
313-
export type ThemeConfigType = typeof tokens
314-
export type ThemeColorsAll = {
315-
[key in keyof ThemeConfigType['colors']]: key
316-
}[keyof ThemeConfigType['colors']]
317-
export type AtomicThemeColors =
318-
| 'white'
319-
| 'black'
320-
| 'transparent'
321-
| 'current'
322-
| 'inherit'
323-
export type ThemeColors = Exclude<ThemeColorsAll, AtomicThemeColors>

packages/query-devtools/src/utils.tsx

+2-11
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@ export function getQueryStatusLabel(query: Query) {
1515
: 'fresh'
1616
}
1717

18-
export const queryStatusLabels = [
19-
'fresh',
20-
'stale',
21-
'paused',
22-
'inactive',
23-
'fetching',
24-
] as const
25-
export type IQueryStatusLabel = (typeof queryStatusLabels)[number]
18+
type QueryStatusLabel = 'fresh' | 'stale' | 'paused' | 'inactive' | 'fetching'
2619

2720
export function getSidedProp<T extends string>(
2821
prop: T,
@@ -71,7 +64,7 @@ export function getMutationStatusColor({
7164
: 'gray'
7265
}
7366

74-
export function getQueryStatusColorByLabel(label: IQueryStatusLabel) {
67+
export function getQueryStatusColorByLabel(label: QueryStatusLabel) {
7568
return label === 'fresh'
7669
? 'green'
7770
: label === 'stale'
@@ -156,8 +149,6 @@ export const convertRemToPixels = (rem: number) => {
156149
return rem * parseFloat(getComputedStyle(document.documentElement).fontSize)
157150
}
158151

159-
export const convertPixelsToRem = (px: number) => px / convertRemToPixels(1)
160-
161152
export const getPreferredColorScheme = () => {
162153
const [colorScheme, setColorScheme] = createSignal<'light' | 'dark'>('dark')
163154

packages/react-query-devtools/package.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@
7171
"devDependencies": {
7272
"@tanstack/react-query": "workspace:*",
7373
"@types/react": "^18.2.31",
74-
"@types/react-dom": "^18.2.14",
75-
"react": "^18.2.0",
76-
"react-dom": "^18.2.0",
77-
"react-error-boundary": "^3.1.4"
74+
"react": "^18.2.0"
7875
},
7976
"peerDependencies": {
8077
"@tanstack/react-query": "workspace:^",
81-
"react": "^18.0.0",
82-
"react-dom": "^18.0.0"
78+
"react": "^18.0.0"
8379
}
8480
}

0 commit comments

Comments
 (0)