Skip to content

Commit

Permalink
Run Knip on 4.0 branch (#12291)
Browse files Browse the repository at this point in the history
* knip experiment

* some more config

* run `knip --fix` once

* more configuration, fix issues

* Remove deprecated `resetApolloContext` export

* add another exlusion

* run prettier

* some cleanup

* api-extractor

* config adjustments

* add CI job

* remove `isNullish`

* add a small integration test showing various re-export situations a wrapping library might use

* prettier
  • Loading branch information
phryneas authored Jan 27, 2025
1 parent 3a60a6f commit ae5d06a
Show file tree
Hide file tree
Showing 39 changed files with 602 additions and 324 deletions.
3 changes: 0 additions & 3 deletions .api-reports/api-report-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1991,9 +1991,6 @@ class RenderPromises {
// @public (undocumented)
type RequestHandler = (operation: Operation, forward: NextLink) => Observable<FetchResult> | null;

// @public @deprecated (undocumented)
export const resetApolloContext: typeof getApolloContext;

// @public (undocumented)
type ResetFunction = () => void;

Expand Down
3 changes: 0 additions & 3 deletions .api-reports/api-report-react_context.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1703,9 +1703,6 @@ class RenderPromises {
// @public (undocumented)
type RequestHandler = (operation: Operation, forward: NextLink) => Observable<FetchResult> | null;

// @public @deprecated (undocumented)
export const resetApolloContext: typeof getApolloContext;

// @public (undocumented)
type Resolver = (rootValue?: any, args?: any, context?: any, info?: {
field: FieldNode;
Expand Down
5 changes: 5 additions & 0 deletions .changeset/chatty-planes-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Remove deprecated `resetApolloContext` export
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ workflows:
- dual-module-test-standard
- vite
- vite-swc
- wrapping-library
# -browser-esm would need a package publish to npm/CDNs
react:
- latest
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/knip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Look for dead code and unused dependencies

on: push

jobs:
lint:
runs-on: ubuntu-latest
name: Ubuntu/Node v20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: Install dependencies (with cache)
uses: bahmutov/npm-install@v1
- name: Run knip
run: npm run knip
65 changes: 52 additions & 13 deletions integration-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"vite-swc",
"node-standard",
"node-esm",
"wrapping-library",
"shared"
],
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions integration-tests/wrapping-library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"devDependencies": {
"typescript": "^5.7.3"
},
"dependencies": {
"@apollo/client": "^3.12.7"
},
"scripts": {
"build": "tsc",
"test": "true"
}
}
29 changes: 29 additions & 0 deletions integration-tests/wrapping-library/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TypedDocumentNode, OperationVariables } from "@apollo/client";
import { useQuery } from "@apollo/client/react";
import { getMarkupFromTree } from "@apollo/client/react/ssr";
import { relayStylePagination } from "@apollo/client/utilities";
import { MockSubscriptionLink, MockLink } from "@apollo/client/testing";

/**
* This file simulates a library re-exporting functions that wrap Apollo Client
* functionality or exporting objects created with AC utilities.
*
* This is here to ensure that with the introduction of the `exports` field,
* TypeScript compilation errors on declaration emit are avoided.
*/

export function useWrappedQuery<TData, TVariables extends OperationVariables>(
query: TypedDocumentNode<TData, TVariables>
) {
return useQuery(query);
}

export const fieldPolicy = relayStylePagination();
export const mockSubscriptionLink = new MockSubscriptionLink();
export const mockLinkWrapper = (
...args: ConstructorParameters<typeof MockLink>
) => new MockLink(...args);

export const mockGetMarkupFromTree = (
...options: Parameters<typeof getMarkupFromTree>
) => getMarkupFromTree(...options);
18 changes: 18 additions & 0 deletions integration-tests/wrapping-library/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"sourceMap": true,
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["./src"],
"exclude": ["node_modules"]
}
66 changes: 66 additions & 0 deletions knip.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// @ts-check

import { join } from "node:path";
import { map } from "./config/entryPoints.js";
import { readFileSync } from "node:fs";

const packageJSON = JSON.parse(readFileSync("package.json", "utf-8"));

const packageEntries = map(({ dirs, bundleName = "index.ts" }) =>
join("src", ...dirs, bundleName)
);

const scriptEntries = Array.from(
Object.values(packageJSON.scripts)
.join("\n")
.matchAll(/(config\/.*?\.[jt]s)/g)
)
.map((match) => match[1])
.filter((value, index, arr) => index === arr.indexOf(value));

/** @type{import('knip').KnipConfig}*/
const config = {
exclude: ["optionalPeerDependencies", "unresolved"],
entry: []
.concat(packageEntries)
.concat(scriptEntries)
.concat([
"src/cache/inmemory/fixPolyfills.native.ts",
"src/react/types/types.documentation.ts",
"eslint-local-rules/index.js",
]),
project: ["src/**/*.ts", "config/*.[jt]s", "eslint-local-rules/*.[jt]s"],
ignore: ["integration-tests/**/*", ".yalc/**/*"],
ignoreBinaries: ["jq"],
ignoreDependencies: [
/@size-limit\/.*/,
/eslint-.*/,
// used by `recast`
"@babel/parser",
// called in a script, but with `xargs`
"tsx",
// TS types referenced by "rollup-plugin-terser"
"terser",
// used as a reporter by the `test:coverage` script
"jest-junit",
],
jest: {
config: "config/jest.config.js",
entry: [
"src/config/jest/setup.ts",
"src/testing/matchers/index.d.ts",
"**/__tests__/**/*.[jt]s?(x)",
"**/*.test.[jt]s?(x)",
"src/**/__benches__/*.bench.ts",
],
},
typescript: {
config: [
"tsconfig.json",
"src/tsconfig.json",
"eslint-local-rules/tsconfig.json",
],
},
};

export default config;
Loading

0 comments on commit ae5d06a

Please sign in to comment.