Skip to content

Commit bbc9e35

Browse files
authored
feat(eslint-plugin): [dot-notation] add allowProtectedClassPropertyAccess option (typescript-eslint#2622)
1 parent 63d1d81 commit bbc9e35

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

packages/eslint-plugin/docs/rules/dot-notation.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ This rule adds the following options:
2323
```ts
2424
interface Options extends BaseDotNotationOptions {
2525
allowPrivateClassPropertyAccess?: boolean;
26+
allowProtectedClassPropertyAccess?: boolean;
2627
}
2728
const defaultOptions: Options = {
2829
...baseDotNotationDefaultOptions,
2930
allowPrivateClassPropertyAccess: false,
31+
allowProtectedClassPropertyAccess: false,
3032
};
3133
```
3234

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

48+
### `allowProtectedClassPropertyAccess`
49+
50+
Example of a correct code when `allowProtectedClassPropertyAccess` is set to `true`
51+
52+
```ts
53+
class X {
54+
protected protected_prop = 123;
55+
}
56+
57+
const x = new X();
58+
x['protected_prop'] = 123;
59+
```
60+
4661
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/dot-notation.md)</sup>

packages/eslint-plugin/src/rules/dot-notation.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export default createRule<Options, MessageIds>({
3838
type: 'boolean',
3939
default: false,
4040
},
41+
allowProtectedClassPropertyAccess: {
42+
type: 'boolean',
43+
default: false,
44+
},
4145
},
4246
additionalProperties: false,
4347
},
@@ -48,6 +52,7 @@ export default createRule<Options, MessageIds>({
4852
defaultOptions: [
4953
{
5054
allowPrivateClassPropertyAccess: false,
55+
allowProtectedClassPropertyAccess: false,
5156
allowKeywords: true,
5257
allowPattern: '',
5358
},
@@ -56,20 +61,30 @@ export default createRule<Options, MessageIds>({
5661
const rules = baseRule.create(context);
5762
const allowPrivateClassPropertyAccess =
5863
options.allowPrivateClassPropertyAccess;
64+
const allowProtectedClassPropertyAccess =
65+
options.allowProtectedClassPropertyAccess;
5966

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

6370
return {
6471
MemberExpression(node: TSESTree.MemberExpression): void {
65-
if (allowPrivateClassPropertyAccess && node.computed) {
72+
if (
73+
(allowPrivateClassPropertyAccess ||
74+
allowProtectedClassPropertyAccess) &&
75+
node.computed
76+
) {
6677
// for perf reasons - only fetch the symbol if we have to
6778
const objectSymbol = typeChecker.getSymbolAtLocation(
6879
parserServices.esTreeNodeToTSNodeMap.get(node.property),
6980
);
81+
const modifierKind = objectSymbol?.getDeclarations()?.[0]
82+
?.modifiers?.[0].kind;
7083
if (
71-
objectSymbol?.getDeclarations()?.[0]?.modifiers?.[0].kind ===
72-
ts.SyntaxKind.PrivateKeyword
84+
(allowPrivateClassPropertyAccess &&
85+
modifierKind == ts.SyntaxKind.PrivateKeyword) ||
86+
(allowProtectedClassPropertyAccess &&
87+
modifierKind == ts.SyntaxKind.ProtectedKeyword)
7388
) {
7489
return;
7590
}

packages/eslint-plugin/tests/rules/dot-notation.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ x['priv_prop'] = 123;
7575
`,
7676
options: [{ allowPrivateClassPropertyAccess: true }],
7777
},
78+
79+
{
80+
code: `
81+
class X {
82+
protected protected_prop = 123;
83+
}
84+
85+
const x = new X();
86+
x['protected_prop'] = 123;
87+
`,
88+
options: [{ allowProtectedClassPropertyAccess: true }],
89+
},
7890
],
7991
invalid: [
8092
{
@@ -255,5 +267,25 @@ x.pub_prop = 123;
255267
options: [{ allowKeywords: false }],
256268
errors: [{ messageId: 'useBrackets', data: { key: 'if' } }],
257269
},
270+
{
271+
code: `
272+
class X {
273+
protected protected_prop = 123;
274+
}
275+
276+
const x = new X();
277+
x['protected_prop'] = 123;
278+
`,
279+
options: [{ allowProtectedClassPropertyAccess: false }],
280+
output: `
281+
class X {
282+
protected protected_prop = 123;
283+
}
284+
285+
const x = new X();
286+
x.protected_prop = 123;
287+
`,
288+
errors: [{ messageId: 'useDot' }],
289+
},
258290
],
259291
});

packages/eslint-plugin/typings/eslint-rules.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ declare module 'eslint/lib/rules/dot-notation' {
708708
allowKeywords?: boolean;
709709
allowPattern?: string;
710710
allowPrivateClassPropertyAccess?: boolean;
711+
allowProtectedClassPropertyAccess?: boolean;
711712
},
712713
],
713714
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy