Navigation Menu

Skip to content

Commit

Permalink
feat(eslint-plugin): [dot-notation] add `allowProtectedClassPropertyA…
Browse files Browse the repository at this point in the history
…ccess` option (#2622)
  • Loading branch information
a-tarasyuk committed Oct 18, 2020
1 parent 63d1d81 commit bbc9e35
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/eslint-plugin/docs/rules/dot-notation.md
Expand Up @@ -23,10 +23,12 @@ This rule adds the following options:
```ts
interface Options extends BaseDotNotationOptions {
allowPrivateClassPropertyAccess?: boolean;
allowProtectedClassPropertyAccess?: boolean;
}
const defaultOptions: Options = {
...baseDotNotationDefaultOptions,
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
};
```

Expand All @@ -43,4 +45,17 @@ const x = new X();
x['priv_prop'] = 123;
```

### `allowProtectedClassPropertyAccess`

Example of a correct code when `allowProtectedClassPropertyAccess` is set to `true`

```ts
class X {
protected protected_prop = 123;
}

const x = new X();
x['protected_prop'] = 123;
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/dot-notation.md)</sup>
21 changes: 18 additions & 3 deletions packages/eslint-plugin/src/rules/dot-notation.ts
Expand Up @@ -38,6 +38,10 @@ export default createRule<Options, MessageIds>({
type: 'boolean',
default: false,
},
allowProtectedClassPropertyAccess: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
Expand All @@ -48,6 +52,7 @@ export default createRule<Options, MessageIds>({
defaultOptions: [
{
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowKeywords: true,
allowPattern: '',
},
Expand All @@ -56,20 +61,30 @@ export default createRule<Options, MessageIds>({
const rules = baseRule.create(context);
const allowPrivateClassPropertyAccess =
options.allowPrivateClassPropertyAccess;
const allowProtectedClassPropertyAccess =
options.allowProtectedClassPropertyAccess;

const parserServices = getParserServices(context);
const typeChecker = parserServices.program.getTypeChecker();

return {
MemberExpression(node: TSESTree.MemberExpression): void {
if (allowPrivateClassPropertyAccess && node.computed) {
if (
(allowPrivateClassPropertyAccess ||
allowProtectedClassPropertyAccess) &&
node.computed
) {
// for perf reasons - only fetch the symbol if we have to
const objectSymbol = typeChecker.getSymbolAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node.property),
);
const modifierKind = objectSymbol?.getDeclarations()?.[0]
?.modifiers?.[0].kind;
if (
objectSymbol?.getDeclarations()?.[0]?.modifiers?.[0].kind ===
ts.SyntaxKind.PrivateKeyword
(allowPrivateClassPropertyAccess &&
modifierKind == ts.SyntaxKind.PrivateKeyword) ||
(allowProtectedClassPropertyAccess &&
modifierKind == ts.SyntaxKind.ProtectedKeyword)
) {
return;
}
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint-plugin/tests/rules/dot-notation.test.ts
Expand Up @@ -75,6 +75,18 @@ x['priv_prop'] = 123;
`,
options: [{ allowPrivateClassPropertyAccess: true }],
},

{
code: `
class X {
protected protected_prop = 123;
}
const x = new X();
x['protected_prop'] = 123;
`,
options: [{ allowProtectedClassPropertyAccess: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -255,5 +267,25 @@ x.pub_prop = 123;
options: [{ allowKeywords: false }],
errors: [{ messageId: 'useBrackets', data: { key: 'if' } }],
},
{
code: `
class X {
protected protected_prop = 123;
}
const x = new X();
x['protected_prop'] = 123;
`,
options: [{ allowProtectedClassPropertyAccess: false }],
output: `
class X {
protected protected_prop = 123;
}
const x = new X();
x.protected_prop = 123;
`,
errors: [{ messageId: 'useDot' }],
},
],
});
1 change: 1 addition & 0 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Expand Up @@ -708,6 +708,7 @@ declare module 'eslint/lib/rules/dot-notation' {
allowKeywords?: boolean;
allowPattern?: string;
allowPrivateClassPropertyAccess?: boolean;
allowProtectedClassPropertyAccess?: boolean;
},
],
{
Expand Down

0 comments on commit bbc9e35

Please sign in to comment.