/
dot-notation.ts
126 lines (121 loc) · 3.76 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import * as tsutils from 'tsutils';
import baseRule from 'eslint/lib/rules/dot-notation';
import {
createRule,
getParserServices,
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} 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: {
type: 'boolean',
default: false,
},
allowProtectedClassPropertyAccess: {
type: 'boolean',
default: false,
},
allowIndexSignaturePropertyAccess: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
],
fixable: baseRule.meta.fixable,
messages: baseRule.meta.messages,
},
defaultOptions: [
{
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowIndexSignaturePropertyAccess: false,
allowKeywords: true,
allowPattern: '',
},
],
create(context, [options]) {
const rules = baseRule.create(context);
const { program, esTreeNodeToTSNodeMap } = getParserServices(context);
const typeChecker = program.getTypeChecker();
const allowPrivateClassPropertyAccess =
options.allowPrivateClassPropertyAccess;
const allowProtectedClassPropertyAccess =
options.allowProtectedClassPropertyAccess;
const allowIndexSignaturePropertyAccess =
(options.allowIndexSignaturePropertyAccess ?? false) ||
tsutils.isCompilerOptionEnabled(
program.getCompilerOptions(),
// @ts-expect-error - TS is refining the type to never for some reason
'noPropertyAccessFromIndexSignature',
);
return {
MemberExpression(node: TSESTree.MemberExpression): void {
if (
(allowPrivateClassPropertyAccess ||
allowProtectedClassPropertyAccess ||
allowIndexSignaturePropertyAccess) &&
node.computed
) {
// for perf reasons - only fetch symbols if we have to
const propertySymbol = typeChecker.getSymbolAtLocation(
esTreeNodeToTSNodeMap.get(node.property),
);
const modifierKind = propertySymbol?.getDeclarations()?.[0]
?.modifiers?.[0].kind;
if (
(allowPrivateClassPropertyAccess &&
modifierKind == ts.SyntaxKind.PrivateKeyword) ||
(allowProtectedClassPropertyAccess &&
modifierKind == ts.SyntaxKind.ProtectedKeyword)
) {
return;
}
if (
propertySymbol === undefined &&
allowIndexSignaturePropertyAccess
) {
const objectType = typeChecker.getTypeAtLocation(
esTreeNodeToTSNodeMap.get(node.object),
);
const indexType = typeChecker.getIndexTypeOfType(
objectType,
ts.IndexKind.String,
);
if (indexType != undefined) {
return;
}
}
}
rules.MemberExpression(node);
},
};
},
});