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-empty-character-class.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @fileoverview Rule to flag the use of empty character classes in regular expressions
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /*
  10. * plain-English description of the following regexp:
  11. * 0. `^` fix the match at the beginning of the string
  12. * 1. `\/`: the `/` that begins the regexp
  13. * 2. `([^\\[]|\\.|\[([^\\\]]|\\.)+\])*`: regexp contents; 0 or more of the following
  14. * 2.0. `[^\\[]`: any character that's not a `\` or a `[` (anything but escape sequences and character classes)
  15. * 2.1. `\\.`: an escape sequence
  16. * 2.2. `\[([^\\\]]|\\.)+\]`: a character class that isn't empty
  17. * 3. `\/` the `/` that ends the regexp
  18. * 4. `[gimuy]*`: optional regexp flags
  19. * 5. `$`: fix the match at the end of the string
  20. */
  21. const regex = /^\/([^\\[]|\\.|\[([^\\\]]|\\.)+])*\/[gimuys]*$/;
  22. //------------------------------------------------------------------------------
  23. // Rule Definition
  24. //------------------------------------------------------------------------------
  25. module.exports = {
  26. meta: {
  27. type: "problem",
  28. docs: {
  29. description: "disallow empty character classes in regular expressions",
  30. category: "Possible Errors",
  31. recommended: true,
  32. url: "https://eslint.org/docs/rules/no-empty-character-class"
  33. },
  34. schema: [],
  35. messages: {
  36. unexpected: "Empty class."
  37. }
  38. },
  39. create(context) {
  40. const sourceCode = context.getSourceCode();
  41. return {
  42. Literal(node) {
  43. const token = sourceCode.getFirstToken(node);
  44. if (token.type === "RegularExpression" && !regex.test(token.value)) {
  45. context.report({ node, messageId: "unexpected" });
  46. }
  47. }
  48. };
  49. }
  50. };