no-debug.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  3. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  4. to[j] = from[i];
  5. return to;
  6. };
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.RULE_NAME = void 0;
  9. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  10. var utils_1 = require("../utils");
  11. var node_utils_1 = require("../node-utils");
  12. exports.RULE_NAME = 'no-debug';
  13. function isRenderVariableDeclarator(node, renderFunctions) {
  14. if (node.init) {
  15. if (node_utils_1.isAwaitExpression(node.init)) {
  16. return (node.init.argument &&
  17. node_utils_1.isRenderFunction(node.init.argument, __spreadArray([
  18. 'render'
  19. ], renderFunctions)));
  20. }
  21. else {
  22. return (node_utils_1.isCallExpression(node.init) &&
  23. node_utils_1.isRenderFunction(node.init, __spreadArray(['render'], renderFunctions)));
  24. }
  25. }
  26. return false;
  27. }
  28. function hasTestingLibraryImportModule(importDeclarationNode) {
  29. var literal = importDeclarationNode.source;
  30. return utils_1.LIBRARY_MODULES.some(function (module) { return module === literal.value; });
  31. }
  32. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  33. name: exports.RULE_NAME,
  34. meta: {
  35. type: 'problem',
  36. docs: {
  37. description: 'Disallow unnecessary debug usages in the tests',
  38. category: 'Best Practices',
  39. recommended: 'warn',
  40. },
  41. messages: {
  42. noDebug: 'Unexpected debug statement',
  43. },
  44. fixable: null,
  45. schema: [
  46. {
  47. type: 'object',
  48. properties: {
  49. renderFunctions: {
  50. type: 'array',
  51. },
  52. },
  53. },
  54. ],
  55. },
  56. defaultOptions: [
  57. {
  58. renderFunctions: [],
  59. },
  60. ],
  61. create: function (context, _a) {
  62. var _b;
  63. var options = _a[0];
  64. var hasDestructuredDebugStatement = false;
  65. var renderVariableDeclarators = [];
  66. var renderFunctions = options.renderFunctions;
  67. var hasImportedScreen = false;
  68. var wildcardImportName = null;
  69. return _b = {
  70. VariableDeclarator: function (node) {
  71. if (isRenderVariableDeclarator(node, renderFunctions)) {
  72. if (node_utils_1.isObjectPattern(node.id) &&
  73. node.id.properties.some(function (property) {
  74. return node_utils_1.isProperty(property) &&
  75. node_utils_1.isIdentifier(property.key) &&
  76. property.key.name === 'debug';
  77. })) {
  78. hasDestructuredDebugStatement = true;
  79. }
  80. if (node.id.type === 'Identifier') {
  81. renderVariableDeclarators.push(node);
  82. }
  83. }
  84. }
  85. },
  86. _b["VariableDeclarator > CallExpression > Identifier[name=\"require\"]"] = function (node) {
  87. var args = node.parent.arguments;
  88. var literalNodeScreenModuleName = args.find(function (args) {
  89. return node_utils_1.isLiteral(args) &&
  90. typeof args.value === 'string' &&
  91. utils_1.LIBRARY_MODULES.includes(args.value);
  92. });
  93. if (!literalNodeScreenModuleName) {
  94. return;
  95. }
  96. var declaratorNode = node.parent
  97. .parent;
  98. hasImportedScreen =
  99. node_utils_1.isObjectPattern(declaratorNode.id) &&
  100. declaratorNode.id.properties.some(function (property) {
  101. return node_utils_1.isProperty(property) &&
  102. node_utils_1.isIdentifier(property.key) &&
  103. property.key.name === 'screen';
  104. });
  105. },
  106. _b.ImportDeclaration = function (node) {
  107. if (!hasTestingLibraryImportModule(node))
  108. return;
  109. hasImportedScreen = node.specifiers.some(function (s) { return node_utils_1.isImportSpecifier(s) && s.imported.name === 'screen'; });
  110. },
  111. _b['ImportDeclaration ImportNamespaceSpecifier'] = function (node) {
  112. var importDeclarationNode = node.parent;
  113. if (!hasTestingLibraryImportModule(importDeclarationNode))
  114. return;
  115. wildcardImportName = node.local && node.local.name;
  116. },
  117. _b["CallExpression > Identifier[name=\"debug\"]"] = function (node) {
  118. if (hasDestructuredDebugStatement) {
  119. context.report({
  120. node: node,
  121. messageId: 'noDebug',
  122. });
  123. }
  124. },
  125. _b["CallExpression > MemberExpression > Identifier[name=\"debug\"]"] = function (node) {
  126. var memberExpression = node.parent;
  127. var identifier = memberExpression.object;
  128. var memberExpressionName = identifier.name;
  129. var isScreenDebugUsed = hasImportedScreen && memberExpressionName === 'screen';
  130. var isNamespaceDebugUsed = wildcardImportName && memberExpressionName === wildcardImportName;
  131. if (isScreenDebugUsed || isNamespaceDebugUsed) {
  132. context.report({
  133. node: node,
  134. messageId: 'noDebug',
  135. });
  136. }
  137. },
  138. _b['Program:exit'] = function () {
  139. renderVariableDeclarators.forEach(function (renderVar) {
  140. var renderVarReferences = context
  141. .getDeclaredVariables(renderVar)[0]
  142. .references.slice(1);
  143. renderVarReferences.forEach(function (ref) {
  144. var parent = ref.identifier.parent;
  145. if (node_utils_1.isMemberExpression(parent) &&
  146. node_utils_1.isIdentifier(parent.property) &&
  147. parent.property.name === 'debug' &&
  148. node_utils_1.isCallExpression(parent.parent)) {
  149. context.report({
  150. node: parent.property,
  151. messageId: 'noDebug',
  152. });
  153. }
  154. });
  155. });
  156. },
  157. _b;
  158. },
  159. });