feat: detect execaNode scripts in execa visitor#1824
Merged
Conversation
commit: |
Member
|
Thank you! ✨ |
Member
|
🚀 This pull request is included in v6.22.0. See Release 6.22.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Execa script visitor did not detect Node.js scripts executed with
execaNode.For example:
Both calls execute
build.mjsas a Node.js script, so Knip should treatbuild.mjsas a used entry file. Without this handling, files executed only throughexecaNodecould be reported as unused.Cause
Knip collects scripts from Execa APIs in
packages/knip/src/plugins/execa/visitors/execa.ts.The visitor already handled APIs such as:
However, it did not handle
execaNode.This looks like an omission rather than intentional behavior. The existing fixture in
packages/knip/fixtures/plugin-config/script-visitors-execa/methods.mjsalready importedexecaNode, but it did not include any actualexecaNodecall:So the fixture acknowledged the API, but the visitor did not verify that scripts executed through that API were collected.
The Execa API reference documents
execaNode(scriptPath, arguments?, options?)as the preferred API for executing Node.js files. It runs a Node.js file asnode scriptPath ...arguments.References:
execaNode: https://github.com/sindresorhus/execa/blob/main/docs/api.md#execanodescriptpath-arguments-optionsexecaNode\command``: https://github.com/sindresorhus/execa/blob/main/docs/api.md#execacommandexecaNode: https://github.com/sindresorhus/execa#ipcBecause of this,
execaNode('build.mjs')andexecaNode\build.mjs`` should be treated as script entry usage by Knip.Changes
This PR adds
execaNodesupport to the Execa script visitor.execaNode('file.mjs').execaNode\file.mjs``.node file.mjsbefore passing them to the existing script parser.$and$synctagged template handling unchanged.execaNodecall and tagged template cases.execaNodeare not reported as unused.Impact
Files executed through
execaNodewill no longer be reported as unused files.This reduces false positives in projects that use Execa to run build scripts, release scripts, code generation scripts, or other local Node.js entry files.
Verification
The related Execa script visitor test passes.