no-control-regex.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @fileoverview Rule to forbid control characters from regular expressions.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. const RegExpValidator = require("regexpp").RegExpValidator;
  7. const collector = new (class {
  8. constructor() {
  9. this._source = "";
  10. this._controlChars = [];
  11. this._validator = new RegExpValidator(this);
  12. }
  13. onPatternEnter() {
  14. this._controlChars = [];
  15. }
  16. onCharacter(start, end, cp) {
  17. if (cp >= 0x00 &&
  18. cp <= 0x1F &&
  19. (
  20. this._source.codePointAt(start) === cp ||
  21. this._source.slice(start, end).startsWith("\\x") ||
  22. this._source.slice(start, end).startsWith("\\u")
  23. )
  24. ) {
  25. this._controlChars.push(`\\x${`0${cp.toString(16)}`.slice(-2)}`);
  26. }
  27. }
  28. collectControlChars(regexpStr, flags) {
  29. const uFlag = typeof flags === "string" && flags.includes("u");
  30. try {
  31. this._source = regexpStr;
  32. this._validator.validatePattern(regexpStr, void 0, void 0, uFlag); // Call onCharacter hook
  33. } catch {
  34. // Ignore syntax errors in RegExp.
  35. }
  36. return this._controlChars;
  37. }
  38. })();
  39. //------------------------------------------------------------------------------
  40. // Rule Definition
  41. //------------------------------------------------------------------------------
  42. /** @type {import('../shared/types').Rule} */
  43. module.exports = {
  44. meta: {
  45. type: "problem",
  46. docs: {
  47. description: "Disallow control characters in regular expressions",
  48. recommended: true,
  49. url: "https://eslint.org/docs/rules/no-control-regex"
  50. },
  51. schema: [],
  52. messages: {
  53. unexpected: "Unexpected control character(s) in regular expression: {{controlChars}}."
  54. }
  55. },
  56. create(context) {
  57. /**
  58. * Get the regex expression
  59. * @param {ASTNode} node `Literal` node to evaluate
  60. * @returns {{ pattern: string, flags: string | null } | null} Regex if found (the given node is either a regex literal
  61. * or a string literal that is the pattern argument of a RegExp constructor call). Otherwise `null`. If flags cannot be determined,
  62. * the `flags` property will be `null`.
  63. * @private
  64. */
  65. function getRegExp(node) {
  66. if (node.regex) {
  67. return node.regex;
  68. }
  69. if (typeof node.value === "string" &&
  70. (node.parent.type === "NewExpression" || node.parent.type === "CallExpression") &&
  71. node.parent.callee.type === "Identifier" &&
  72. node.parent.callee.name === "RegExp" &&
  73. node.parent.arguments[0] === node
  74. ) {
  75. const pattern = node.value;
  76. const flags =
  77. node.parent.arguments.length > 1 &&
  78. node.parent.arguments[1].type === "Literal" &&
  79. typeof node.parent.arguments[1].value === "string"
  80. ? node.parent.arguments[1].value
  81. : null;
  82. return { pattern, flags };
  83. }
  84. return null;
  85. }
  86. return {
  87. Literal(node) {
  88. const regExp = getRegExp(node);
  89. if (regExp) {
  90. const { pattern, flags } = regExp;
  91. const controlCharacters = collector.collectControlChars(pattern, flags);
  92. if (controlCharacters.length > 0) {
  93. context.report({
  94. node,
  95. messageId: "unexpected",
  96. data: {
  97. controlChars: controlCharacters.join(", ")
  98. }
  99. });
  100. }
  101. }
  102. }
  103. };
  104. }
  105. };