| 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-global-regexp-flag-in-query';
|
|---|
| 8 | exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
|
|---|
| 9 | name: exports.RULE_NAME,
|
|---|
| 10 | meta: {
|
|---|
| 11 | type: 'suggestion',
|
|---|
| 12 | docs: {
|
|---|
| 13 | description: 'Disallow the use of the global RegExp flag (/g) in queries',
|
|---|
| 14 | recommendedConfig: {
|
|---|
| 15 | dom: false,
|
|---|
| 16 | angular: false,
|
|---|
| 17 | react: false,
|
|---|
| 18 | vue: false,
|
|---|
| 19 | marko: false,
|
|---|
| 20 | },
|
|---|
| 21 | },
|
|---|
| 22 | messages: {
|
|---|
| 23 | noGlobalRegExpFlagInQuery: 'Avoid using the global RegExp flag (/g) in queries',
|
|---|
| 24 | },
|
|---|
| 25 | fixable: 'code',
|
|---|
| 26 | schema: [],
|
|---|
| 27 | },
|
|---|
| 28 | defaultOptions: [],
|
|---|
| 29 | create(context, _, helpers) {
|
|---|
| 30 | function reportLiteralWithRegex(literalNode) {
|
|---|
| 31 | if ((0, node_utils_1.isLiteral)(literalNode) &&
|
|---|
| 32 | 'regex' in literalNode &&
|
|---|
| 33 | literalNode.regex.flags.includes('g')) {
|
|---|
| 34 | context.report({
|
|---|
| 35 | node: literalNode,
|
|---|
| 36 | messageId: 'noGlobalRegExpFlagInQuery',
|
|---|
| 37 | fix(fixer) {
|
|---|
| 38 | const splitter = literalNode.raw.lastIndexOf('/');
|
|---|
| 39 | const raw = literalNode.raw.substring(0, splitter);
|
|---|
| 40 | const flags = literalNode.raw.substring(splitter + 1);
|
|---|
| 41 | const flagsWithoutGlobal = flags.replace('g', '');
|
|---|
| 42 | return fixer.replaceText(literalNode, `${raw}/${flagsWithoutGlobal}`);
|
|---|
| 43 | },
|
|---|
| 44 | });
|
|---|
| 45 | return true;
|
|---|
| 46 | }
|
|---|
| 47 | return false;
|
|---|
| 48 | }
|
|---|
| 49 | function getArguments(identifierNode) {
|
|---|
| 50 | if ((0, node_utils_1.isCallExpression)(identifierNode.parent)) {
|
|---|
| 51 | return identifierNode.parent.arguments;
|
|---|
| 52 | }
|
|---|
| 53 | else if ((0, node_utils_1.isMemberExpression)(identifierNode.parent) &&
|
|---|
| 54 | (0, node_utils_1.isCallExpression)(identifierNode.parent.parent)) {
|
|---|
| 55 | return identifierNode.parent.parent.arguments;
|
|---|
| 56 | }
|
|---|
| 57 | return [];
|
|---|
| 58 | }
|
|---|
| 59 | const variableNodesWithRegexs = [];
|
|---|
| 60 | function hasRegexInVariable(identifier) {
|
|---|
| 61 | return variableNodesWithRegexs.find((varNode) => {
|
|---|
| 62 | if (utils_1.ASTUtils.isVariableDeclarator(varNode) &&
|
|---|
| 63 | utils_1.ASTUtils.isIdentifier(varNode.id)) {
|
|---|
| 64 | return varNode.id.name === identifier.name;
|
|---|
| 65 | }
|
|---|
| 66 | return undefined;
|
|---|
| 67 | });
|
|---|
| 68 | }
|
|---|
| 69 | return {
|
|---|
| 70 | VariableDeclarator(node) {
|
|---|
| 71 | if (utils_1.ASTUtils.isVariableDeclarator(node) &&
|
|---|
| 72 | (0, node_utils_1.isLiteral)(node.init) &&
|
|---|
| 73 | 'regex' in node.init &&
|
|---|
| 74 | node.init.regex.flags.includes('g')) {
|
|---|
| 75 | variableNodesWithRegexs.push(node);
|
|---|
| 76 | }
|
|---|
| 77 | },
|
|---|
| 78 | CallExpression(node) {
|
|---|
| 79 | const identifierNode = (0, node_utils_1.getDeepestIdentifierNode)(node);
|
|---|
| 80 | if (!identifierNode || !helpers.isQuery(identifierNode)) {
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 | const [firstArg, secondArg] = getArguments(identifierNode);
|
|---|
| 84 | const firstArgumentHasError = reportLiteralWithRegex(firstArg);
|
|---|
| 85 | if (firstArgumentHasError) {
|
|---|
| 86 | return;
|
|---|
| 87 | }
|
|---|
| 88 | if (utils_1.ASTUtils.isIdentifier(firstArg)) {
|
|---|
| 89 | const regexVariableNode = hasRegexInVariable(firstArg);
|
|---|
| 90 | if (regexVariableNode !== undefined) {
|
|---|
| 91 | context.report({
|
|---|
| 92 | node: firstArg,
|
|---|
| 93 | messageId: 'noGlobalRegExpFlagInQuery',
|
|---|
| 94 | fix(fixer) {
|
|---|
| 95 | if (utils_1.ASTUtils.isVariableDeclarator(regexVariableNode) &&
|
|---|
| 96 | (0, node_utils_1.isLiteral)(regexVariableNode.init) &&
|
|---|
| 97 | 'regex' in regexVariableNode.init &&
|
|---|
| 98 | regexVariableNode.init.regex.flags.includes('g')) {
|
|---|
| 99 | const splitter = regexVariableNode.init.raw.lastIndexOf('/');
|
|---|
| 100 | const raw = regexVariableNode.init.raw.substring(0, splitter);
|
|---|
| 101 | const flags = regexVariableNode.init.raw.substring(splitter + 1);
|
|---|
| 102 | const flagsWithoutGlobal = flags.replace('g', '');
|
|---|
| 103 | return fixer.replaceText(regexVariableNode.init, `${raw}/${flagsWithoutGlobal}`);
|
|---|
| 104 | }
|
|---|
| 105 | return null;
|
|---|
| 106 | },
|
|---|
| 107 | });
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | if ((0, node_utils_1.isObjectExpression)(secondArg)) {
|
|---|
| 111 | const namePropertyNode = secondArg.properties.find((p) => (0, node_utils_1.isProperty)(p) &&
|
|---|
| 112 | utils_1.ASTUtils.isIdentifier(p.key) &&
|
|---|
| 113 | p.key.name === 'name' &&
|
|---|
| 114 | (0, node_utils_1.isLiteral)(p.value));
|
|---|
| 115 | if (namePropertyNode) {
|
|---|
| 116 | reportLiteralWithRegex(namePropertyNode.value);
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 | },
|
|---|
| 120 | };
|
|---|
| 121 | },
|
|---|
| 122 | });
|
|---|