Skip to content

Commit da88a9d

Browse files
author
Mateus Felix
committed
refactor(no-container): set rules
1 parent 094f53c commit da88a9d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

lib/rules/no-container.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { getDocsUrl } from '../utils';
3+
import {
4+
isCallExpression,
5+
isIdentifier,
6+
isMemberExpression,
7+
isObjectPattern,
8+
isProperty,
9+
} from '../node-utils';
10+
11+
export const RULE_NAME = 'no-container';
12+
13+
function isRender(callNode: TSESTree.CallExpression) {
14+
return isIdentifier(callNode.callee) && callNode.callee.name === 'render';
15+
}
16+
17+
function isRenderVariableDeclarator(node: TSESTree.VariableDeclarator) {
18+
if (node.init) {
19+
if (isCallExpression(node.init)) {
20+
return isRender(node.init);
21+
}
22+
}
23+
}
24+
25+
export default ESLintUtils.RuleCreator(getDocsUrl)({
26+
name: RULE_NAME,
27+
meta: {
28+
type: 'problem',
29+
docs: {
30+
description: 'Disallow the usage of container methods',
31+
category: 'Best Practices',
32+
recommended: 'error',
33+
},
34+
messages: {
35+
noContainer: 'Unexpected use of container methods. Prefer the use of "screen.someMethod()".',
36+
},
37+
fixable: null,
38+
schema: [],
39+
},
40+
defaultOptions: [],
41+
42+
create(context) {
43+
let destructuredContainerName = '';
44+
45+
return {
46+
VariableDeclarator(node) {
47+
if (isRenderVariableDeclarator(node)) {
48+
if (isObjectPattern(node.id)) {
49+
const containerIndex = node.id.properties.findIndex(
50+
property =>
51+
isProperty(property) &&
52+
isIdentifier(property.key) &&
53+
property.key.name === 'container'
54+
);
55+
const nodeValue = node.id.properties[containerIndex].value;
56+
destructuredContainerName =
57+
containerIndex !== -1 &&
58+
isIdentifier(nodeValue) &&
59+
nodeValue.name;
60+
}
61+
}
62+
},
63+
64+
[`CallExpression`](node: TSESTree.CallExpression) {
65+
if (
66+
isMemberExpression(node.callee) &&
67+
isIdentifier(node.callee.object) &&
68+
node.callee.object.name === destructuredContainerName
69+
) {
70+
context.report({
71+
node,
72+
messageId: 'noContainer',
73+
});
74+
}
75+
},
76+
};
77+
},
78+
});

0 commit comments

Comments
 (0)