Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rule proposal: Disallow variables declared with [await] using from being passed beyond the lifetime of their declaration scope #11309

Open
@Renegade334

Description

@Renegade334

Before You File a Proposal Please Confirm You Have Done The Following...

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions