no-octal-escape.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @fileoverview Rule to flag octal escape sequences in string literals.
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Disallow octal escape sequences in string literals",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-octal-escape"
  17. },
  18. schema: [],
  19. messages: {
  20. octalEscapeSequence: "Don't use octal: '\\{{sequence}}'. Use '\\u....' instead."
  21. }
  22. },
  23. create(context) {
  24. return {
  25. Literal(node) {
  26. if (typeof node.value !== "string") {
  27. return;
  28. }
  29. // \0 represents a valid NULL character if it isn't followed by a digit.
  30. const match = node.raw.match(
  31. /^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|0(?=[89])|[1-7])/su
  32. );
  33. if (match) {
  34. context.report({
  35. node,
  36. messageId: "octalEscapeSequence",
  37. data: { sequence: match[1] }
  38. });
  39. }
  40. }
  41. };
  42. }
  43. };