Blame view
node_modules/eslint/lib/rules/no-control-regex.js
1.6 KB
c39994410
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/** * @fileoverview Rule to forbid control charactes from regular expressions. * @author Nicholas C. Zakas */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = 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; } } } else { return null; } } return { "Literal": function(node) { var computedValue, regex = getRegExp(node); if (regex) { computedValue = regex.toString(); if (/[\x00-\x1f]/.test(computedValue)) { context.report(node, "Unexpected control character in regular expression."); } } } }; }; module.exports.schema = []; |