no-div-regex.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * @fileoverview Rule to check for ambiguous div operator in regexes
  3. * @author Matt DuVall <http://www.mattduvall.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow division operators explicitly at the beginning of regular expressions",
  13. category: "Best Practices",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/no-div-regex"
  16. },
  17. schema: [],
  18. messages: {
  19. unexpected: "A regular expression literal can be confused with '/='."
  20. }
  21. },
  22. create(context) {
  23. const sourceCode = context.getSourceCode();
  24. return {
  25. Literal(node) {
  26. const token = sourceCode.getFirstToken(node);
  27. if (token.type === "RegularExpression" && token.value[1] === "=") {
  28. context.report({ node, messageId: "unexpected" });
  29. }
  30. }
  31. };
  32. }
  33. };