-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Bug: [no-unused-vars] false positive when using decorators on class #6260
Comments
🤔 I would think most users would consider this to be an unused class. Anecdotally speaking, most class decorators I've seen in use today only modify the class - they don't send it somewhere else. Perhaps this needs to be an opt-in option using the format in discussion in #6017? Marking as blocked pending that discussion's resolution (waiting on eslint/eslint#16540). |
Actually my case is using decorators to define custom elements, just like the introduction example of the tc39/proposal-decorators @defineElement("my-class")
class C extends HTMLElement {
@reactive accessor clicked = false;
} |
I agree that we can't tell a decorator whether would use the decorated target, just by AST. I didn't fully understand your proposal in that discussion, but I think we can provide an opt-in option to mark some decorators as having side effects on the decorated target? |
Yup! The tricky part is specifying the format for that opt-in option. Enough rules have this need that we're trying to make one unified format. That discussion is talking about how we want that format to look. |
👍 Looking forward to it |
The rule is correct here based purely on semantics. Why? Because decorators are just syntactic sugar that does something like this: @deco
class Foo {}
// same as
const Foo = deco(class Foo {}) Notice how regardless of what side-effects the decorator may or may not do, there is still an unused variable left defined? To put this another way, would you consider this to be an unused variable: function sideEffect(arg) {
window.store.add(arg);
return arg;
}
const foo = sideEffect(1); I'd hope you'd answer yes, as the variable is clearly unused. Given that the rule is correct here, the question is whether or not there should be an option to ignore this sort of case?
(1) may change over time for sure as decorators are standardised fully and more use cases are explored by the community. If it does I think we could definitely revisit this discussion (though at that point it would be in the context of the core rule). |
@bradzacher Thanks for your patient explaining, but I guess I'll keep using it as decorator and ignore such warnings, : ( |
As you mentioned above of the de-sugared code is like: const Foo = deco(class Foo {}) Even though only the latter Currently in vscode, if I click "Quick Fix", the editor will remove my whole code, which is of course wrong I think (but it maybe the problem of typescript) |
@weareoutman |
@bradzacher Yes, it's provided by TypeScript, I also fired an issue there, since TS reports an unused error for the same use case, too. It seems that someone else has the same opinion as you mentioned above, however, I don't agree with that. I explained with more of my thoughts on that issue. |
This is done because the use of TypeScript annotations causes this check to produce false positives with TypeScript version 4.9, which seems to be a known issue, see [1] and [2]. [1] typescript-eslint/typescript-eslint#6260 [2] microsoft/TypeScript#51992 Bug: b/265863256 Change-Id: Idc9079a0a4fe9b88b10f6abb065f95bbd53f1a57 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4176095 Reviewed-by: Joel Hockey <joelhockey@chromium.org> Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org> Commit-Queue: Joel Hockey <joelhockey@chromium.org> Cr-Commit-Position: refs/heads/main@{#1093689}
This affects a lot of projects I work on where every entity I create for use with TypeORM has decorators. Refer https://github.com/typeorm/typeorm/blob/master/docs/entities.md |
We do not encode semantics for framework-specific side-effects in our lint rules. If you'd like to encode information about your specific framework's side-effects into the rule - you can freely do that by creating a new rule that marks the variable as used using ESLint's standard API, Here are two example rules that do exactly that: |
Thanks for your advice of custom rules, that seems to be the right way |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=4.9.3&sourceType=module&code=GYVwdgxgLglg9mABAEwKYQBQEoBciCGYAnogN4BQAvueQAJqZbkQA2+Azu4gEJk2VA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQHYHsBaWXWZRAE0IDcBDaVDSfAMxcgBpxtIABAFwCeABxQBjaAEth-QiniTc-APQFipclToN0UAO71ckbgF8QJoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEk6AHolsvOkXgIaoAi6cmHPLExlKeTAFd04MAF8QsoA
Repro Code
ESLint Config
tsconfig
Expected Result
No warnings.
Actual Result
'B' is defined but never used.
Additional Info
No response
The text was updated successfully, but these errors were encountered: