forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-node-access.ts
87 lines (76 loc) · 2.75 KB
/
no-node-access.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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { isIdentifier, isMemberExpression, isLiteral } from '../node-utils';
import { getDocsUrl, ALL_QUERIES_METHODS, ALL_RETURNING_NODES } from '../utils';
export const RULE_NAME = 'no-node-access';
const ALL_QUERIES_AND_RETURNING_NODES = [
...ALL_QUERIES_METHODS,
...ALL_RETURNING_NODES,
];
export default ESLintUtils.RuleCreator(getDocsUrl)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow direct Node access',
category: 'Best Practices',
recommended: 'error',
},
messages: {
noNodeAccess:
'Avoid direct Node access. Prefer using the methods from Testing Library.',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context) {
const variablesWithNodes: string[] = [];
function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
const isLiteralNumber =
isLiteral(node.property) && typeof node.property.value === 'number';
const hasForbiddenMethod =
isIdentifier(node.property) &&
ALL_RETURNING_NODES.includes(node.property.name);
(isLiteralNumber || hasForbiddenMethod) &&
context.report({
node: node,
loc: node.property.loc.start,
messageId: 'noNodeAccess',
});
}
function checkVariablesWithNodes(node: TSESTree.MemberExpression) {
const callExpression = node.parent as TSESTree.CallExpression;
const variableDeclarator = callExpression.parent as TSESTree.VariableDeclarator;
const methodsNames = ALL_QUERIES_AND_RETURNING_NODES.filter(
method =>
isIdentifier(node.property) && node.property.name.includes(method)
);
if (methodsNames.length) {
variablesWithNodes.push(
isIdentifier(variableDeclarator.id) && variableDeclarator.id.name
);
}
}
function checkDirectNodeAccess(node: TSESTree.Identifier) {
if (variablesWithNodes.includes(node.name)) {
isMemberExpression(node.parent) && showErrorForNodeAccess(node.parent);
}
}
function checkDirectMethodCall(node: TSESTree.CallExpression) {
const methodsNames = ALL_QUERIES_AND_RETURNING_NODES.filter(
method =>
isMemberExpression(node.callee) &&
isIdentifier(node.callee.property) &&
node.callee.property.name.includes(method)
);
if (methodsNames.length && isMemberExpression(node.parent)) {
showErrorForNodeAccess(node.parent);
}
}
return {
['VariableDeclarator > CallExpression > MemberExpression']: checkVariablesWithNodes,
['MemberExpression > Identifier']: checkDirectNodeAccess,
['CallExpression']: checkDirectMethodCall,
};
},
});