sveltejs / language-tools

The Svelte Language Server, and official extensions which use it
MIT License
1.25k stars 200 forks source link

report unused exports (typescript dead code) #1337

Open opensas opened 2 years ago

opensas commented 2 years ago

I'd like svelte-check to provide an option to report unused exports

Describe the solution you'd like

Svelte check lists unused exports as warning, with a configuration to ignore such cases

Describe alternatives you've considered

I tried with several utilities but they don't seem to get along fine with .svelte files

Additional context

Perhaps it could be possible to pass the svelte components compiled code to one of these libraries to take advatange of what they already do.

dummdidumm commented 2 years ago

As you said, svelte-check already issues warnings for unused exports (they come from the Svelte Compiler). What else is it what you want, could you clarify?

opensas commented 2 years ago

I mean when you export a function from a /.ts file and the you don't use it (not event import it) from anywhere.

I created this sample project: https://github.com/opensas/svelte-unused-exports-lint

you can open it on gipod and then:

gitpod /workspace/svelte-unused-exports-lint $ npx svelte-check

====================================
Loading svelte-check in workspace: /workspace/svelte-unused-exports-lint
Getting Svelte diagnostics...

====================================
svelte-check found 0 errors, 0 warnings, and 0 hints

I would like it to report something like:

/workspace/svelte-unused-exports-lint/src/lib/utils.ts:1:1
Hint: 'capitalize' is exported but it is never inported (ts)
  export const capitalize = (text: string): string =>
opensas commented 2 years ago

BTW (and please let me know if you'd like me to report a new issue), if I run npm run lint (you can also try it on gitpod I get this warning

$ npm run lint

> svelte-unused-exports-lint@0.0.1 lint
> prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .

Checking formatting...
All matched files use Prettier code style!
=============

WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: >=3.3.1 <4.5.0

YOUR TYPESCRIPT VERSION: 4.5.4

Please only submit bug reports when using the officially supported version.

=============

/workspace/svelte-unused-exports-lint/src/routes/index.svelte
  2:11  warning  'capitalize' is defined but never used  @typescript-eslint/no-unused-vars

✖ 1 problem (0 errors, 1 warning)

Even though the typescript version in package.json is "typescript": "^4.4.3"

dummdidumm commented 2 years ago

I see, thanks. I don't think this is something that we will be able to provide anytime soon. We essentially delegate to TypeScript for all these things, and if TypeScript doesn't report it, we won't as well. If we would add it to ourselves to both TS and Svelte files, we would need to somehow hook into the diagnostics of TypeScript and tell it to enhance this with something related and this comes with a performance hit, and we would come up with our own diagnostic message which should be consistent with other TS messages, and so on and so on. TLDR: If TS doesn't support it, it's likely we won't ever, either.

Your other issue is unrelated. The warning says that the TS eslint plugin only supports TS versions up to 4.5 and you are using 4.5.4. This is nothing the Svelte eslint plugin can fix either, you just have to wait until the official plugin supports the new version (maybe it does already, try bump the version).

opensas commented 2 years ago

Thanks a lot for your prompt reply. I thought that perhaps we could somehow use some of the afforementioned tools with the source compiled from svelte components, so that they could run all the checks they are already performing (on large codebases it would be terribly useful!), but I'm not an expert on the subject. If you can think of any tip to help get me started I might have a look at it. Thanks a lot!