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)

Most upvoted comments

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 getNameFromMember function 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?

class Test {
  #foo: string = 'a';
  '#foo': string = 'b';

  test() {
    console.log(this.#foo, this['#foo']);
  }
}

#foo is a private class property whose name is foo. '#foo' is a public class property whose name is #foo.

For both of these cases, however, getNameFromMember will 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.

package version
@typescript-eslint/eslint-plugin 5.3.1
@typescript-eslint/parser 5.3.1
TypeScript 4.4.4
ESLint 8.2.0
node 16.13.0

Do you have all the needed info to reproduce it?

the # is not included in the name.

This statement seems to be incorrect. The util.getNameFromMember method 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:

const propertyName = util.getNameFromMember(classProperty, sourceCode);

by

const propertyName = util.getNameFromMember(classProperty, sourceCode);
if (propertyName.startsWith('#')) { return; }

@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:

Missing accessibility modifier on class property

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' } }]

To clarify - this will work right now due to “dumb luck” of sorts. The getNameFromMember function 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?

class Test {
  #foo: string = 'a';
  '#foo': string = 'b';

  test() {
    console.log(this.#foo, this['#foo']);
  }
}

#foo is a private class property whose name is foo. '#foo' is a public class property whose name is #foo.

For both of these cases, however, getNameFromMember will currently print #foo. So we cannot currently rely on this value to drive the lint rule as it is not correct.

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.