Skip to content

Commit

Permalink
New: no-invalid-regexp allows custom flags (fixes eslint#5249)
Browse files Browse the repository at this point in the history
  • Loading branch information
Afnan Fahim committed Feb 21, 2016
1 parent 40fec58 commit bf16380
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/rules/no-invalid-regexp.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ this.RegExp('[')

ECMAScript 6 adds the "u" ([unicode](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.unicode)) and "y" ([sticky](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.sticky)) flags. You can enable these to be recognized as valid by setting the ECMAScript version to 6 in your [ESLint configuration](../user-guide/configuring).


## Options

If you want to allow additional constructor flags for any reason, you can specify them using an `allowConstructorFlags` option in `.eslintrc`. These flags will then be ignored by the rule regardless of the `ecmaVersion` setting.

### `allowConstructorFlags`

This takes in an array of flags. With this option, the following patterns aren't considered problems:

```js
/*eslint no-invalid-regexp: [2, {"allowConstructorFlags": ["u", "y"]}]*/

new RegExp('.', 'y')

new RegExp('.', 'yu')
```


## Further Reading

* [Annotated ES5 §7.8.5 - Regular Expression Literals](http://es5.github.io/#x7.8.5)
24 changes: 23 additions & 1 deletion lib/rules/no-invalid-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ var espree = require("espree");

module.exports = function(context) {

var options = context.options[0],
allowedFlags = "";

if (options && options.allowConstructorFlags) {
allowedFlags = options.allowConstructorFlags.join("");
}

/**
* Check if node is a string
* @param {ASTNode} node node to evaluate
Expand All @@ -37,6 +44,10 @@ module.exports = function(context) {
if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(node.arguments[0])) {
var flags = isString(node.arguments[1]) ? node.arguments[1].value : "";

if (allowedFlags) {
flags = flags.replace(new RegExp("[" + allowedFlags + "]", "gi"), "");
}

try {
void new RegExp(node.arguments[0].value);
} catch (e) {
Expand All @@ -62,4 +73,15 @@ module.exports = function(context) {

};

module.exports.schema = [];
module.exports.schema = [{
"type": "object",
"properties": {
"allowConstructorFlags": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
}];
4 changes: 4 additions & 0 deletions tests/lib/rules/no-invalid-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ruleTester.run("no-invalid-regexp", rule, {
"new RegExp",
"new RegExp('.', 'im')",
"global.RegExp('\\\\')",
{ code: "new RegExp('.', 'y')", options: [{ allowConstructorFlags: ["y"] }]},
{ code: "new RegExp('.', 'u')", options: [{ allowConstructorFlags: ["U"] }]},
{ code: "new RegExp('.', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }]},
{ code: "new RegExp('\/', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }]},
{ code: "new RegExp('.', 'y')", parserOptions: { ecmaVersion: 6 }},
{ code: "new RegExp('.', 'u')", parserOptions: { ecmaVersion: 6 }},
{ code: "new RegExp('.', 'yu')", parserOptions: { ecmaVersion: 6 }},
Expand Down

0 comments on commit bf16380

Please sign in to comment.