| 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.RULE_NAME = void 0;
|
|---|
| 4 | const utils_1 = require("@typescript-eslint/utils");
|
|---|
| 5 | const create_testing_library_rule_1 = require("../create-testing-library-rule");
|
|---|
| 6 | const node_utils_1 = require("../node-utils");
|
|---|
| 7 | exports.RULE_NAME = 'no-promise-in-fire-event';
|
|---|
| 8 | exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
|
|---|
| 9 | name: exports.RULE_NAME,
|
|---|
| 10 | meta: {
|
|---|
| 11 | type: 'problem',
|
|---|
| 12 | docs: {
|
|---|
| 13 | description: 'Disallow the use of promises passed to a `fireEvent` method',
|
|---|
| 14 | recommendedConfig: {
|
|---|
| 15 | dom: 'error',
|
|---|
| 16 | angular: 'error',
|
|---|
| 17 | react: 'error',
|
|---|
| 18 | vue: 'error',
|
|---|
| 19 | marko: 'error',
|
|---|
| 20 | },
|
|---|
| 21 | },
|
|---|
| 22 | messages: {
|
|---|
| 23 | noPromiseInFireEvent: "A promise shouldn't be passed to a `fireEvent` method, instead pass the DOM element",
|
|---|
| 24 | },
|
|---|
| 25 | schema: [],
|
|---|
| 26 | },
|
|---|
| 27 | defaultOptions: [],
|
|---|
| 28 | create(context, _, helpers) {
|
|---|
| 29 | function checkSuspiciousNode(node, originalNode) {
|
|---|
| 30 | if (utils_1.ASTUtils.isAwaitExpression(node)) {
|
|---|
| 31 | return;
|
|---|
| 32 | }
|
|---|
| 33 | if ((0, node_utils_1.isNewExpression)(node)) {
|
|---|
| 34 | if ((0, node_utils_1.isPromiseIdentifier)(node.callee)) {
|
|---|
| 35 | context.report({
|
|---|
| 36 | node: originalNode !== null && originalNode !== void 0 ? originalNode : node,
|
|---|
| 37 | messageId: 'noPromiseInFireEvent',
|
|---|
| 38 | });
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | if ((0, node_utils_1.isCallExpression)(node)) {
|
|---|
| 43 | const domElementIdentifier = (0, node_utils_1.getDeepestIdentifierNode)(node);
|
|---|
| 44 | if (!domElementIdentifier) {
|
|---|
| 45 | return;
|
|---|
| 46 | }
|
|---|
| 47 | if (helpers.isAsyncQuery(domElementIdentifier) ||
|
|---|
| 48 | (0, node_utils_1.isPromiseIdentifier)(domElementIdentifier)) {
|
|---|
| 49 | context.report({
|
|---|
| 50 | node: originalNode !== null && originalNode !== void 0 ? originalNode : node,
|
|---|
| 51 | messageId: 'noPromiseInFireEvent',
|
|---|
| 52 | });
|
|---|
| 53 | return;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | if (utils_1.ASTUtils.isIdentifier(node)) {
|
|---|
| 57 | const nodeVariable = utils_1.ASTUtils.findVariable(context.getScope(), node.name);
|
|---|
| 58 | if (!nodeVariable) {
|
|---|
| 59 | return;
|
|---|
| 60 | }
|
|---|
| 61 | for (const definition of nodeVariable.defs) {
|
|---|
| 62 | const variableDeclarator = definition.node;
|
|---|
| 63 | if (variableDeclarator.init) {
|
|---|
| 64 | checkSuspiciousNode(variableDeclarator.init, node);
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | return {
|
|---|
| 70 | 'CallExpression Identifier'(node) {
|
|---|
| 71 | if (!helpers.isFireEventMethod(node)) {
|
|---|
| 72 | return;
|
|---|
| 73 | }
|
|---|
| 74 | const closestCallExpression = (0, node_utils_1.findClosestCallExpressionNode)(node, true);
|
|---|
| 75 | if (!closestCallExpression) {
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 | const domElementArgument = closestCallExpression.arguments[0];
|
|---|
| 79 | checkSuspiciousNode(domElementArgument);
|
|---|
| 80 | },
|
|---|
| 81 | };
|
|---|
| 82 | },
|
|---|
| 83 | });
|
|---|