-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to lint legacy usage of new Disposable
type?
#187
Comments
You can't design such a thing in JS without type information - but for TS codebases, i assume eslint-plugin-typescript would be a natural place to suggest such a feature. |
The excess dispose is not obviously a problem. Most disposables, including node file handles, can safely be closed multiple times (with later calls being a no-op). Also, there's no reason to use an explicit try/finally to clean up a As such, a lint warning for any use of a |
Although it's valid in this specific case, it's generally not a good idea to dispose something twice. This is also a challenge for linters to tell if it's safe to disposed a resource more than once. Or should we recommend all
Then all legacy usage would get that warning. A bit annoying but manageable I guess. |
I don't think linters should try to warn you against disposing something multiple times, and I do think Disposables should safely handle multiple disposals (with the second being a no-op). Yes, sometimes there will be Disposables which are in fact not safe to dispose multiple times, but linters don't need to catch every possible error.
Personally, once I start using the new syntax and enabling lint rules and so on for it, I will want get get warnings for the legacy pattern - it's more verbose and error-prone. So I don't really see this as a problem. |
In lieu of type information, a linter could at the very least have heuristics to recognize well-known host APIs that will produce disposables, such as |
I'm extending your idea of type annotating used and unused What you were describing sounded like a generic type annotation that can help I'm expanding that thread of discussion to explore more on that idea. |
I've created a very basic lint rule (just warns on use of |
One reason of having the
Disposable
type is to support better static analysis on resource management. However, after we upgraded existing functions to return aDisposable
type, we now have two valid mechanisms to dispose the resource:Symbol.dispose
orSymbol.asyncDispose
.It raises question on how can we apply generic linter rules to effectively detect resource management mistakes when having two mechanisms both being valid.
Take the following examples using the new
node:fs
Disposable
APIs:The first two cases are valid, while the others are misuses. How can we design a generic linter to effectively detect the misuses without false positive on the legacy usage?
The text was updated successfully, but these errors were encountered: