language-tools
language-tools copied to clipboard
svelte-check: Ignore diagnostics for imported modules
Description
We use [email protected] and Typescript. We also use generated code in our code basis and would like to have the diagnostics for those modules ignored.
We can neither use the --ignore flag nor an exclude setting in the tsconfig.json to solve this issue, because it does not work for modules who get imported by modules which are getting diagnosed.
Proposed solution
Let the diagnostics for modules be ignored/suppressed, also when they get imported by a module which gets diagnosed.
So, e.g. the lines https://github.com/sveltejs/language-tools/blob/master/packages/language-server/src/plugins/PluginHost.ts#L75-L87 implement this feature hard-coded for files under node_modules. It would be nice if this would also work for any file specified by the --ignore flag (or any newly-introduced flag).
Alternatives
No response
Additional Information, eg. Screenshots
No response
I'm a little hesitant to approve this because it would deviate quite a bit from how "vanilla" TypeScript/tsc works, AFAIK you can't ignore certain files by passing a command line option. Maybe we could allow the ignore option to work for Svelte files but not TS files - but that's possibly even more confusing.
If it's about TS/JS errors, you can silence them by adding a // @ts-nocheck comment to the top of the script tag in your Svelte/TS files.
Thanks for the reply. So for our case, I would like to suppress the diagnostics for generated code (e.g. generated OpenAPI client stubs, which are in effect typescript modules). We use the output of svelte-check in our CI to determine the code quality.
Certainly I could add (or modify the script in the CI to do it) a // @ts-nocheck comment at the top, but it feels a bit hacky, as I consider these files read-only like the modules under node_modules.
Beyond that, svelte-check still delivers hint diagnostics (but no more errors) for files commented with // @ts-nocheck (see also this issue https://github.com/microsoft/TypeScript/issues/47315), which is also a bit annoying because this pollutes the output (especially if running svelte-check manually).
You can tell svelte-check to not output hints by adding --threshold warning. I don't think that adding // @ts-nocheck is hacky, it's the recommended way by TS to omit typechecking for files you don't want to be checked, which is the case here.
To me it sounds like your use case could be worked around with these two things, so I'm hesitant to merge this feature right now, maybe sleep on it and/or await some more feedback from other people (other people: please post your use cases here!). I get your point though, this is a valid feature request!
@cbartz what errors exactly appear in the generated code? Are you able to fix these errors or is this impractical (if yes, why)? How do other code bases (TS+X, not necessarily Svelte) deal with this, this problem must have appeared in many other places as well?
@dummdidumm We get a Hint: 'x' is declared but its value is never read. The CI Job inspects the exit code of svelte-check in order to decide if it should fail or not, which means that we can ignore this hint in the CI because the exit code is equal to zero. So, for the use case CI, this is not a huge problem, but nevertheless the CI job log gets polluted.
For local development this is a bit annoying, because the output of svelte-check always shows this hint and we are not able to fix it (we do not want to fix generated code).
// @ts-nocheck does not work, at least for me. It seems like there is absolutely no way to silence these hints.
I have reproduced it on Stackblitz. This currently makes svelte-check completely unusable for me, since I have to include a emscripten-generated file, which produces over 500 hints.
In the link above, I tried:
- Setting the
checkscript tosvelte-check --tsconfig ./jsconfig.json --ignore ./src/lib/im-a-generated-file.js - Adding
// @ts-nocheckto the file - Adding
"exclude": ["./src/lib/im-a-generated-file.js"]tojsconfig.json - Setting
checkJstofalseinjsconfig.json
None of which worked.
For me, // @ts-nocheck is disabling errors but not hints for a bundle of PrismJS which I am importing from a vendor directory in my project. It is still printing Hint: This constructor function may be converted to a class declaration.
The same is for generated grpc-web client. Errors and warnings are suppresed, but not hints — they are still polluting all the logs.