no-template-curly-in-string.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. module.exports = {
  10. meta: {
  11. type: "problem",
  12. docs: {
  13. description: "disallow template literal placeholder syntax in regular strings",
  14. category: "Possible Errors",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-template-curly-in-string"
  17. },
  18. schema: [],
  19. messages: {
  20. unexpectedTemplateExpression: "Unexpected template string expression."
  21. }
  22. },
  23. create(context) {
  24. const regex = /\$\{[^}]+\}/u;
  25. return {
  26. Literal(node) {
  27. if (typeof node.value === "string" && regex.test(node.value)) {
  28. context.report({
  29. node,
  30. messageId: "unexpectedTemplateExpression"
  31. });
  32. }
  33. }
  34. };
  35. }
  36. };