forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-utils.ts
141 lines (120 loc) · 3.32 KB
/
node-utils.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
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
import { TSESTree } from '@typescript-eslint/experimental-utils';
export function isCallExpression(
node: TSESTree.Node
): node is TSESTree.CallExpression {
return node && node.type === 'CallExpression';
}
export function isAwaitExpression(
node: TSESTree.Node
): node is TSESTree.AwaitExpression {
return node && node.type === 'AwaitExpression';
}
export function isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {
return node && node.type === 'Identifier';
}
export function isMemberExpression(
node: TSESTree.Node
): node is TSESTree.MemberExpression {
return node && node.type === 'MemberExpression';
}
export function isLiteral(node: TSESTree.Node): node is TSESTree.Literal {
return node && node.type === 'Literal';
}
export function isImportSpecifier(
node: TSESTree.Node
): node is TSESTree.ImportSpecifier {
return node && node.type === 'ImportSpecifier';
}
export function isImportDefaultSpecifier(
node: TSESTree.Node
): node is TSESTree.ImportDefaultSpecifier {
return node && node.type === 'ImportDefaultSpecifier';
}
export function isBlockStatement(
node: TSESTree.Node
): node is TSESTree.BlockStatement {
return node && node.type === 'BlockStatement';
}
export function isVariableDeclarator(
node: TSESTree.Node
): node is TSESTree.VariableDeclarator {
return node && node.type === 'VariableDeclarator';
}
export function isObjectPattern(
node: TSESTree.Node
): node is TSESTree.ObjectPattern {
return node && node.type === 'ObjectPattern';
}
export function isProperty(node: TSESTree.Node): node is TSESTree.Property {
return node && node.type === 'Property';
}
export function isJSXAttribute(
node: TSESTree.Node
): node is TSESTree.JSXAttribute {
return node && node.type === 'JSXAttribute';
}
export function findClosestCallExpressionNode(
node: TSESTree.Node
): TSESTree.CallExpression {
if (isCallExpression(node)) {
return node;
}
return findClosestCallExpressionNode(node.parent);
}
export function findClosestCallNode(
node: TSESTree.Node,
name: string
): TSESTree.CallExpression {
if (!node.parent) {
return null;
}
if (
isCallExpression(node) &&
isIdentifier(node.callee) &&
node.callee.name === name
) {
return node;
} else {
return findClosestCallNode(node.parent, name);
}
}
export function hasThenProperty(node: TSESTree.Node) {
return (
isMemberExpression(node) &&
isIdentifier(node.property) &&
node.property.name === 'then'
);
}
export function isArrowFunctionExpression(node: TSESTree.Node): node is TSESTree.ArrowFunctionExpression {
return node && node.type === 'ArrowFunctionExpression'
}
function isRenderFunction(
callNode: TSESTree.CallExpression,
renderFunctions: string[]
) {
return ['render', ...renderFunctions].some(
name => isIdentifier(callNode.callee) && name === callNode.callee.name
);
}
export function isRenderVariableDeclarator(
node: TSESTree.VariableDeclarator,
renderFunctions: string[]
) {
if (node.init) {
if (isAwaitExpression(node.init)) {
return (
node.init.argument &&
isRenderFunction(
node.init.argument as TSESTree.CallExpression,
renderFunctions
)
);
} else {
return (
isCallExpression(node.init) &&
isRenderFunction(node.init, renderFunctions)
);
}
}
return false;
}