-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[naming-convention] Allow underscore prefix for backing fields #816
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
Question: what counts as a backing field? A private field that only use in accessors? A private field that's returned from a getter? What about the following? class C {
private _f1;
private _f2;
get f() {
return this._f1 + this._f2;
}
set f(v) {
this._f1 = v;
this._f2 = v;
}
} ...Or, should we keep the scope very limited and only detect getters that contain a single |
The initial idea was to only allow the underscore prefix if there was a getter with the exact same name (with or without actually checking that it returned that private field). In your example, you wouldn't be able to prefix If that is extended to "any private field that appears to be a backing field", then yes, it would need to check all accessors to ensure that at least one of them referenced the private field. Different goals I guess. Perhaps there would need to be an option (any backing field, or only backing fields that would otherwise conflict with accessor names). |
If it's only "private field with the same name as a getter", then I think it's easily doable. Sounds like a fair heuristic. |
Per #8792, we're feature freezing this rule. It's reached the point of complexity where we're having a hard time justifying working on it. If you'd like to see this option happen, I'd encourage you to add it to a community project such as eslint.style. Cheers! 💙 |
I also mentioned this just now over in my question to the ESLint Stylistic maintainers about potentially migrating this rule to ESLint Stylistic: |
Edit: The description below referred to the
member-naming
rule, which has since been replaced by the newnaming-convention
rule. This issue is still relevant.Repro
Expected Result
Error for
_var1
, but not for_var2
(because it's a backing field).Actual Result
Error for both fields.
Additional Info
Similar TSLint issue with 33 👍.
Since this rule is different to the one in TSLint, the implementation would be different. Maybe an
ignoreBackingFields: boolean
option, or a way to set the naming for backing fields separately.Versions
@typescript-eslint/eslint-plugin
1.13.0
@typescript-eslint/parser
1.13.0
TypeScript
3.4.5
ESLint
6.1.0
node
10.16.0
npm
6.9.0
The text was updated successfully, but these errors were encountered: