node-utils.js
5.61 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVariableReferences = exports.isPromiseResolved = exports.isAwaited = exports.isArrayExpression = exports.isReturnStatement = exports.isArrowFunctionExpression = exports.isAwaitExpression = exports.hasThenProperty = exports.isObjectExpression = exports.findClosestCallNode = exports.findClosestCallExpressionNode = exports.isJSXAttribute = exports.isProperty = exports.isObjectPattern = exports.isRenderFunction = exports.isVariableDeclarator = exports.isBlockStatement = exports.isImportDefaultSpecifier = exports.isImportNamespaceSpecifier = exports.isImportSpecifier = exports.isLiteral = exports.isMemberExpression = exports.isIdentifier = exports.isCallExpression = void 0;
var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
function isCallExpression(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.CallExpression;
}
exports.isCallExpression = isCallExpression;
function isIdentifier(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.Identifier;
}
exports.isIdentifier = isIdentifier;
function isMemberExpression(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression;
}
exports.isMemberExpression = isMemberExpression;
function isLiteral(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.Literal;
}
exports.isLiteral = isLiteral;
function isImportSpecifier(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.ImportSpecifier;
}
exports.isImportSpecifier = isImportSpecifier;
function isImportNamespaceSpecifier(node) {
return (node === null || node === void 0 ? void 0 : node.type) === experimental_utils_1.AST_NODE_TYPES.ImportNamespaceSpecifier;
}
exports.isImportNamespaceSpecifier = isImportNamespaceSpecifier;
function isImportDefaultSpecifier(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.ImportDefaultSpecifier;
}
exports.isImportDefaultSpecifier = isImportDefaultSpecifier;
function isBlockStatement(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.BlockStatement;
}
exports.isBlockStatement = isBlockStatement;
function isVariableDeclarator(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.VariableDeclarator;
}
exports.isVariableDeclarator = isVariableDeclarator;
function isRenderFunction(callNode, renderFunctions) {
return renderFunctions.some(function (name) {
return ((isIdentifier(callNode.callee) && name === callNode.callee.name) ||
(isMemberExpression(callNode.callee) &&
isIdentifier(callNode.callee.property) &&
name === callNode.callee.property.name));
});
}
exports.isRenderFunction = isRenderFunction;
function isObjectPattern(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.ObjectPattern;
}
exports.isObjectPattern = isObjectPattern;
function isProperty(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.Property;
}
exports.isProperty = isProperty;
function isJSXAttribute(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.JSXAttribute;
}
exports.isJSXAttribute = isJSXAttribute;
function findClosestCallExpressionNode(node) {
if (isCallExpression(node)) {
return node;
}
if (!node.parent)
return null;
return findClosestCallExpressionNode(node.parent);
}
exports.findClosestCallExpressionNode = findClosestCallExpressionNode;
function findClosestCallNode(node, name) {
if (!node.parent) {
return null;
}
if (isCallExpression(node) &&
isIdentifier(node.callee) &&
node.callee.name === name) {
return node;
}
else {
return findClosestCallNode(node.parent, name);
}
}
exports.findClosestCallNode = findClosestCallNode;
function isObjectExpression(node) {
return (node === null || node === void 0 ? void 0 : node.type) === experimental_utils_1.AST_NODE_TYPES.ObjectExpression;
}
exports.isObjectExpression = isObjectExpression;
function hasThenProperty(node) {
return (isMemberExpression(node) &&
isIdentifier(node.property) &&
node.property.name === 'then');
}
exports.hasThenProperty = hasThenProperty;
function isAwaitExpression(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.AwaitExpression;
}
exports.isAwaitExpression = isAwaitExpression;
function isArrowFunctionExpression(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression;
}
exports.isArrowFunctionExpression = isArrowFunctionExpression;
function isReturnStatement(node) {
return node && node.type === experimental_utils_1.AST_NODE_TYPES.ReturnStatement;
}
exports.isReturnStatement = isReturnStatement;
function isArrayExpression(node) {
return (node === null || node === void 0 ? void 0 : node.type) === experimental_utils_1.AST_NODE_TYPES.ArrayExpression;
}
exports.isArrayExpression = isArrayExpression;
function isAwaited(node) {
return (isAwaitExpression(node) ||
isArrowFunctionExpression(node) ||
isReturnStatement(node));
}
exports.isAwaited = isAwaited;
function isPromiseResolved(node) {
var parent = node.parent;
if (isCallExpression(parent)) {
return hasThenProperty(parent.parent);
}
return hasThenProperty(parent);
}
exports.isPromiseResolved = isPromiseResolved;
function getVariableReferences(context, node) {
return ((isVariableDeclarator(node) &&
context.getDeclaredVariables(node)[0].references.slice(1)) ||
[]);
}
exports.getVariableReferences = getVariableReferences;