Skip to content
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

Rule proposal: explicit-resource-management-literal-pair-using-dispose #7160

Closed
6 tasks done
loynoir opened this issue Jul 2, 2023 · 3 comments
Closed
6 tasks done
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@loynoir
Copy link

loynoir commented Jul 2, 2023

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

  • When RHS literal object have literal property Symbol.dispose, LHS should have using

  • When LHS have using, RHS should be literal object with literal property Symbol.dispose

Fail Cases

const x = {
  [Symbol.dispose]() {
    console.log(2, { x });
  },
};

console.log(1, { x });
using x = {
  dispose() {
    console.log(2, { x });
  },
};

console.log(1, { x });
const fn = () => {
  return {
    [Symbol.dispose]() {
      console.log(2, { x });
    },
  }
}

using x = fn();

console.log(1, { x });

Pass Cases

using x = {
  [Symbol.dispose]() {
    console.log(2, { x });
  },
};

console.log(1, { x });

Additional Info

https://github.com/tc39/proposal-explicit-resource-management

https://devblogs.microsoft.com/typescript/announcing-typescript-5-2-beta/#using-declarations-and-explicit-resource-management

microsoft/TypeScript#54505

evanw/esbuild#3191

@loynoir loynoir added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 2, 2023
@bradzacher
Copy link
Member

Thanks for the suggestion!
First up - as this feature is implemented in the just announced 5.2 beta - we won't be accepting suggestions for it at this time.

That being said - TBH I think it's a bit early on in the proposal's lifecycle to be writing or proposing lint rules for it - given this is brand new syntax I think we should wait a few months for people to play around and use the feature to figure out the footguns and lint cases.

Based on my limited understanding of the feature - I don't think that you will want to always using a disposable. For example - imagine a utility method like

function returnsDisposable(): Disposable {
  const inner = { [Symbol.dispose]: () => {} };
  return inner;
}

using outer = returnsDisposable();

In this instance you 100% wouldn't want to have using inner instead of const inner because doing so would mean that inner is cleaned up before the disposable is returned!

This is why I think we need to wait some time before people sus out the patterns and pitfalls of the feature.

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Jul 2, 2023
@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for team members to take a look labels Jul 2, 2023
@loynoir
Copy link
Author

loynoir commented Jul 3, 2023

Description updated in #7160 (comment)

@loynoir loynoir changed the title Rule proposal: explicit-resource-management Rule proposal: explicit-resource-management-MUST-cleanup Jul 3, 2023
@bradzacher
Copy link
Member

There's lots of potential footguns with using - I can see that it's very easy to dispose an object multiple times:

const x = {[Symbol.dispose]() {}};
using one = x;
using two = x;
using three = one;

This code is valid and at the end x will get disposed 3 times!
There's also the case I mentioned before about returning a using value and thus returning a disposed object.

It's so hard to know exactly how people will want to use this feature and what the lint cases will be overall. I suspect a lot of these lint rules will probably not require types and will be able to live in eslint core as well.

We'll have to wait and see!

@loynoir loynoir changed the title Rule proposal: explicit-resource-management-MUST-cleanup Rule proposal: explicit-resource-management-literal-pair-using-dispose Jul 3, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants