Add script that finds unused frontend production exports
What does this MR do and why?
This MR adds scripts/frontend/check_unused_production_code.mjs.
For the modules you give it, the script lists the exports that no production code uses.
It also lists the specs that import each of those exports, so you can delete the tests in the same change.
dependency-cruiser already finds modules that nothing imports.
It does not find the unused exports inside a module that is still used.
Shared modules such as lib/utils/common_utils.js collect those exports when their last caller goes away.
The script asks knip every question, through knip/session, the API behind the knip language server.
Knip runs with the webpack entry points as its only entries, so a knip finding means "unused in production".
!261115 (merged) already added knip for spec code.
This is not a CI check. Application code can land before the feature that uses it, so a finding is a candidate to review, not an error.
Example, for lib/utils/common_utils.js:
app/assets/javascripts/lib/utils/common_utils.js: 10 of 55 exports unused in production
remove convertArrayOfObjectsToCamelCase delete it and its tests
spec spec/frontend/lib/utils/common_utils_spec.js
remove debounceByAnimationFrame delete it and its tests
spec spec/frontend/lib/utils/common_utils_spec.js
remove hasCiConfigExtension delete it and its tests
spec spec/frontend/lib/utils/common_utils_spec.js
remove isDefaultCiConfig delete it and its tests
spec spec/frontend/lib/utils/common_utils_spec.js
remove isInWorkItemPage delete it and its tests
unexport BACKOFF_TIMEOUT remove `export`, the module uses it itself
unexport checkPageAndAction remove `export`, the module uses it itself
unexport convertObjectProps remove `export`, the module uses it itself
spec spec/frontend/lib/utils/common_utils_spec.js
unexport isMetaKey remove `export`, the module uses it itself
spec spec/frontend/lib/utils/common_utils_spec.js
unexport rstrip remove `export`, the module uses it itselfHow the verdicts are made
Every verdict comes from knip's findings:
remove: knip reports the export withignoreExportsUsedInFile: true, so even its own module does not use it.unexport: knip reports the export only withignoreExportsUsedInFile: false, so only its own module uses it.keep: knip does not report the export.
Knip misses two kinds of use in the export's own module, even with ignoreExportsUsedInFile: true:
- A computed key, such as
{ [NAME]: value }. - The local name of
export default class Name.
So before it gives remove, the script searches the module for the name again. If it finds the name, the verdict is unexport.
A match in a comment also counts, so the check can only keep code, never delete it.
A three-file reproduction shows the knip bug on knip 6.38.0 and 6.43.0.
On the whole frontend, the check moves 91 exports from remove to unexport.
Use by specs does not change the verdict.
The exception is a test seam, an export whose name ends in ForTests.
A test seam stays while a spec uses it.
The script adds these entries that knip cannot infer:
- The modules that a webpack alias substitutes for a package, such as
lib/utils/vue3compat/vue.jsforvue. - The base entry points in
config/helpers/entry_points.js.
ee_else_ce resolves EE first.
For a CE module that has an EE override, the script also runs knip on the FOSS build, so a use that only FOSS builds see still counts.
describeFile() lists the importers of each export.
That list is for the report only, because it does not include dynamic imports.
For example, spec/frontend/lib/mousetrap_spec.js loads its test seams with await import().
Knip's findings count that use, but the importer list does not show it.
knip/session is a public export of the knip package, but the knip docs do not describe it.
The script checks for describeFile() and getIssues() and fails with a clear message if a knip upgrade removes them.
What was ruled out
- A custom import graph built on
oxc-parserandoxc-resolverwas faster, but it reinvented what knip already does and had its own bugs. For example, it counted a property key such asSUPPORTED_FORMATS.gigabytesas a use ofexport const gigabytes. Knip is a dependency already and is proven. knip --trace-filegives a per-file view, but it accepts one file and builds the whole project graph on every run.jestNamedFiles()and the alias map are duplicated fromscripts/frontend/check_unused_spec_code.mjs. Sharing them means changing that CI check, so a follow-up MR should move them into a shared module.
Screenshots or screen recordings
Not a UI change. The example output above shows the result.
How to set up and validate locally
-
Run the script on a module:
node scripts/frontend/check_unused_production_code.mjs app/assets/javascripts/lib/utils/common_utils.js -
Run it on a directory. Quote the glob so the shell does not expand it:
node scripts/frontend/check_unused_production_code.mjs 'app/assets/javascripts/lib/utils/**/*.{js,vue}' -
Check a few findings by hand. For example, search for
isInWorkItemPage: onlycommon_utils.jscontains it.
The first run takes about 30 seconds. Later runs use the knip cache and take about 10 seconds.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
References
- Follow-up to !261115 (merged), which added knip for frontend spec code
- Related to !263171 (merged), which removed the unused spec code that the spec check found
- The per-config cache works around https://github.com/webpro-nl/knip/issues/2078
- Findings on the whole frontend: #636669
- The own-file check came from the false positives in the test MR !265792