web 3d图形渲染器
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.

144 lines
4.2 KiB

  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 getBlockType = statement => {
  9. const func = statement.parent;
  10. /* istanbul ignore if */
  11. if (!func) {
  12. throw new Error(`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
  13. } // functionDeclaration: function func() {}
  14. if (func.type === _experimentalUtils.AST_NODE_TYPES.FunctionDeclaration) {
  15. return 'function';
  16. }
  17. if ((0, _utils.isFunction)(func) && func.parent) {
  18. const expr = func.parent; // arrow function or function expr
  19. if (expr.type === _experimentalUtils.AST_NODE_TYPES.VariableDeclarator) {
  20. return 'function';
  21. } // if it's not a variable, it will be callExpr, we only care about describe
  22. if (expr.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isDescribeCall)(expr)) {
  23. return 'describe';
  24. }
  25. }
  26. return null;
  27. };
  28. const isEach = node => node.callee.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && node.callee.callee.property.type === _experimentalUtils.AST_NODE_TYPES.Identifier && node.callee.callee.property.name === 'each' && node.callee.callee.object.type === _experimentalUtils.AST_NODE_TYPES.Identifier && _utils.TestCaseName.hasOwnProperty(node.callee.callee.object.name);
  29. var _default = (0, _utils.createRule)({
  30. name: __filename,
  31. meta: {
  32. docs: {
  33. category: 'Best Practices',
  34. description: 'Disallow using `expect` outside of `it` or `test` blocks',
  35. recommended: 'error'
  36. },
  37. messages: {
  38. unexpectedExpect: 'Expect must be inside of a test block.'
  39. },
  40. type: 'suggestion',
  41. schema: [{
  42. properties: {
  43. additionalTestBlockFunctions: {
  44. type: 'array',
  45. items: {
  46. type: 'string'
  47. }
  48. }
  49. },
  50. additionalProperties: false
  51. }]
  52. },
  53. defaultOptions: [{
  54. additionalTestBlockFunctions: []
  55. }],
  56. create(context, [{
  57. additionalTestBlockFunctions = []
  58. }]) {
  59. const callStack = [];
  60. const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils.getNodeName)(node) || '');
  61. const isTestBlock = node => (0, _utils.isTestCaseCall)(node) || isCustomTestBlockFunction(node);
  62. return {
  63. CallExpression(node) {
  64. if ((0, _utils.isExpectCall)(node)) {
  65. const parent = callStack[callStack.length - 1];
  66. if (!parent || parent === _utils.DescribeAlias.describe) {
  67. context.report({
  68. node,
  69. messageId: 'unexpectedExpect'
  70. });
  71. }
  72. return;
  73. }
  74. if (isTestBlock(node)) {
  75. callStack.push('test');
  76. }
  77. if (node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
  78. callStack.push('template');
  79. }
  80. },
  81. 'CallExpression:exit'(node) {
  82. const top = callStack[callStack.length - 1];
  83. if (top === 'test' && (isEach(node) || isTestBlock(node) && node.callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression) || top === 'template' && node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
  84. callStack.pop();
  85. }
  86. },
  87. BlockStatement(statement) {
  88. const blockType = getBlockType(statement);
  89. if (blockType) {
  90. callStack.push(blockType);
  91. }
  92. },
  93. 'BlockStatement:exit'(statement) {
  94. if (callStack[callStack.length - 1] === getBlockType(statement)) {
  95. callStack.pop();
  96. }
  97. },
  98. ArrowFunctionExpression(node) {
  99. var _node$parent;
  100. if (((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
  101. callStack.push('arrow');
  102. }
  103. },
  104. 'ArrowFunctionExpression:exit'() {
  105. if (callStack[callStack.length - 1] === 'arrow') {
  106. callStack.pop();
  107. }
  108. }
  109. };
  110. }
  111. });
  112. exports.default = _default;