-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathno-node-access.ts
44 lines (40 loc) · 1.25 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
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { ALL_RETURNING_NODES } from '../utils';
import { isIdentifier } from '../node-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
export const RULE_NAME = 'no-node-access';
export type MessageIds = 'noNodeAccess';
type Options = [];
export default createTestingLibraryRule<Options, MessageIds>({
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) {
function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
isIdentifier(node.property) &&
ALL_RETURNING_NODES.includes(node.property.name) &&
context.report({
node: node,
loc: node.property.loc.start,
messageId: 'noNodeAccess',
});
}
return {
['ExpressionStatement MemberExpression']: showErrorForNodeAccess,
['VariableDeclarator MemberExpression']: showErrorForNodeAccess,
};
},
});