Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-invalid-regexp.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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]/g;
  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. },
  38. create(context) {
  39. const options = context.options[0];
  40. let allowedFlags = null;
  41. if (options && options.allowConstructorFlags) {
  42. const temp = options.allowConstructorFlags.join("").replace(validFlags, "");
  43. if (temp) {
  44. allowedFlags = new RegExp(`[${temp}]`, "gi");
  45. }
  46. }
  47. /**
  48. * Check if node is a string
  49. * @param {ASTNode} node node to evaluate
  50. * @returns {boolean} True if its a string
  51. * @private
  52. */
  53. function isString(node) {
  54. return node && node.type === "Literal" && typeof node.value === "string";
  55. }
  56. /**
  57. * Check syntax error in a given pattern.
  58. * @param {string} pattern The RegExp pattern to validate.
  59. * @param {boolean} uFlag The Unicode flag.
  60. * @returns {string|null} The syntax error.
  61. */
  62. function validateRegExpPattern(pattern, uFlag) {
  63. try {
  64. validator.validatePattern(pattern, undefined1, undefined1, uFlag);
  65. return null;
  66. } catch (err) {
  67. return err.message;
  68. }
  69. }
  70. /**
  71. * Check syntax error in a given flags.
  72. * @param {string} flags The RegExp flags to validate.
  73. * @returns {string|null} The syntax error.
  74. */
  75. function validateRegExpFlags(flags) {
  76. try {
  77. validator.validateFlags(flags);
  78. return null;
  79. } catch (err) {
  80. return `Invalid flags supplied to RegExp constructor '${flags}'`;
  81. }
  82. }
  83. return {
  84. "CallExpression, NewExpression"(node) {
  85. if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp" || !isString(node.arguments[0])) {
  86. return;
  87. }
  88. const pattern = node.arguments[0].value;
  89. let flags = isString(node.arguments[1]) ? node.arguments[1].value : "";
  90. if (allowedFlags) {
  91. flags = flags.replace(allowedFlags, "");
  92. }
  93. // If flags are unknown, check both are errored or not.
  94. const message = validateRegExpFlags(flags) || (
  95. flags
  96. ? validateRegExpPattern(pattern, flags.indexOf("u") !== -1)
  97. : validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
  98. );
  99. if (message) {
  100. context.report({
  101. node,
  102. message: "{{message}}.",
  103. data: { message }
  104. });
  105. }
  106. }
  107. };
  108. }
  109. };