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.

wrap-regex.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @fileoverview Rule to flag when regex literals are not wrapped in parens
  3. * @author Matt DuVall <http://www.mattduvall.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "require parenthesis around regex literals",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/wrap-regex"
  17. },
  18. schema: [],
  19. fixable: "code",
  20. messages: {
  21. requireParens: "Wrap the regexp literal in parens to disambiguate the slash."
  22. }
  23. },
  24. create(context) {
  25. const sourceCode = context.getSourceCode();
  26. return {
  27. Literal(node) {
  28. const token = sourceCode.getFirstToken(node),
  29. nodeType = token.type;
  30. if (nodeType === "RegularExpression") {
  31. const beforeToken = sourceCode.getTokenBefore(node);
  32. const afterToken = sourceCode.getTokenAfter(node);
  33. const ancestors = context.getAncestors();
  34. const grandparent = ancestors[ancestors.length - 1];
  35. if (grandparent.type === "MemberExpression" && grandparent.object === node &&
  36. !(beforeToken && beforeToken.value === "(" && afterToken && afterToken.value === ")")) {
  37. context.report({
  38. node,
  39. messageId: "requireParens",
  40. fix: fixer => fixer.replaceText(node, `(${sourceCode.getText(node)})`)
  41. });
  42. }
  43. }
  44. }
  45. };
  46. }
  47. };