Skip to content
Permalink
Browse files

feat(eslint-plugin): [dot-notation] add `allowProtectedClassPropertyA…

…ccess` option (#2622)
  • Loading branch information
a-tarasyuk committed Oct 18, 2020
1 parent 63d1d81 commit bbc9e3540576891552dc2dc54b2acbc54104be9d
@@ -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,
};
```

@@ -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>
@@ -38,6 +38,10 @@ export default createRule<Options, MessageIds>({
type: 'boolean',
default: false,
},
allowProtectedClassPropertyAccess: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
@@ -48,6 +52,7 @@ export default createRule<Options, MessageIds>({
defaultOptions: [
{
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowKeywords: true,
allowPattern: '',
},
@@ -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;
}
@@ -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: [
{
@@ -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' }],
},
],
});
@@ -708,6 +708,7 @@ declare module 'eslint/lib/rules/dot-notation' {
allowKeywords?: boolean;
allowPattern?: string;
allowPrivateClassPropertyAccess?: boolean;
allowProtectedClassPropertyAccess?: boolean;
},
],
{

0 comments on commit bbc9e35

Please sign in to comment.
You can’t perform that action at this time.