typescript-eslint: [explicit-member-accessibility] False positive when using private fields
Repro
The rule @typescript-eslint/explicit-member-accessibility reports a false positive when using private fields. At the same time, TypeScript reports the error An accessibility modifier cannot be used with a private identifier if an accessibility modifier would be used.
{
"rules": {
"@typescript-eslint/explicit-member-accessibility": ["warn"]
}
}
class Person {
#name: string
constructor(name: string) {
this.#name = name;
}
greet() {
console.log(`Hello, my name is ${this.#name}!`);
}
}
Expected Result
No false positive when using private fields.
Actual Result
Additional Info
Versions
| package | version |
|---|---|
@typescript-eslint/eslint-plugin |
2.22.0 |
@typescript-eslint/parser |
2.22.0 |
TypeScript |
3.8.3 |
ESLint |
6.8.0 |
node |
12.16.1 |
npm |
6.14.1 |
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 28
- Comments: 17 (9 by maintainers)
Any updates?
Is there a rough estimation when this could be fixed?
Do we have any updates on this issue?
Unless we have a solution for this, this rule becomes useless for most projects as it will throw continuously false positives.
To clarify - this will work right now due to “dumb luck” of sorts. The
getNameFromMemberfunction just says “if I don’t know how to handle the node, just get the source code string for the node”. Hence the function returns"#foo".However - this isn’t entirely correct. The name of the member is
foo, not#foo. Why does this distinction matter?#foois a private class property whose name isfoo.'#foo'is a public class property whose name is#foo.For both of these cases, however,
getNameFromMemberwill currently print#foo. So we cannot currently rely on this value to drive the lint rule as it is not correct.Here are the versions.
@typescript-eslint/eslint-plugin5.3.1@typescript-eslint/parser5.3.1TypeScript4.4.4ESLint8.2.0node16.13.0Do you have all the needed info to reproduce it?
This statement seems to be incorrect. The
util.getNameFromMembermethod returns the name with the leading#.I’ve just checked that and this small change fixed the problem: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts Method
checkPropertyAccessibilityModifier. Replace:by
@OldStarchy could you please post a full reproduction so that I can take a look? With just your versions and a single error there’s nothing I can help with.
Adding exact search term here because I stumbled over this issue, too:
I’m getting
Missing accessibility modifier on class property #doesItWork.error also.Test case:
export class PrivateTest { #doesItWork?: number }My rule:'@typescript-eslint/explicit-member-accessibility': ['error', { overrides: { constructors: 'no-public' } }]I believe majority of projects don’t use leading dash and quotes around class properties and even they do, now they have a problem because leading dash means a private class property in js. Even if js doesn’t throw an error for this syntax, it will be confusing for developers.
So, I don’t understand why all the rest should struggle to use new private class properties just for the sake of rule to be 100% complete (even though it’s not 100% complete now, if we take into consideration private properties)
No. The AST for private names is different, and thus the
#is not included in the name.The AST is not yet finalised either, so we cannot build a rule on top of it.
We do not yet support 3.8 syntax, including private fields. See #1436 and associated PRs.