Skip to content

Commit

Permalink
New: Add prefer-regex-literals rule (fixes #12238) (#12254)
Browse files Browse the repository at this point in the history
* New: Add prefer-regex-literals rule (fixes #12238)

* Update docs/rules/prefer-regex-literals.md

Co-Authored-By: Kevin Partington <platinum.azure@kernelpanicstudios.com>

* Update docs/rules/prefer-regex-literals.md

Co-Authored-By: Kevin Partington <platinum.azure@kernelpanicstudios.com>

* Pass ecmaVersion to RuleTester constructor

* Check global String reference
  • Loading branch information
mdjermanovic authored and ilyavolodin committed Sep 14, 2019
1 parent 37c0fde commit 3be04fd
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 0 deletions.
90 changes: 90 additions & 0 deletions docs/rules/prefer-regex-literals.md
@@ -0,0 +1,90 @@
# Disallow use of the `RegExp` constructor in favor of regular expression literals (prefer-regex-literals)

There are two ways to create a regular expression:

* Regular expression literals, e.g., `/abc/u`.
* The `RegExp` constructor function, e.g., `new RegExp("abc", "u")` or `RegExp("abc", "u")`.

The constructor function is particularly useful when you want to dynamically generate the pattern,
because it takes string arguments.

When using the constructor function with string literals, don't forget that the string escaping rules still apply.
If you want to put a backslash in the pattern, you need to escape it in the string literal.
Thus, the following are equivalent:

```js
new RegExp("^\\d\\.$");

/^\d\.$/;

// matches "0.", "1.", "2." ... "9."
```

In the above example, the regular expression literal is easier to read and reason about.
Also, it's a common mistake to omit the extra `\` in the string literal, which would produce a completely different regular expression:

```js
new RegExp("^\d\.$");

// equivalent to /^d.$/, matches "d1", "d2", "da", "db" ...
```

When a regular expression is known in advance, it is considered a best practice to avoid the string literal notation on top
of the regular expression notation, and use regular expression literals instead of the constructor function.

## Rule Details

This rule disallows the use of the `RegExp` constructor function with string literals as its arguments.

This rule also disallows the use of the `RegExp` constructor function with template literals without expressions
and `String.raw` tagged template literals without expressions.

The rule does not disallow all use of the `RegExp` constructor. It should be still used for
dynamically generated regular expressions.

Examples of **incorrect** code for this rule:

```js
new RegExp("abc");

new RegExp("abc", "u");

RegExp("abc");

RegExp("abc", "u");

new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");

RegExp(`^\\d\\.$`);

new RegExp(String.raw`^\d\.$`);
```

Examples of **correct** code for this rule:

```js
/abc/;

/abc/u;

/\d\d\.\d\d\.\d\d\d\d/;

/^\d\.$/;

// RegExp constructor is allowed for dynamically generated regular expressions

new RegExp(pattern);

RegExp("abc", flags);

new RegExp(prefix + "abc");

RegExp(`${prefix}abc`);

new RegExp(String.raw`^\d\. ${sufix}`);
```

## Further Reading

* [MDN: Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
* [MDN: RegExp Constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -242,6 +242,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
"prefer-object-spread": () => require("./prefer-object-spread"),
"prefer-promise-reject-errors": () => require("./prefer-promise-reject-errors"),
"prefer-reflect": () => require("./prefer-reflect"),
"prefer-regex-literals": () => require("./prefer-regex-literals"),
"prefer-rest-params": () => require("./prefer-rest-params"),
"prefer-spread": () => require("./prefer-spread"),
"prefer-template": () => require("./prefer-template"),
Expand Down
125 changes: 125 additions & 0 deletions lib/rules/prefer-regex-literals.js
@@ -0,0 +1,125 @@
/**
* @fileoverview Rule to disallow use of the `RegExp` constructor in favor of regular expression literals
* @author Milos Djermanovic
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");
const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("eslint-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Determines whether the given node is a string literal.
* @param {ASTNode} node Node to check.
* @returns {boolean} True if the node is a string literal.
*/
function isStringLiteral(node) {
return node.type === "Literal" && typeof node.value === "string";
}

/**
* Determines whether the given node is a template literal without expressions.
* @param {ASTNode} node Node to check.
* @returns {boolean} True if the node is a template literal without expressions.
*/
function isStaticTemplateLiteral(node) {
return node.type === "TemplateLiteral" && node.expressions.length === 0;
}


//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "disallow use of the `RegExp` constructor in favor of regular expression literals",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/prefer-regex-literals"
},

schema: [],

messages: {
unexpectedRegExp: "Use a regular expression literal instead of the 'RegExp' constructor."
}
},

create(context) {

/**
* Determines whether the given identifier node is a reference to a global variable.
* @param {ASTNode} node `Identifier` node to check.
* @returns {boolean} True if the identifier is a reference to a global variable.
*/
function isGlobalReference(node) {
const scope = context.getScope();
const variable = findVariable(scope, node);

return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
}

/**
* Determines whether the given node is a String.raw`` tagged template expression
* with a static template literal.
* @param {ASTNode} node Node to check.
* @returns {boolean} True if the node is String.raw`` with a static template.
*/
function isStringRawTaggedStaticTemplateLiteral(node) {
return node.type === "TaggedTemplateExpression" &&
node.tag.type === "MemberExpression" &&
node.tag.object.type === "Identifier" &&
node.tag.object.name === "String" &&
isGlobalReference(node.tag.object) &&
astUtils.getStaticPropertyName(node.tag) === "raw" &&
isStaticTemplateLiteral(node.quasi);
}

/**
* Determines whether the given node is considered to be a static string by the logic of this rule.
* @param {ASTNode} node Node to check.
* @returns {boolean} True if the node is a static string.
*/
function isStaticString(node) {
return isStringLiteral(node) ||
isStaticTemplateLiteral(node) ||
isStringRawTaggedStaticTemplateLiteral(node);
}

return {
Program() {
const scope = context.getScope();
const tracker = new ReferenceTracker(scope);
const traceMap = {
RegExp: {
[CALL]: true,
[CONSTRUCT]: true
}
};

for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
const args = node.arguments;

if (
(args.length === 1 || args.length === 2) &&
args.every(isStaticString)
) {
context.report({ node, messageId: "unexpectedRegExp" });
}
}
}
};
}
};

0 comments on commit 3be04fd

Please sign in to comment.