Skip to content

Commit 40e5777

Browse files
committed
Use Knip to detect dead code
1 parent 34dae0d commit 40e5777

File tree

11 files changed

+301
-19
lines changed

11 files changed

+301
-19
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extends:
44
- prettier/react
55

66
plugins:
7+
- import
78
- prettier
89

910
globals:

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
- name: generate file system-based packs
7272
run: cd spec/dummy && RAILS_ENV=test bundle exec rake react_on_rails:generate_packs
7373
- name: Build test bundles for dummy app
74-
run: cd spec/dummy && rm -rf public/webpack/test && yarn build:rescript && RAILS_ENV=test NODE_ENV=test bin/${{ matrix.versions == 'oldest' && 'web' || 'shaka' }}packer
74+
run: cd spec/dummy && rm -rf public/webpack/test && yarn run build:rescript && RAILS_ENV=test NODE_ENV=test bin/${{ matrix.versions == 'oldest' && 'web' || 'shaka' }}packer
7575
- id: get-sha
7676
run: echo "::set-output name=sha::$(git rev-parse HEAD)"
7777
- name: Save test webpack bundles to cache (for build number checksum used by rspec job)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
react_on_rails (14.1.0)
4+
react_on_rails (14.1.1)
55
addressable
66
connection_pool
77
execjs (~> 2.5)

knip.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { KnipConfig } from 'knip';
2+
3+
const config: KnipConfig = {
4+
// ! at the end means files are used in production
5+
entry: ['node_package/src/ReactOnRails.ts!', 'node_package/src/ReactOnRails.node.ts!'],
6+
project: ['node_package/src/**/*.[jt]s!', 'node_package/tests/**/*.[jt]s'],
7+
ignoreBinaries: [
8+
// Knip fails to detect it's declared in devDependencies
9+
'nps',
10+
// local scripts
11+
'node_package/scripts/.*',
12+
],
13+
ignoreDependencies: [
14+
// Required for TypeScript compilation, but we don't depend on Turbolinks itself.
15+
'@types/turbolinks',
16+
// used in package-scripts.yml
17+
'concurrently',
18+
],
19+
babel: {
20+
config: ['node_package/babel.config.js', 'package.json'],
21+
},
22+
};
23+
24+
export default config;

node_package/src/buildConsoleReplay.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare global {
99
}
1010
}
1111

12+
/** @internal Exported only for tests */
1213
export function consoleReplay(customConsoleHistory: typeof console['history'] | undefined = undefined, numberOfMessagesToSkip: number = 0): string {
1314
// console.history is a global polyfill used in server rendering.
1415
const consoleHistory = customConsoleHistory ?? console.history;

node_package/src/clientStartup.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
RenderFunction,
88
Root,
99
} from './types';
10+
import type { Context } from './context';
1011

1112
import createReactOutput from './createReactOutput';
1213
import { isServerRenderHash } from './isServerRenderResult';
@@ -22,12 +23,11 @@ declare global {
2223
roots: Root[];
2324
}
2425

25-
namespace NodeJS {
26-
interface Global {
27-
ReactOnRails: ReactOnRailsType;
28-
roots: Root[];
29-
}
26+
namespace globalThis {
27+
var ReactOnRails: ReactOnRailsType;
28+
var roots: Root[];
3029
}
30+
3131
namespace Turbolinks {
3232
interface TurbolinksStatic {
3333
controller?: unknown;
@@ -39,8 +39,6 @@ declare const ReactOnRails: ReactOnRailsType;
3939

4040
const REACT_ON_RAILS_STORE_ATTRIBUTE = 'data-js-react-on-rails-store';
4141

42-
type Context = Window | NodeJS.Global;
43-
4442
function findContext(): Context {
4543
if (typeof window.ReactOnRails !== 'undefined') {
4644
return window;

node_package/src/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
export type Context = Window | typeof globalThis;
2+
13
/**
24
* Get the context, be it window or global
3-
* @returns {boolean|Window|*|context}
45
*/
5-
export default function context(this: void): Window | NodeJS.Global | void {
6+
export default function context(this: void): Context | void {
67
return ((typeof window !== 'undefined') && window) ||
78
((typeof global !== 'undefined') && global) ||
89
this;

node_package/src/reactHydrateOrRender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ if (supportsRootApi) {
2323
}
2424
}
2525

26-
export const reactHydrate: HydrateOrRenderType = supportsRootApi ?
26+
const reactHydrate: HydrateOrRenderType = supportsRootApi ?
2727
reactDomClient.hydrateRoot :
2828
(domNode, reactElement) => ReactDOM.hydrate(reactElement, domNode);
2929

30-
export function reactRender(domNode: Element, reactElement: ReactElement): RenderReturnType {
30+
function reactRender(domNode: Element, reactElement: ReactElement): RenderReturnType {
3131
if (supportsRootApi) {
3232
const root = reactDomClient.createRoot(domNode);
3333
root.render(reactElement);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@babel/preset-react": "^7.18.6",
2121
"@babel/types": "^7.20.7",
2222
"@types/jest": "^29.5.14",
23+
"@types/node": "^20.17.16",
2324
"@types/react": "^18.3.18",
2425
"@types/react-dom": "^18.3.5",
2526
"@types/turbolinks": "^5.2.2",
@@ -38,6 +39,7 @@
3839
"jest": "^29.7.0",
3940
"jest-environment-jsdom": "^29.7.0",
4041
"jsdom": "^22.1.0",
42+
"knip": "^5.43.1",
4143
"nps": "^5.9.3",
4244
"prettier": "^2.8.8",
4345
"prettier-eslint-cli": "^5.0.0",
@@ -73,7 +75,8 @@
7375
"type-check": "yarn run tsc --noEmit --noErrorTruncation",
7476
"release:patch": "node_package/scripts/release patch",
7577
"release:minor": "node_package/scripts/release minor",
76-
"release:major": "node_package/scripts/release major"
78+
"release:major": "node_package/scripts/release major",
79+
"knip": "knip"
7780
},
7881
"repository": {
7982
"type": "git",

script/convert

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ File.rename(old_config, new_config)
1515

1616
gsub_file_content("../Gemfile.development_dependencies", 'gem "shakapacker", "8.0.0"', 'gem "shakapacker", "6.6.0"')
1717

18+
# Knip doesn't work on the oldest supported Node version and isn't needed there anyway
19+
gsub_file_content("../package.json", /"knip": "[^"]*",/, "")
20+
1821
gsub_file_content("../spec/dummy/package.json", '"shakapacker": "8.0.0",', '"shakapacker": "6.6.0",')
1922

2023
gsub_file_content("../spec/dummy/config/webpack/commonWebpackConfig.js", /generateWebpackConfig(\(\))?/,

0 commit comments

Comments
 (0)