Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [dot-notation] add allowProtectedClassPropertyAccess option #2622

Merged
merged 2 commits into from Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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