no-straight-apostrophes-in-locales.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Reject straight apostrophes (') in JSON string values. Straight apostrophes
  2. // in Angular templates can lead to XSS; use the right single quotation mark
  3. // ’ (U+2019) instead. See https://github.com/overleaf/issues/issues/4478
  4. module.exports = {
  5. meta: {
  6. type: 'problem',
  7. fixable: 'code',
  8. docs: {
  9. description:
  10. 'Disallow straight apostrophes in JSON string values (typically locale files).',
  11. },
  12. schema: [],
  13. messages: {
  14. straightApostrophe:
  15. "Locale value contains a straight apostrophe ('). Use the right single quotation mark ’ (U+2019) instead.",
  16. },
  17. },
  18. create(context) {
  19. return {
  20. Member(node) {
  21. if (node.value.type !== 'String') return
  22. const original = node.value.value
  23. if (!original.includes("'")) return
  24. const fixed = original.replace(/'/g, '’')
  25. context.report({
  26. node: node.value,
  27. messageId: 'straightApostrophe',
  28. fix(fixer) {
  29. return fixer.replaceText(node.value, JSON.stringify(fixed))
  30. },
  31. })
  32. },
  33. }
  34. },
  35. }