Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a85c3e1

Browse files
authored
feat(eslint-plugin): add extension rule dot-notation (typescript-eslint#1867)
1 parent f667ff1 commit a85c3e1

File tree

7 files changed

+409
-0
lines changed

7 files changed

+409
-0
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ In these cases, we create what we call an extension rule; a rule within our plug
182182
| [`@typescript-eslint/brace-style`](./docs/rules/brace-style.md) | Enforce consistent brace style for blocks | | :wrench: | |
183183
| [`@typescript-eslint/comma-spacing`](./docs/rules/comma-spacing.md) | Enforces consistent spacing before and after commas | | :wrench: | |
184184
| [`@typescript-eslint/default-param-last`](./docs/rules/default-param-last.md) | Enforce default parameters to be last | | | |
185+
| [`@typescript-eslint/dot-notation`](./docs/rules/dot-notation.md) | enforce dot notation whenever possible | | :wrench: | :thought_balloon: |
185186
| [`@typescript-eslint/func-call-spacing`](./docs/rules/func-call-spacing.md) | Require or disallow spacing between function identifiers and their invocations | | :wrench: | |
186187
| [`@typescript-eslint/indent`](./docs/rules/indent.md) | Enforce consistent indentation | | :wrench: | |
187188
| [`@typescript-eslint/init-declarations`](./docs/rules/init-declarations.md) | require or disallow initialization in variable declarations | | | |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# enforce dot notation whenever possible (`dot-notation`)
2+
3+
## Rule Details
4+
5+
This rule extends the base [`eslint/dot-notation`](https://eslint.org/docs/rules/dot-notation) rule.
6+
It adds support for optionally ignoring computed `private` member access.
7+
8+
## How to use
9+
10+
```cjson
11+
{
12+
// note you must disable the base rule as it can report incorrect errors
13+
"dot-notation": "off",
14+
"@typescript-eslint/dot-notation": ["error"]
15+
}
16+
```
17+
18+
## Options
19+
20+
See [`eslint/dot-notation`](https://eslint.org/docs/rules/dot-notation#options) options.
21+
This rule adds the following options:
22+
23+
```ts
24+
interface Options extends BaseDotNotationOptions {
25+
allowPrivateClassPropertyAccess?: boolean;
26+
}
27+
const defaultOptions: Options = {
28+
...baseDotNotationDefaultOptions,
29+
allowPrivateClassPropertyAccess: false,
30+
};
31+
```
32+
33+
### `allowPrivateClassPropertyAccess`
34+
35+
Example of a correct code when `allowPrivateClassPropertyAccess` is set to `true`
36+
37+
```ts
38+
class X {
39+
private priv_prop = 123;
40+
}
41+
42+
const x = new X();
43+
x['priv_prop'] = 123;
44+
```
45+
46+
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/dot-notation.md)</sup>

packages/eslint-plugin/src/configs/all.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"@typescript-eslint/consistent-type-definitions": "error",
1616
"default-param-last": "off",
1717
"@typescript-eslint/default-param-last": "error",
18+
"dot-notation": "off",
19+
"@typescript-eslint/dot-notation": "error",
1820
"@typescript-eslint/explicit-function-return-type": "error",
1921
"@typescript-eslint/explicit-member-accessibility": "error",
2022
"@typescript-eslint/explicit-module-boundary-types": "error",
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import * as ts from 'typescript';
3+
import baseRule from 'eslint/lib/rules/dot-notation';
4+
import {
5+
InferOptionsTypeFromRule,
6+
InferMessageIdsTypeFromRule,
7+
createRule,
8+
getParserServices,
9+
} from '../util';
10+
11+
export type Options = InferOptionsTypeFromRule<typeof baseRule>;
12+
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;
13+
14+
export default createRule<Options, MessageIds>({
15+
name: 'dot-notation',
16+
meta: {
17+
type: 'suggestion',
18+
docs: {
19+
description: 'enforce dot notation whenever possible',
20+
category: 'Best Practices',
21+
recommended: false,
22+
extendsBaseRule: true,
23+
requiresTypeChecking: true,
24+
},
25+
schema: [
26+
{
27+
type: 'object',
28+
properties: {
29+
allowKeywords: {
30+
type: 'boolean',
31+
default: true,
32+
},
33+
allowPattern: {
34+
type: 'string',
35+
default: '',
36+
},
37+
allowPrivateClassPropertyAccess: {
38+
tyoe: 'boolean',
39+
default: false,
40+
},
41+
},
42+
additionalProperties: false,
43+
},
44+
],
45+
fixable: baseRule.meta.fixable,
46+
messages: baseRule.meta.messages,
47+
},
48+
defaultOptions: [
49+
{
50+
allowPrivateClassPropertyAccess: false,
51+
allowKeywords: true,
52+
allowPattern: '',
53+
},
54+
],
55+
create(context, [options]) {
56+
const rules = baseRule.create(context);
57+
const allowPrivateClassPropertyAccess =
58+
options.allowPrivateClassPropertyAccess;
59+
60+
const parserServices = getParserServices(context);
61+
const typeChecker = parserServices.program.getTypeChecker();
62+
63+
return {
64+
MemberExpression(node: TSESTree.MemberExpression): void {
65+
const objectSymbol = typeChecker.getSymbolAtLocation(
66+
parserServices.esTreeNodeToTSNodeMap.get(node.property),
67+
);
68+
69+
if (
70+
allowPrivateClassPropertyAccess &&
71+
objectSymbol?.declarations[0]?.modifiers?.[0].kind ===
72+
ts.SyntaxKind.PrivateKeyword
73+
) {
74+
return;
75+
}
76+
rules.MemberExpression(node);
77+
},
78+
};
79+
},
80+
});

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import commaSpacing from './comma-spacing';
1212
import consistentTypeAssertions from './consistent-type-assertions';
1313
import consistentTypeDefinitions from './consistent-type-definitions';
1414
import defaultParamLast from './default-param-last';
15+
import dotNotation from './dot-notation';
1516
import explicitFunctionReturnType from './explicit-function-return-type';
1617
import explicitMemberAccessibility from './explicit-member-accessibility';
1718
import explicitModuleBoundaryTypes from './explicit-module-boundary-types';
@@ -114,6 +115,7 @@ export default {
114115
'consistent-type-assertions': consistentTypeAssertions,
115116
'consistent-type-definitions': consistentTypeDefinitions,
116117
'default-param-last': defaultParamLast,
118+
'dot-notation': dotNotation,
117119
'explicit-function-return-type': explicitFunctionReturnType,
118120
'explicit-member-accessibility': explicitMemberAccessibility,
119121
'explicit-module-boundary-types': explicitModuleBoundaryTypes,

0 commit comments

Comments
 (0)