Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
- My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Description
Explicit resource management is a new language paradigm, and carries with it potential footguns.
Most notably, there's nothing to stop variables declared with [await] using
having their contents passed outside of the scope in which they're declared, in such a way that the disposable resource persists beyond the lifetime of the declaring scope. This is almost invariably unintentional, as the resource in question will be disposed of when execution leaves the original scope.
{
using resource = getDisposableResource()
someCallbackStyleAsyncAPI((data) => {
resource.doSomething(data)
})
} // oops, the resource is disposed before the callback is invoked
For a variable disposable
declared using [await] using
, it should be possible for a rule to flag the following:
- assignment of the value of
disposable
to a variable outside the declaration scope - returning
disposable
from a function - exporting
disposable
- referencing
disposable
within a closure
Fail Cases
function makeMeADisposable() {
using disposable = ...
someSetupOperation(disposable)
return disposable
}
using disposable = ...
someSetupOperation(disposable)
export { disposable }
let resource
for (using disposable of iterator) {
if (condition) {
resource = disposable
break
}
}
{
using disposable = ...
someCallbackStyleAsyncFunction((err, data) => {
doSomething(disposable, data)
})
}
Pass Cases
function makeMeADisposable() {
const disposable = ...
someSetupOperation(disposable)
return disposable
}
const disposable = ...
someSetupOperation(disposable)
export { disposable }
let property
for (using disposable of iterator) {
if (condition) {
property = disposable.property
break
}
}
{
using disposable = ...
const data = await somePromiseStyleAsyncFunction()
doSomething(disposable, data)
}
Additional Info
export using disposable = ...
is an illegal pattern under the specification, but export { disposable }
is not.