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.

function getDisposableResource() {
  using resource = { ... }
  // ...
  return resource
} // oops, the resource is disposed upon exiting the function

For a variable disposable declared using [await] using, it should be possible for a rule to flag the following:

Fail Cases

function makeMeADisposable() {
  using disposable = { ... }
  // ...
  return disposable
}

using disposable = { ... }
// ...
export { disposable }

let resource
for (using disposable of iterator) {
  if (condition) {
    resource = disposable
    break
  }
}

Pass Cases

function makeMeADisposable() {
  const disposable = { ... }
  // ...
  return disposable
}

const disposable = { ... }
// ...
export { disposable }

let property
for (using disposable of iterator) {
  if (condition) {
    property = disposable.property
    break
  }
}

Additional Info

export using disposable = ... is an illegal pattern under the specification, but export { disposable } is not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting responseIssues waiting for a reply from the OP or another partyenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy