-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Bug: [class-literal-property-style] The overridden literal getter does not pass lint and incorrectly auto-fix #5962
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
Comments
Related to #5961 I think it should be fixed in two parts:
We need to think more deeply about the semantics of this rule 🤔 Accepting a PR to change it to a suggestion fixer for now. |
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961) [prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962) BREAKING CHANGE
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961) [prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962) BREAKING CHANGE
Whilst yes, it's technically a change in behaviour - in the vast majority of code this difference isn't actually a change that would cause any runtime difference. The only real time the difference is going to come into effect is when you're accessing the member in the superclass's constructor. For all other real usages I can think of right now you're just going to be accessing the instance directly ( Ultimately the intersection of the "times that that problem's going to occur" and "this rule will fire" is going to be quite small. To be clear - you specifically need to have a getter defined on a class that returns a literal whose parent class accesses that accessor in the constructor. Also worth noting that TS doesn't model the prototype vs instance behaviour (and neither do more sound checkers like flow) because it's just not something that matters to code. I'm reluctant for us to remove the autofixer from this usecase considering it's such a rare case, but I'm okay as long as it remains a suggestion fixer. We definitely shouldn't stop reporting on this case given how rare it is. |
To be clear, it breaks any time you try to make a "stub getter" for children to override. class P {
get a() {
return "stub";
}
}
class C extends P {
get a() {
console.log("Do something more complicated");
return "Actual stuff";
}
}
new C().a; // "Actual stuff" class P {
a = "stub";
}
class C extends P {
get a() {
console.log("Do something more complicated");
return "Actual stuff";
}
}
new C().a; // "stub" Is this common? —I don't know, maybe it's not, but that's actually an intended use case of accessors (especially with auto-accessors to be introduced with decorators, I expect it to be even more common). Is there a good way to change the code pattern to appease the rule? —Maybe not, except for a disable comment. I was playing with this with @JoshuaKGoldberg and I attempted to make the getter abstract, but it turns out you can't have an abstract getter and a concrete setter, so... ¯_(ツ)_/¯ |
When looking at lint rules basing decisions based on our experience is really the best way for us to make judgements about what rules should exist. This evaluation is important because some rules are just going to work for some codebases and some rules aren't. If we make judgements about lint rules based on what some codebases might possibly do and how they might break if the rule's turned on - then a lot of really good rules might not exist! As a related example - the
Is it? Based on my understanding of the spec - auto-accessors are designed to work well with decorators (hence they are included in the decorators spec) I personally have never seen someone implement an accessor with the intention that a child class must explicitly override it. It's obviously not THAT uncommon though considering TS has abstract accessors though. |
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961) [prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962) BREAKING CHANGE
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961) [prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962) BREAKING CHANGE
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961) [prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962)
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=4.8.4&sourceType=module&code=MYGwhgzhAECC0G8CwAoa0D6AXAphLAXNPgE4CWAdgOYDcqq6VOW0u+AFAJRGmVWIN00EswCuJCqwAWZCADpseLHTTQAvqg0pUoSDABC0HAA9cFACYx4yVUxZssXHlnLUBq9CKzjJAcge+KuhaWkA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6M+AQ2WVsf0TRO8WsWgB7UtCK1kRJOiiCJ0SODABfEBqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDUjoAHoizJ46IngCGqACL9YmNXl3Cwo9FIC+IM0A
Repro Code
ESLint Config
tsconfig
Expected Result
There are no errors because the getter is overridden
Actual Result
Error:
Literals should be exposed using readonly fields.
Auto-fix:
Then TypeScript error:
'test' is defined as an accessor in class 'A', but is overridden here in 'B' as an instance property.
Additional Info
No response
Versions
@typescript-eslint/eslint-plugin
5.42.1
@typescript-eslint/parser
5.42.1
TypeScript
4.8.4
ESLint
8.15.0
node
web
The text was updated successfully, but these errors were encountered: