-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
dot-notation.ts
122 lines (116 loc) · 3.69 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
import type { TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../util';
import { createRule, getModifiers, getParserServices } from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
const baseRule = getESLintCoreRule('dot-notation');
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',
recommended: 'strict',
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,
hasSuggestions: baseRule.meta.hasSuggestions,
messages: baseRule.meta.messages,
},
defaultOptions: [
{
allowPrivateClassPropertyAccess: false,
allowProtectedClassPropertyAccess: false,
allowIndexSignaturePropertyAccess: false,
allowKeywords: true,
allowPattern: '',
},
],
create(context, [options]) {
const rules = baseRule.create(context);
const services = getParserServices(context);
const allowPrivateClassPropertyAccess =
options.allowPrivateClassPropertyAccess;
const allowProtectedClassPropertyAccess =
options.allowProtectedClassPropertyAccess;
const allowIndexSignaturePropertyAccess =
(options.allowIndexSignaturePropertyAccess ?? false) ||
tsutils.isCompilerOptionEnabled(
services.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 = services.getSymbolAtLocation(node.property);
const modifierKind = getModifiers(
propertySymbol?.getDeclarations()?.[0],
)?.[0].kind;
if (
(allowPrivateClassPropertyAccess &&
modifierKind === ts.SyntaxKind.PrivateKeyword) ||
(allowProtectedClassPropertyAccess &&
modifierKind === ts.SyntaxKind.ProtectedKeyword)
) {
return;
}
if (
propertySymbol === undefined &&
allowIndexSignaturePropertyAccess
) {
const objectType = services.getTypeAtLocation(node.object);
const indexType = objectType
.getNonNullableType()
.getStringIndexType();
if (indexType !== undefined) {
return;
}
}
}
rules.MemberExpression(node);
},
};
},
});