Pack no-regex-dot into a module

master
Vitaliy Filippov 2016-07-05 18:39:16 +03:00
commit 08d6d4d9fe
3 changed files with 121 additions and 0 deletions

9
index.js Normal file
View File

@ -0,0 +1,9 @@
'use strict';
module.exports = {
rules: {
'no-regex-dot': require('./no-regex-dot')
},
configs: {
}
};

80
no-regex-dot.js Normal file
View File

@ -0,0 +1,80 @@
/**
* @fileoverview Rule to forbid . in regular expressions (it doesn't match newlines, [\s\S] should be used instead)
* @author Vitaliy Filippov
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow . in regular expressions",
category: "Possible Errors",
recommended: true
},
schema: []
},
create: function(context) {
/**
* Get the regex expression
* @param {ASTNode} node node to evaluate
* @returns {*} Regex if found else null
* @private
*/
function getRegExp(node) {
if (node.value instanceof RegExp) {
return node.value;
} else if (typeof node.value === "string") {
var parent = context.getAncestors().pop();
if ((parent.type === "NewExpression" || parent.type === "CallExpression") &&
parent.callee.type === "Identifier" && parent.callee.name === "RegExp"
) {
// there could be an invalid regular expression string
try {
return new RegExp(node.value);
} catch (ex) {
return null;
}
}
}
return null;
}
/**
* Check if given regex string has . in it
* @param {String} regexStr regex as string to check
* @returns {Boolean} returns true if finds control characters on given string
* @private
*/
function hasDot(regexStr) {
return /(^|[^\\])(\\\\)*\./.test(regexStr);
}
return {
Literal: function(node) {
var computedValue,
regex = getRegExp(node);
if (regex) {
computedValue = regex.toString();
if (hasDot(computedValue)) {
context.report(node, "Unexpected . in regular expression, use [\\s\\S] instead");
}
}
}
};
}
};

32
package.json Normal file
View File

@ -0,0 +1,32 @@
{
"name": "eslint-plugin-no-regex-dot",
"version": "1.0.0",
"author": {
"name": "Vitaliy Filippov",
"email": "vitalif@yourcmc.ru"
},
"description": "Disallow . in regular expressions in favor of [\\s\\S]",
"main": "index.js",
"files": [
"index.js",
"no-regex-dot.js"
],
"repository": {
"type": "git",
"url": "git+https://github.com/vitalif/eslint-plugin-no-regex-dot.git"
},
"homepage": "https://github.com/vitalif/eslint-plugin-no-regex-dot",
"bugs": {
"url": "https://github.com/vitalif/eslint-plugin-no-regex-dot/issues"
},
"dependencies": {
},
"devDependencies": {
},
"keywords": [
"eslint",
"eslint-plugin",
"eslintplugin"
],
"license": "MIT"
}