no-invalid-regexp.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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({ ecmaVersion: 2018 });
  11. const validFlags = /[gimuys]/gu;
  12. const undefined1 = void 0;
  13. //------------------------------------------------------------------------------
  14. // Rule Definition
  15. //------------------------------------------------------------------------------
  16. module.exports = {
  17. meta: {
  18. type: "problem",
  19. docs: {
  20. description: "disallow invalid regular expression strings in `RegExp` constructors",
  21. category: "Possible Errors",
  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. * Check if node is a string
  52. * @param {ASTNode} node node to evaluate
  53. * @returns {boolean} True if its a string
  54. * @private
  55. */
  56. function isString(node) {
  57. return node && node.type === "Literal" && typeof node.value === "string";
  58. }
  59. /**
  60. * Check syntax error in a given pattern.
  61. * @param {string} pattern The RegExp pattern to validate.
  62. * @param {boolean} uFlag The Unicode flag.
  63. * @returns {string|null} The syntax error.
  64. */
  65. function validateRegExpPattern(pattern, uFlag) {
  66. try {
  67. validator.validatePattern(pattern, undefined1, undefined1, uFlag);
  68. return null;
  69. } catch (err) {
  70. return err.message;
  71. }
  72. }
  73. /**
  74. * Check syntax error in a given flags.
  75. * @param {string} flags The RegExp flags to validate.
  76. * @returns {string|null} The syntax error.
  77. */
  78. function validateRegExpFlags(flags) {
  79. try {
  80. validator.validateFlags(flags);
  81. return null;
  82. } catch {
  83. return `Invalid flags supplied to RegExp constructor '${flags}'`;
  84. }
  85. }
  86. return {
  87. "CallExpression, NewExpression"(node) {
  88. if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp" || !isString(node.arguments[0])) {
  89. return;
  90. }
  91. const pattern = node.arguments[0].value;
  92. let flags = isString(node.arguments[1]) ? node.arguments[1].value : "";
  93. if (allowedFlags) {
  94. flags = flags.replace(allowedFlags, "");
  95. }
  96. // If flags are unknown, check both are errored or not.
  97. const message = validateRegExpFlags(flags) || (
  98. flags
  99. ? validateRegExpPattern(pattern, flags.indexOf("u") !== -1)
  100. : validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
  101. );
  102. if (message) {
  103. context.report({
  104. node,
  105. messageId: "regexMessage",
  106. data: { message }
  107. });
  108. }
  109. }
  110. };
  111. }
  112. };