Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prefer-expect-assertions.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const isExpectAssertionsOrHasAssertionsCall = expression => expression.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && expression.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(expression.callee.object, 'expect') && (0, _utils.isSupportedAccessor)(expression.callee.property) && ['assertions', 'hasAssertions'].includes((0, _utils.getAccessorValue)(expression.callee.property));
  9. const isFirstLineExprStmt = functionBody => functionBody[0] && functionBody[0].type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement;
  10. const suggestRemovingExtraArguments = (args, extraArgsStartAt) => ({
  11. messageId: 'suggestRemovingExtraArguments',
  12. fix: fixer => fixer.removeRange([args[extraArgsStartAt].range[0] - Math.sign(extraArgsStartAt), args[args.length - 1].range[1]])
  13. });
  14. const suggestions = [['suggestAddingHasAssertions', 'expect.hasAssertions();'], ['suggestAddingAssertions', 'expect.assertions();']];
  15. var _default = (0, _utils.createRule)({
  16. name: __filename,
  17. meta: {
  18. docs: {
  19. category: 'Best Practices',
  20. description: 'Suggest using `expect.assertions()` OR `expect.hasAssertions()`',
  21. recommended: false,
  22. suggestion: true
  23. },
  24. messages: {
  25. hasAssertionsTakesNoArguments: '`expect.hasAssertions` expects no arguments',
  26. assertionsRequiresOneArgument: '`expect.assertions` excepts a single argument of type number',
  27. assertionsRequiresNumberArgument: 'This argument should be a number',
  28. haveExpectAssertions: 'Every test should have either `expect.assertions(<number of assertions>)` or `expect.hasAssertions()` as its first expression',
  29. suggestAddingHasAssertions: 'Add `expect.hasAssertions()`',
  30. suggestAddingAssertions: 'Add `expect.assertions(<number of assertions>)`',
  31. suggestRemovingExtraArguments: 'Remove extra arguments'
  32. },
  33. type: 'suggestion',
  34. hasSuggestions: true,
  35. schema: [{
  36. type: 'object',
  37. properties: {
  38. onlyFunctionsWithAsyncKeyword: {
  39. type: 'boolean'
  40. }
  41. },
  42. additionalProperties: false
  43. }]
  44. },
  45. defaultOptions: [{
  46. onlyFunctionsWithAsyncKeyword: false
  47. }],
  48. create(context, [options]) {
  49. return {
  50. CallExpression(node) {
  51. if (!(0, _utils.isTestCaseCall)(node)) {
  52. return;
  53. }
  54. if (node.arguments.length < 2) {
  55. return;
  56. }
  57. const [, testFn] = node.arguments;
  58. if (!(0, _utils.isFunction)(testFn) || testFn.body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement || options.onlyFunctionsWithAsyncKeyword && !testFn.async) {
  59. return;
  60. }
  61. const testFuncBody = testFn.body.body;
  62. if (!isFirstLineExprStmt(testFuncBody)) {
  63. context.report({
  64. messageId: 'haveExpectAssertions',
  65. node,
  66. suggest: suggestions.map(([messageId, text]) => ({
  67. messageId,
  68. fix: fixer => fixer.insertTextBeforeRange([testFn.body.range[0] + 1, testFn.body.range[1]], text)
  69. }))
  70. });
  71. return;
  72. }
  73. const testFuncFirstLine = testFuncBody[0].expression;
  74. if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
  75. context.report({
  76. messageId: 'haveExpectAssertions',
  77. node,
  78. suggest: suggestions.map(([messageId, text]) => ({
  79. messageId,
  80. fix: fixer => fixer.insertTextBefore(testFuncBody[0], text)
  81. }))
  82. });
  83. return;
  84. }
  85. if ((0, _utils.isSupportedAccessor)(testFuncFirstLine.callee.property, 'hasAssertions')) {
  86. if (testFuncFirstLine.arguments.length) {
  87. context.report({
  88. messageId: 'hasAssertionsTakesNoArguments',
  89. node: testFuncFirstLine.callee.property,
  90. suggest: [suggestRemovingExtraArguments(testFuncFirstLine.arguments, 0)]
  91. });
  92. }
  93. return;
  94. }
  95. if (!(0, _utils.hasOnlyOneArgument)(testFuncFirstLine)) {
  96. let {
  97. loc
  98. } = testFuncFirstLine.callee.property;
  99. const suggest = [];
  100. if (testFuncFirstLine.arguments.length) {
  101. loc = testFuncFirstLine.arguments[1].loc;
  102. suggest.push(suggestRemovingExtraArguments(testFuncFirstLine.arguments, 1));
  103. }
  104. context.report({
  105. messageId: 'assertionsRequiresOneArgument',
  106. suggest,
  107. loc
  108. });
  109. return;
  110. }
  111. const [arg] = testFuncFirstLine.arguments;
  112. if (arg.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof arg.value === 'number' && Number.isInteger(arg.value)) {
  113. return;
  114. }
  115. context.report({
  116. messageId: 'assertionsRequiresNumberArgument',
  117. node: arg
  118. });
  119. }
  120. };
  121. }
  122. });
  123. exports.default = _default;