no-invalid-regexp.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @fileoverview Validate strings passed to the RegExp constructor
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const RegExpValidator = require("regexpp").RegExpValidator;
  10. const validator = new RegExpValidator();
  11. const validFlags = /[dgimsuy]/gu;
  12. const undefined1 = void 0;
  13. //------------------------------------------------------------------------------
  14. // Rule Definition
  15. //------------------------------------------------------------------------------
  16. /** @type {import('../shared/types').Rule} */
  17. module.exports = {
  18. meta: {
  19. type: "problem",
  20. docs: {
  21. description: "Disallow invalid regular expression strings in `RegExp` constructors",
  22. recommended: true,
  23. url: "https://eslint.org/docs/rules/no-invalid-regexp"
  24. },
  25. schema: [{
  26. type: "object",
  27. properties: {
  28. allowConstructorFlags: {
  29. type: "array",
  30. items: {
  31. type: "string"
  32. }
  33. }
  34. },
  35. additionalProperties: false
  36. }],
  37. messages: {
  38. regexMessage: "{{message}}."
  39. }
  40. },
  41. create(context) {
  42. const options = context.options[0];
  43. let allowedFlags = null;
  44. if (options && options.allowConstructorFlags) {
  45. const temp = options.allowConstructorFlags.join("").replace(validFlags, "");
  46. if (temp) {
  47. allowedFlags = new RegExp(`[${temp}]`, "giu");
  48. }
  49. }
  50. /**
  51. * Reports error with the provided message.
  52. * @param {ASTNode} node The node holding the invalid RegExp
  53. * @param {string} message The message to report.
  54. * @returns {void}
  55. */
  56. function report(node, message) {
  57. context.report({
  58. node,
  59. messageId: "regexMessage",
  60. data: { message }
  61. });
  62. }
  63. /**
  64. * Check if node is a string
  65. * @param {ASTNode} node node to evaluate
  66. * @returns {boolean} True if its a string
  67. * @private
  68. */
  69. function isString(node) {
  70. return node && node.type === "Literal" && typeof node.value === "string";
  71. }
  72. /**
  73. * Gets flags of a regular expression created by the given `RegExp()` or `new RegExp()` call
  74. * Examples:
  75. * new RegExp(".") // => ""
  76. * new RegExp(".", "gu") // => "gu"
  77. * new RegExp(".", flags) // => null
  78. * @param {ASTNode} node `CallExpression` or `NewExpression` node
  79. * @returns {string|null} flags if they can be determined, `null` otherwise
  80. * @private
  81. */
  82. function getFlags(node) {
  83. if (node.arguments.length < 2) {
  84. return "";
  85. }
  86. if (isString(node.arguments[1])) {
  87. return node.arguments[1].value;
  88. }
  89. return null;
  90. }
  91. /**
  92. * Check syntax error in a given pattern.
  93. * @param {string} pattern The RegExp pattern to validate.
  94. * @param {boolean} uFlag The Unicode flag.
  95. * @returns {string|null} The syntax error.
  96. */
  97. function validateRegExpPattern(pattern, uFlag) {
  98. try {
  99. validator.validatePattern(pattern, undefined1, undefined1, uFlag);
  100. return null;
  101. } catch (err) {
  102. return err.message;
  103. }
  104. }
  105. /**
  106. * Check syntax error in a given flags.
  107. * @param {string|null} flags The RegExp flags to validate.
  108. * @returns {string|null} The syntax error.
  109. */
  110. function validateRegExpFlags(flags) {
  111. if (!flags) {
  112. return null;
  113. }
  114. try {
  115. validator.validateFlags(flags);
  116. return null;
  117. } catch {
  118. return `Invalid flags supplied to RegExp constructor '${flags}'`;
  119. }
  120. }
  121. return {
  122. "CallExpression, NewExpression"(node) {
  123. if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
  124. return;
  125. }
  126. let flags = getFlags(node);
  127. if (flags && allowedFlags) {
  128. flags = flags.replace(allowedFlags, "");
  129. }
  130. let message = validateRegExpFlags(flags);
  131. if (message) {
  132. report(node, message);
  133. return;
  134. }
  135. if (!isString(node.arguments[0])) {
  136. return;
  137. }
  138. const pattern = node.arguments[0].value;
  139. message = (
  140. // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
  141. flags === null
  142. ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
  143. : validateRegExpPattern(pattern, flags.includes("u"))
  144. );
  145. if (message) {
  146. report(node, message);
  147. }
  148. }
  149. };
  150. }
  151. };