Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = /[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. * Gets flags of a regular expression created by the given `RegExp()` or `new RegExp()` call
  61. * Examples:
  62. * new RegExp(".") // => ""
  63. * new RegExp(".", "gu") // => "gu"
  64. * new RegExp(".", flags) // => null
  65. * @param {ASTNode} node `CallExpression` or `NewExpression` node
  66. * @returns {string|null} flags if they can be determined, `null` otherwise
  67. * @private
  68. */
  69. function getFlags(node) {
  70. if (node.arguments.length < 2) {
  71. return "";
  72. }
  73. if (isString(node.arguments[1])) {
  74. return node.arguments[1].value;
  75. }
  76. return null;
  77. }
  78. /**
  79. * Check syntax error in a given pattern.
  80. * @param {string} pattern The RegExp pattern to validate.
  81. * @param {boolean} uFlag The Unicode flag.
  82. * @returns {string|null} The syntax error.
  83. */
  84. function validateRegExpPattern(pattern, uFlag) {
  85. try {
  86. validator.validatePattern(pattern, undefined1, undefined1, uFlag);
  87. return null;
  88. } catch (err) {
  89. return err.message;
  90. }
  91. }
  92. /**
  93. * Check syntax error in a given flags.
  94. * @param {string} flags The RegExp flags to validate.
  95. * @returns {string|null} The syntax error.
  96. */
  97. function validateRegExpFlags(flags) {
  98. try {
  99. validator.validateFlags(flags);
  100. return null;
  101. } catch {
  102. return `Invalid flags supplied to RegExp constructor '${flags}'`;
  103. }
  104. }
  105. return {
  106. "CallExpression, NewExpression"(node) {
  107. if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp" || !isString(node.arguments[0])) {
  108. return;
  109. }
  110. const pattern = node.arguments[0].value;
  111. let flags = getFlags(node);
  112. if (flags && allowedFlags) {
  113. flags = flags.replace(allowedFlags, "");
  114. }
  115. const message =
  116. (
  117. flags && validateRegExpFlags(flags)
  118. ) ||
  119. (
  120. // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
  121. flags === null
  122. ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
  123. : validateRegExpPattern(pattern, flags.includes("u"))
  124. );
  125. if (message) {
  126. context.report({
  127. node,
  128. messageId: "regexMessage",
  129. data: { message }
  130. });
  131. }
  132. }
  133. };
  134. }
  135. };