Skip to content

Commit 5d02426

Browse files
author
Mateus Felix
committed
refactor(no-debug): extract auxiliary functions
to node-utils, to be used by no-container
1 parent ee369e5 commit 5d02426

File tree

3 files changed

+37
-47
lines changed

3 files changed

+37
-47
lines changed

lib/node-utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,37 @@ export function hasThenProperty(node: TSESTree.Node) {
105105

106106
export function isArrowFunctionExpression(node: TSESTree.Node): node is TSESTree.ArrowFunctionExpression {
107107
return node && node.type === 'ArrowFunctionExpression'
108+
}
109+
110+
function isRenderFunction(
111+
callNode: TSESTree.CallExpression,
112+
renderFunctions: string[]
113+
) {
114+
return ['render', ...renderFunctions].some(
115+
name => isIdentifier(callNode.callee) && name === callNode.callee.name
116+
);
117+
}
118+
119+
export function isRenderVariableDeclarator(
120+
node: TSESTree.VariableDeclarator,
121+
renderFunctions: string[] = []
122+
) {
123+
if (node.init) {
124+
if (isAwaitExpression(node.init)) {
125+
return (
126+
node.init.argument &&
127+
isRenderFunction(
128+
node.init.argument as TSESTree.CallExpression,
129+
renderFunctions
130+
)
131+
);
132+
} else {
133+
return (
134+
isCallExpression(node.init) &&
135+
isRenderFunction(node.init, renderFunctions)
136+
);
137+
}
138+
}
139+
140+
return false;
108141
}

lib/rules/no-container.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@ import {
66
isMemberExpression,
77
isObjectPattern,
88
isProperty,
9+
isRenderVariableDeclarator,
910
} from '../node-utils';
1011

1112
export const RULE_NAME = 'no-container';
1213

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-
2514
export default ESLintUtils.RuleCreator(getDocsUrl)({
2615
name: RULE_NAME,
2716
meta: {
@@ -32,7 +21,8 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
3221
recommended: 'error',
3322
},
3423
messages: {
35-
noContainer: 'Unexpected use of container methods. Prefer the use of "screen.someMethod()".',
24+
noContainer:
25+
'Unexpected use of container methods. Prefer the use of "screen.someMethod()".',
3626
},
3727
fixable: null,
3828
schema: [],

lib/rules/no-debug.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,13 @@ import {
66
isIdentifier,
77
isCallExpression,
88
isLiteral,
9-
isAwaitExpression,
109
isMemberExpression,
1110
isImportSpecifier,
11+
isRenderVariableDeclarator,
1212
} from '../node-utils';
1313

1414
export const RULE_NAME = 'no-debug';
1515

16-
function isRenderFunction(
17-
callNode: TSESTree.CallExpression,
18-
renderFunctions: string[]
19-
) {
20-
return ['render', ...renderFunctions].some(
21-
name => isIdentifier(callNode.callee) && name === callNode.callee.name
22-
);
23-
}
24-
25-
function isRenderVariableDeclarator(
26-
node: TSESTree.VariableDeclarator,
27-
renderFunctions: string[]
28-
) {
29-
if (node.init) {
30-
if (isAwaitExpression(node.init)) {
31-
return (
32-
node.init.argument &&
33-
isRenderFunction(
34-
node.init.argument as TSESTree.CallExpression,
35-
renderFunctions
36-
)
37-
);
38-
} else {
39-
return (
40-
isCallExpression(node.init) &&
41-
isRenderFunction(node.init, renderFunctions)
42-
);
43-
}
44-
}
45-
46-
return false;
47-
}
48-
4916
function hasTestingLibraryImportModule(
5017
importDeclarationNode: TSESTree.ImportDeclaration
5118
) {

0 commit comments

Comments
 (0)