False Positive for Namespace Import in Vue SFC with Script Setup
Reproduction url
https://stackblitz.com/edit/github-bquuj6
Description of the issue
Knip does not detect used exports that are used via namespace imports in Vue SFCs with script setup.
In this example, Bar.bar is reported as unused, while Baz.baz is reported as used. Of course, the same import and usage of Bar in a TS file would be reported as used.
<script setup lang="ts">
import * as Bar from 'Bar.ts';
import { baz } from 'Baz.ts';
Bar.bar();
baz();
</script>
This is basically a dup of https://github.com/webpro-nl/knip/issues/733
Knip only extracts import statements in the built-in "compilers", you'd need to install an actual compiler.
And I need to document this properly :)
Ah, thanks for the info. I read through most of the docs but somehow missed that 🤦
Unfortunately, I forked the repro and made those changes (after fixing the missing declaration of getStyleImports and adding the css compiler), and it didn't have any effect. Here's that url: https://stackblitz.com/edit/github-bquuj6-32sfgs
Wouldn't be surprised if I'm doing something wrong, but I'm still new to Knip and not very familiar with the vue sfc compiler.
What does the compiled JS look like? The return value is what we're interested in, because that's what Knip is working with.
Closing due to inactivity.
The compiled output from @kirkbaly's example appears to be correct, as shown below
import * as Bar from 'Bar.ts';
import { baz } from 'Baz.ts';
Bar.bar();
baz();
After investigating, I suspect that this issue might be due to Knip only updating importedInternalSymbols for DEFAULT_EXTENSIONS here?
@yishuolin Thanks, so you're having the same issue? That could very well be the cause indeed. Would you like submit a pull request? No worries if not and I'll investigate/pick it up later.
@webpro I encountered a similar issue—Knip reports unused enumMembers if the enum member is used in a file compiled by a custom compiler. Here's a minimal reproduction.
Apologies, but I might not be able to look into this issue at the moment, so I’ll need to hand it over to you. Thank you!
@yishuolin @kirkbaly I might have a fix, would be great if you could test this version in your project(s):
npm i -D https://pkg.pr.new/knip@249794a
Thanks for the fix! I tested it, and it worked perfectly in my project.
Thank you so much for confirming. I'll release it today.
:rocket: This issue has been resolved in v5.37.1. See Release 5.37.1 for release notes.
Using Knip in a commercial project? Please consider becoming a sponsor.
Awesome! Sorry for the unresponsiveness, but thanks for the fix! Now to scan the template as well as the script... 😅 😂