2 * @fileoverview Rule to disallow use of the `RegExp` constructor in favor of regular expression literals
3 * @author Milos Djermanovic
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
13 const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("eslint-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
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.
24 function isStringLiteral(node) {
25 return node.type === "Literal" && typeof node.value === "string";
29 * Determines whether the given node is a regex literal.
30 * @param {ASTNode} node Node to check.
31 * @returns {boolean} True if the node is a regex literal.
33 function isRegexLiteral(node) {
34 return node.type === "Literal" && Object.prototype.hasOwnProperty.call(node, "regex");
38 * Determines whether the given node is a template literal without expressions.
39 * @param {ASTNode} node Node to check.
40 * @returns {boolean} True if the node is a template literal without expressions.
42 function isStaticTemplateLiteral(node) {
43 return node.type === "TemplateLiteral" && node.expressions.length === 0;
47 //------------------------------------------------------------------------------
49 //------------------------------------------------------------------------------
56 description: "disallow use of the `RegExp` constructor in favor of regular expression literals",
57 category: "Best Practices",
59 url: "https://eslint.org/docs/rules/prefer-regex-literals"
66 disallowRedundantWrapping: {
71 additionalProperties: false
76 unexpectedRegExp: "Use a regular expression literal instead of the 'RegExp' constructor.",
77 unexpectedRedundantRegExp: "Regular expression literal is unnecessarily wrapped within a 'RegExp' constructor.",
78 unexpectedRedundantRegExpWithFlags: "Use regular expression literal with flags instead of the 'RegExp' constructor."
83 const [{ disallowRedundantWrapping = false } = {}] = context.options;
86 * Determines whether the given identifier node is a reference to a global variable.
87 * @param {ASTNode} node `Identifier` node to check.
88 * @returns {boolean} True if the identifier is a reference to a global variable.
90 function isGlobalReference(node) {
91 const scope = context.getScope();
92 const variable = findVariable(scope, node);
94 return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
98 * Determines whether the given node is a String.raw`` tagged template expression
99 * with a static template literal.
100 * @param {ASTNode} node Node to check.
101 * @returns {boolean} True if the node is String.raw`` with a static template.
103 function isStringRawTaggedStaticTemplateLiteral(node) {
104 return node.type === "TaggedTemplateExpression" &&
105 astUtils.isSpecificMemberAccess(node.tag, "String", "raw") &&
106 isGlobalReference(astUtils.skipChainExpression(node.tag).object) &&
107 isStaticTemplateLiteral(node.quasi);
111 * Determines whether the given node is considered to be a static string by the logic of this rule.
112 * @param {ASTNode} node Node to check.
113 * @returns {boolean} True if the node is a static string.
115 function isStaticString(node) {
116 return isStringLiteral(node) ||
117 isStaticTemplateLiteral(node) ||
118 isStringRawTaggedStaticTemplateLiteral(node);
122 * Determines whether the relevant arguments of the given are all static string literals.
123 * @param {ASTNode} node Node to check.
124 * @returns {boolean} True if all arguments are static strings.
126 function hasOnlyStaticStringArguments(node) {
127 const args = node.arguments;
129 if ((args.length === 1 || args.length === 2) && args.every(isStaticString)) {
137 * Determines whether the arguments of the given node indicate that a regex literal is unnecessarily wrapped.
138 * @param {ASTNode} node Node to check.
139 * @returns {boolean} True if the node already contains a regex literal argument.
141 function isUnnecessarilyWrappedRegexLiteral(node) {
142 const args = node.arguments;
144 if (args.length === 1 && isRegexLiteral(args[0])) {
148 if (args.length === 2 && isRegexLiteral(args[0]) && isStaticString(args[1])) {
157 const scope = context.getScope();
158 const tracker = new ReferenceTracker(scope);
166 for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
167 if (disallowRedundantWrapping && isUnnecessarilyWrappedRegexLiteral(node)) {
168 if (node.arguments.length === 2) {
169 context.report({ node, messageId: "unexpectedRedundantRegExpWithFlags" });
171 context.report({ node, messageId: "unexpectedRedundantRegExp" });
173 } else if (hasOnlyStaticStringArguments(node)) {
174 context.report({ node, messageId: "unexpectedRegExp" });