no-consecutive-spaces-in-locales.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Reject runs of two or more consecutive whitespace characters in JSON string
  2. // values. Catches ASCII spaces, NBSP, NNBSP, tabs, and other Unicode whitespace.
  3. const CONSECUTIVE_WHITESPACE = /\s{2,}/
  4. module.exports = {
  5. meta: {
  6. type: 'problem',
  7. fixable: 'code',
  8. docs: {
  9. description:
  10. 'Disallow runs of two or more consecutive whitespace characters in JSON string values (typically locale files).',
  11. },
  12. schema: [],
  13. messages: {
  14. consecutiveSpaces:
  15. 'Locale value contains a run of consecutive whitespace. Collapse to a single space.',
  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 (!CONSECUTIVE_WHITESPACE.test(original)) return
  24. const fixed = original.replace(/\s{2,}/g, ' ')
  25. context.report({
  26. node: node.value,
  27. messageId: 'consecutiveSpaces',
  28. fix(fixer) {
  29. return fixer.replaceText(node.value, JSON.stringify(fixed))
  30. },
  31. })
  32. },
  33. }
  34. },
  35. }