no-template-curly-in-string.js 989 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * @fileoverview Warn when using template string syntax in regular strings
  3. * @author Jeroen Engels
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "problem",
  13. docs: {
  14. description:
  15. "Disallow template literal placeholder syntax in regular strings",
  16. recommended: false,
  17. url: "https://eslint.org/docs/latest/rules/no-template-curly-in-string",
  18. },
  19. schema: [],
  20. messages: {
  21. unexpectedTemplateExpression:
  22. "Unexpected template string expression.",
  23. },
  24. },
  25. create(context) {
  26. const regex = /\$\{[^}]+\}/u;
  27. return {
  28. Literal(node) {
  29. if (typeof node.value === "string" && regex.test(node.value)) {
  30. context.report({
  31. node,
  32. messageId: "unexpectedTemplateExpression",
  33. });
  34. }
  35. },
  36. };
  37. },
  38. };