knip icon indicating copy to clipboard operation
knip copied to clipboard

False Positive for Namespace Import in Vue SFC with Script Setup

Open kirkbaly opened this issue 1 year ago • 3 comments

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>

kirkbaly avatar Jul 26 '24 15:07 kirkbaly

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 :)

webpro avatar Jul 27 '24 07:07 webpro

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.

kirkbaly avatar Jul 28 '24 01:07 kirkbaly

What does the compiled JS look like? The return value is what we're interested in, because that's what Knip is working with.

webpro avatar Jul 28 '24 05:07 webpro

Closing due to inactivity.

webpro avatar Oct 31 '24 12:10 webpro

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 avatar Nov 14 '24 08:11 yishuolin

@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 avatar Nov 14 '24 09:11 webpro

@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 avatar Nov 14 '24 11:11 yishuolin

@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

webpro avatar Nov 14 '24 12:11 webpro

Thanks for the fix! I tested it, and it worked perfectly in my project.

yishuolin avatar Nov 15 '24 01:11 yishuolin

Thank you so much for confirming. I'll release it today.

webpro avatar Nov 15 '24 05:11 webpro

: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.

webpro avatar Nov 15 '24 22:11 webpro

Awesome! Sorry for the unresponsiveness, but thanks for the fix! Now to scan the template as well as the script... 😅 😂

kirkbaly avatar Nov 22 '24 22:11 kirkbaly