-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unused exports & dead code (using Knip) #56817
Conversation
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
3d4a319
to
a05f5e6
Compare
a05f5e6
to
0ec385b
Compare
FWIW I had this exact change a couple of days ago, but decided not to send it because the benefit seemed small (as opposed to on DefinitelyTyped-tools where it found a lot of dead code and deps, and it's now part of CI). What would be more interesting IMO would be for knip to process |
We can fine-tune config however you like it. Knip does catch those
It's kind of the opposite: linting this codebase made me consider adding a The current config has Edit: |
The only entrypoints should be those passed to |
Oh well, then brace yourself... :) |
Following that, using this config: {
"$schema": "https://unpkg.com/knip@3/schema.json",
"entry": [
"Herebyfile.mjs",
// Knip does not understand Herebyfile.mjs (a new Knip plugin could probably handle it), so we add the entry files manually:
"src/cancellationToken/cancellationToken.ts",
"src/testRunner/_namespaces/Harness.ts",
"src/tsc/tsc.ts",
"src/tsserver/server.ts",
"src/typescript/typescript.ts",
"src/typingsInstaller/nodeTypingsInstaller.ts",
"src/watchGuard/watchGuard.ts",
// src/testRunner/tests.ts does not export anything, so we add all test files separately instead:
"src/testRunner/unittests/**/*.ts",
// The rest of the entry files, mostly to track used dependencies:
".eslintplugin.js",
".gulp.js",
"scripts/eslint/{rules,tests}/*.cjs",
"scripts/*.{cjs,mjs}"
],
"ignore": [
"src/lib/**",
"tests/**"
],
"ignoreDependencies": ["c8", "eslint-formatter-autolinkable-stylish", "mocha-fivemat-progress-reporter"],
"ignoreExportsUsedInFile": {
"enum": true,
"interface": true,
"type": true
}
} (output deleted, see next comment) |
0ec385b
to
3ffa43d
Compare
Did some more work on this, we're now at Knip v4.0.0-canary.5. The TS repo is great, because contains plenty of (challenging) grounds for Knip to cover. I expect the current usage/output to be fairly stable for v4. The original output for this PR is improved with a default run: Results for `knip`
It took me a few more iterations, but in the process Knip handles re-exports better and there's a Results for `knip --include-entry-exports --experimental-tags=+internal `
The previous comment ()previous iteration) I deleted (to reduce noise), here's the output with less false positives that should have been there: knip --include-entry-exports
Happy to receive any questions or feedback! |
TypeScript/src/compiler/perfLogger.ts Line 27 in f2e9ebd
|
It didn't look at |
This change doesn't include the |
Thanks for the PR! It looks like you've changed the TSServer protocol in some way. Please ensure that any changes here don't break consumers of the current TSServer API. For some extra review, we'll ping @sheetalkamat, @mjbvz, @zkat, and @joj for you. Feel free to loop in other consumers/maintainers if necessary. |
Pretty cool, I could just run this:
And then some Knip made 5 mistakes, I've put them back: 74a89a9 |
28e0334
to
5360570
Compare
Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page. Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up. |
Thanks, Jake! Just updated Knip, with less dependencies :) |
Agh, unfortunately I think the change to start not complaining about things referenced in other exported members may have been overly conservative. For example, if I restore this to
Then rerun knip, it doesn't complain even though it should. It seems like the analysis says "it's used via some other exported thing so ignore this too". But, it should only be considering that when a declaration is referenced from a position visible externally. This is also why it's saying that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking as needing changes (sorry) per the above so we don't accidentally merge this; the new change seems to more or less defeat most of the analysis for potentially-unexportable members.
# Conflicts: # package-lock.json # package.json # src/compiler/factory/emitHelpers.ts
I'm glad you did, in my enthusiasm I screwed up. Things should be good now. |
Nice, looking good! |
@@ -1752,8 +1751,10 @@ function tryResolveJSModuleWorker(moduleName: string, initialDir: string, host: | |||
/*conditions*/ undefined, | |||
); | |||
} | |||
|
|||
// knip applies the internal marker to _all_ declarations, not just the one overload. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be changed. If not now, something to change on knip side for future.
This PR took a while, but has made Knip itself so much better, it's really great. Thanks for bearing with me :) The TypeScript repo is also part of Knip's integration suite to prevent most nasty surprises/regressions: https://github.com/webpro-nl/knip/blob/main/.github/workflows/integration.yml#L101-L110 (I'll switch it to main) |
If you're looking for more work, I would also take a peek at https://npmgraph.js.org/?q=knip; shows a few deps that you may consider improving. It's a lot better than it was, though, so I don't think it's a problem here anymore. |
But obviously, thanks for all of your work here; going to do a final pass before we take things. |
Would def love to fix up deps. It's mainly the bash-parser, which unfortunately is pretty challenging to replace (tree-sitter is still not an option: tree-sitter/tree-sitter-bash#188). Also, I get that a repo like this one does not need it at all, but it's an important part of Knip nonetheless. Also, tbqh, looking for funding. |
Fixes #56818
When running Knip with a bit of config (see
knip.jsonc
) I get these results:There are a few false positives, but I think the results are still quite useful.
Then I ran
npx knip --fix
to remove theexport
keywords for unused exports, and then I went into the file to check it and remove the remaining dead code. Running the tests does not give issues.Let me know if I should remove Knip + config itself again, I totally understand you might not be eager to introduce new dependencies. Alternatively, I'm happy to extend the work until it reports no issues and then you can keep using this (in CI) it to prevent future regressions.