1/** 2 * @fileoverview Rule to disallow use of the `RegExp` constructor in favor of regular expression literals 3 * @author Milos Djermanovic 4 */ 5 6"use strict"; 7 8//------------------------------------------------------------------------------ 9// Requirements 10//------------------------------------------------------------------------------ 11 12const astUtils = require("./utils/ast-utils"); 13const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("eslint-utils"); 14 15//------------------------------------------------------------------------------ 16// Helpers 17//------------------------------------------------------------------------------ 18 19/** 20 * Determines whether the given node is a string literal. 21 * @param {ASTNode} node Node to check. 22 * @returns {boolean} True if the node is a string literal. 23 */ 24function isStringLiteral(node) { 25 return node.type === "Literal" && typeof node.value === "string"; 26} 27 28/** 29 * Determines whether the given node is a template literal without expressions. 30 * @param {ASTNode} node Node to check. 31 * @returns {boolean} True if the node is a template literal without expressions. 32 */ 33function isStaticTemplateLiteral(node) { 34 return node.type === "TemplateLiteral" && node.expressions.length === 0; 35} 36 37 38//------------------------------------------------------------------------------ 39// Rule Definition 40//------------------------------------------------------------------------------ 41 42module.exports = { 43 meta: { 44 type: "suggestion", 45 46 docs: { 47 description: "disallow use of the `RegExp` constructor in favor of regular expression literals", 48 category: "Best Practices", 49 recommended: false, 50 url: "https://eslint.org/docs/rules/prefer-regex-literals" 51 }, 52 53 schema: [], 54 55 messages: { 56 unexpectedRegExp: "Use a regular expression literal instead of the 'RegExp' constructor." 57 } 58 }, 59 60 create(context) { 61 62 /** 63 * Determines whether the given identifier node is a reference to a global variable. 64 * @param {ASTNode} node `Identifier` node to check. 65 * @returns {boolean} True if the identifier is a reference to a global variable. 66 */ 67 function isGlobalReference(node) { 68 const scope = context.getScope(); 69 const variable = findVariable(scope, node); 70 71 return variable !== null && variable.scope.type === "global" && variable.defs.length === 0; 72 } 73 74 /** 75 * Determines whether the given node is a String.raw`` tagged template expression 76 * with a static template literal. 77 * @param {ASTNode} node Node to check. 78 * @returns {boolean} True if the node is String.raw`` with a static template. 79 */ 80 function isStringRawTaggedStaticTemplateLiteral(node) { 81 return node.type === "TaggedTemplateExpression" && 82 astUtils.isSpecificMemberAccess(node.tag, "String", "raw") && 83 isGlobalReference(astUtils.skipChainExpression(node.tag).object) && 84 isStaticTemplateLiteral(node.quasi); 85 } 86 87 /** 88 * Determines whether the given node is considered to be a static string by the logic of this rule. 89 * @param {ASTNode} node Node to check. 90 * @returns {boolean} True if the node is a static string. 91 */ 92 function isStaticString(node) { 93 return isStringLiteral(node) || 94 isStaticTemplateLiteral(node) || 95 isStringRawTaggedStaticTemplateLiteral(node); 96 } 97 98 return { 99 Program() { 100 const scope = context.getScope(); 101 const tracker = new ReferenceTracker(scope); 102 const traceMap = { 103 RegExp: { 104 [CALL]: true, 105 [CONSTRUCT]: true 106 } 107 }; 108 109 for (const { node } of tracker.iterateGlobalReferences(traceMap)) { 110 const args = node.arguments; 111 112 if ( 113 (args.length === 1 || args.length === 2) && 114 args.every(isStaticString) 115 ) { 116 context.report({ node, messageId: "unexpectedRegExp" }); 117 } 118 } 119 } 120 }; 121 } 122}; 123