no-jasmine-globals.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. var _default = (0, _utils.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Disallow Jasmine globals',
  14. recommended: 'error'
  15. },
  16. messages: {
  17. illegalGlobal: 'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`',
  18. illegalMethod: 'Illegal usage of `{{ method }}`, prefer `{{ replacement }}`',
  19. illegalFail: 'Illegal usage of `fail`, prefer throwing an error, or the `done.fail` callback',
  20. illegalPending: 'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`',
  21. illegalJasmine: 'Illegal usage of jasmine global'
  22. },
  23. fixable: 'code',
  24. schema: [],
  25. type: 'suggestion'
  26. },
  27. defaultOptions: [],
  28. create(context) {
  29. return {
  30. CallExpression(node) {
  31. const {
  32. callee
  33. } = node;
  34. const calleeName = (0, _utils.getNodeName)(callee);
  35. if (!calleeName) {
  36. return;
  37. }
  38. if (calleeName === 'spyOn' || calleeName === 'spyOnProperty' || calleeName === 'fail' || calleeName === 'pending') {
  39. if ((0, _utils.scopeHasLocalReference)(context.getScope(), calleeName)) {
  40. // It's a local variable, not a jasmine global.
  41. return;
  42. }
  43. switch (calleeName) {
  44. case 'spyOn':
  45. case 'spyOnProperty':
  46. context.report({
  47. node,
  48. messageId: 'illegalGlobal',
  49. data: {
  50. global: calleeName,
  51. replacement: 'jest.spyOn'
  52. }
  53. });
  54. break;
  55. case 'fail':
  56. context.report({
  57. node,
  58. messageId: 'illegalFail'
  59. });
  60. break;
  61. case 'pending':
  62. context.report({
  63. node,
  64. messageId: 'illegalPending'
  65. });
  66. break;
  67. }
  68. return;
  69. }
  70. if (callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && calleeName.startsWith('jasmine.')) {
  71. const functionName = calleeName.replace('jasmine.', '');
  72. if (functionName === 'any' || functionName === 'anything' || functionName === 'arrayContaining' || functionName === 'objectContaining' || functionName === 'stringMatching') {
  73. context.report({
  74. fix: fixer => [fixer.replaceText(callee.object, 'expect')],
  75. node,
  76. messageId: 'illegalMethod',
  77. data: {
  78. method: calleeName,
  79. replacement: `expect.${functionName}`
  80. }
  81. });
  82. return;
  83. }
  84. if (functionName === 'addMatchers') {
  85. context.report({
  86. node,
  87. messageId: 'illegalMethod',
  88. data: {
  89. method: calleeName,
  90. replacement: 'expect.extend'
  91. }
  92. });
  93. return;
  94. }
  95. if (functionName === 'createSpy') {
  96. context.report({
  97. node,
  98. messageId: 'illegalMethod',
  99. data: {
  100. method: calleeName,
  101. replacement: 'jest.fn'
  102. }
  103. });
  104. return;
  105. }
  106. context.report({
  107. node,
  108. messageId: 'illegalJasmine'
  109. });
  110. }
  111. },
  112. MemberExpression(node) {
  113. if ((0, _utils.isSupportedAccessor)(node.object, 'jasmine')) {
  114. const {
  115. parent,
  116. property
  117. } = node;
  118. if (parent && parent.type === _experimentalUtils.AST_NODE_TYPES.AssignmentExpression) {
  119. if ((0, _utils.isSupportedAccessor)(property, 'DEFAULT_TIMEOUT_INTERVAL')) {
  120. const {
  121. right
  122. } = parent;
  123. if (right.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
  124. context.report({
  125. fix: fixer => [fixer.replaceText(parent, `jest.setTimeout(${right.value})`)],
  126. node,
  127. messageId: 'illegalJasmine'
  128. });
  129. return;
  130. }
  131. }
  132. context.report({
  133. node,
  134. messageId: 'illegalJasmine'
  135. });
  136. }
  137. }
  138. }
  139. };
  140. }
  141. });
  142. exports.default = _default;