commit 08d6d4d9fefb3a58441edec16d659b14ad7555c3 Author: Vitaliy Filippov Date: Tue Jul 5 18:39:16 2016 +0300 Pack no-regex-dot into a module diff --git a/index.js b/index.js new file mode 100644 index 0000000..014f466 --- /dev/null +++ b/index.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = { + rules: { + 'no-regex-dot': require('./no-regex-dot') + }, + configs: { + } +}; diff --git a/no-regex-dot.js b/no-regex-dot.js new file mode 100644 index 0000000..e95597b --- /dev/null +++ b/no-regex-dot.js @@ -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"); + } + } + } + }; + + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8ecd430 --- /dev/null +++ b/package.json @@ -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" +}