-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathno-node-access.ts
More file actions
147 lines (131 loc) · 3.45 KB
/
no-node-access.ts
File metadata and controls
147 lines (131 loc) · 3.45 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { ASTUtils } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
getDeepestIdentifierNode,
isLiteral,
isMemberExpression,
} from '../node-utils';
import {
ALL_QUERIES_COMBINATIONS,
ALL_RETURNING_NODES,
EVENT_HANDLER_METHODS,
resolveToTestingLibraryFn,
} from '../utils';
import type { TSESTree } from '@typescript-eslint/utils';
const RULE_NAME = 'no-node-access';
export type MessageIds = 'noNodeAccess';
export type Options = [{ allowContainerFirstChild: boolean }];
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow direct Node access',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
svelte: 'error',
marko: 'error',
},
},
messages: {
noNodeAccess:
'Avoid direct Node access. Prefer using the methods from Testing Library.',
},
schema: [
{
type: 'object',
properties: {
allowContainerFirstChild: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
allowContainerFirstChild: false,
},
],
create(context, [{ allowContainerFirstChild = false }], helpers) {
function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
// This rule is so aggressive that can cause tons of false positives outside test files when Aggressive Reporting
// is enabled. Because of that, this rule will skip this mechanism and report only if some Testing Library package
// or custom one (set in utils-module Shared Setting) is found.
if (!helpers.isTestingLibraryImported(true)) {
return;
}
// Skip if this MemberExpression is the object of another MemberExpression
// to avoid duplicate reports for chained property access
if (isMemberExpression(node.parent) && node.parent.object === node) {
return;
}
const propertyName = ASTUtils.isIdentifier(node.property)
? node.property.name
: null;
if (
propertyName &&
ALL_RETURNING_NODES.some(
(allReturningNode) => allReturningNode === propertyName
)
) {
if (allowContainerFirstChild && propertyName === 'firstChild') {
return;
}
if (
ASTUtils.isIdentifier(node.object) &&
node.object.name === 'props'
) {
return;
}
context.report({
node,
loc: node.property.loc.start,
messageId: 'noNodeAccess',
});
}
}
function getProperty(
node: TSESTree.PrivateIdentifier | TSESTree.Expression
) {
if (isLiteral(node)) {
return node;
}
return getDeepestIdentifierNode(node);
}
return {
CallExpression(node: TSESTree.CallExpression) {
if (!isMemberExpression(node.callee)) return;
const { callee } = node;
if (
!EVENT_HANDLER_METHODS.some(
(method) => method === ASTUtils.getPropertyName(callee)
)
) {
return;
}
const identifier = getDeepestIdentifierNode(callee.object);
if (
!identifier ||
!ALL_QUERIES_COMBINATIONS.includes(identifier.name)
) {
return;
}
if (resolveToTestingLibraryFn(node, context)) {
const property = getProperty(callee.property);
context.report({
node,
loc: property?.loc.start,
messageId: 'noNodeAccess',
});
}
},
'ExpressionStatement MemberExpression': showErrorForNodeAccess,
'VariableDeclarator MemberExpression': showErrorForNodeAccess,
};
},
});