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