node-utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. 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;
  4. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  5. function isCallExpression(node) {
  6. return node && node.type === experimental_utils_1.AST_NODE_TYPES.CallExpression;
  7. }
  8. exports.isCallExpression = isCallExpression;
  9. function isIdentifier(node) {
  10. return node && node.type === experimental_utils_1.AST_NODE_TYPES.Identifier;
  11. }
  12. exports.isIdentifier = isIdentifier;
  13. function isMemberExpression(node) {
  14. return node && node.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression;
  15. }
  16. exports.isMemberExpression = isMemberExpression;
  17. function isLiteral(node) {
  18. return node && node.type === experimental_utils_1.AST_NODE_TYPES.Literal;
  19. }
  20. exports.isLiteral = isLiteral;
  21. function isImportSpecifier(node) {
  22. return node && node.type === experimental_utils_1.AST_NODE_TYPES.ImportSpecifier;
  23. }
  24. exports.isImportSpecifier = isImportSpecifier;
  25. function isImportNamespaceSpecifier(node) {
  26. return (node === null || node === void 0 ? void 0 : node.type) === experimental_utils_1.AST_NODE_TYPES.ImportNamespaceSpecifier;
  27. }
  28. exports.isImportNamespaceSpecifier = isImportNamespaceSpecifier;
  29. function isImportDefaultSpecifier(node) {
  30. return node && node.type === experimental_utils_1.AST_NODE_TYPES.ImportDefaultSpecifier;
  31. }
  32. exports.isImportDefaultSpecifier = isImportDefaultSpecifier;
  33. function isBlockStatement(node) {
  34. return node && node.type === experimental_utils_1.AST_NODE_TYPES.BlockStatement;
  35. }
  36. exports.isBlockStatement = isBlockStatement;
  37. function isVariableDeclarator(node) {
  38. return node && node.type === experimental_utils_1.AST_NODE_TYPES.VariableDeclarator;
  39. }
  40. exports.isVariableDeclarator = isVariableDeclarator;
  41. function isRenderFunction(callNode, renderFunctions) {
  42. return renderFunctions.some(function (name) {
  43. return ((isIdentifier(callNode.callee) && name === callNode.callee.name) ||
  44. (isMemberExpression(callNode.callee) &&
  45. isIdentifier(callNode.callee.property) &&
  46. name === callNode.callee.property.name));
  47. });
  48. }
  49. exports.isRenderFunction = isRenderFunction;
  50. function isObjectPattern(node) {
  51. return node && node.type === experimental_utils_1.AST_NODE_TYPES.ObjectPattern;
  52. }
  53. exports.isObjectPattern = isObjectPattern;
  54. function isProperty(node) {
  55. return node && node.type === experimental_utils_1.AST_NODE_TYPES.Property;
  56. }
  57. exports.isProperty = isProperty;
  58. function isJSXAttribute(node) {
  59. return node && node.type === experimental_utils_1.AST_NODE_TYPES.JSXAttribute;
  60. }
  61. exports.isJSXAttribute = isJSXAttribute;
  62. function findClosestCallExpressionNode(node) {
  63. if (isCallExpression(node)) {
  64. return node;
  65. }
  66. if (!node.parent)
  67. return null;
  68. return findClosestCallExpressionNode(node.parent);
  69. }
  70. exports.findClosestCallExpressionNode = findClosestCallExpressionNode;
  71. function findClosestCallNode(node, name) {
  72. if (!node.parent) {
  73. return null;
  74. }
  75. if (isCallExpression(node) &&
  76. isIdentifier(node.callee) &&
  77. node.callee.name === name) {
  78. return node;
  79. }
  80. else {
  81. return findClosestCallNode(node.parent, name);
  82. }
  83. }
  84. exports.findClosestCallNode = findClosestCallNode;
  85. function isObjectExpression(node) {
  86. return (node === null || node === void 0 ? void 0 : node.type) === experimental_utils_1.AST_NODE_TYPES.ObjectExpression;
  87. }
  88. exports.isObjectExpression = isObjectExpression;
  89. function hasThenProperty(node) {
  90. return (isMemberExpression(node) &&
  91. isIdentifier(node.property) &&
  92. node.property.name === 'then');
  93. }
  94. exports.hasThenProperty = hasThenProperty;
  95. function isAwaitExpression(node) {
  96. return node && node.type === experimental_utils_1.AST_NODE_TYPES.AwaitExpression;
  97. }
  98. exports.isAwaitExpression = isAwaitExpression;
  99. function isArrowFunctionExpression(node) {
  100. return node && node.type === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression;
  101. }
  102. exports.isArrowFunctionExpression = isArrowFunctionExpression;
  103. function isReturnStatement(node) {
  104. return node && node.type === experimental_utils_1.AST_NODE_TYPES.ReturnStatement;
  105. }
  106. exports.isReturnStatement = isReturnStatement;
  107. function isArrayExpression(node) {
  108. return (node === null || node === void 0 ? void 0 : node.type) === experimental_utils_1.AST_NODE_TYPES.ArrayExpression;
  109. }
  110. exports.isArrayExpression = isArrayExpression;
  111. function isAwaited(node) {
  112. return (isAwaitExpression(node) ||
  113. isArrowFunctionExpression(node) ||
  114. isReturnStatement(node));
  115. }
  116. exports.isAwaited = isAwaited;
  117. function isPromiseResolved(node) {
  118. var parent = node.parent;
  119. if (isCallExpression(parent)) {
  120. return hasThenProperty(parent.parent);
  121. }
  122. return hasThenProperty(parent);
  123. }
  124. exports.isPromiseResolved = isPromiseResolved;
  125. function getVariableReferences(context, node) {
  126. return ((isVariableDeclarator(node) &&
  127. context.getDeclaredVariables(node)[0].references.slice(1)) ||
  128. []);
  129. }
  130. exports.getVariableReferences = getVariableReferences;