mustache-interpolation-spacing.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @fileoverview enforce unified spacing in mustache interpolations.
  3. * @author Armano
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: 'layout',
  16. docs: {
  17. description: 'enforce unified spacing in mustache interpolations',
  18. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  19. url: 'https://eslint.vuejs.org/rules/mustache-interpolation-spacing.html'
  20. },
  21. fixable: 'whitespace',
  22. schema: [
  23. {
  24. enum: ['always', 'never']
  25. }
  26. ]
  27. },
  28. /** @param {RuleContext} context */
  29. create(context) {
  30. const options = context.options[0] || 'always'
  31. const template =
  32. context.parserServices.getTemplateBodyTokenStore &&
  33. context.parserServices.getTemplateBodyTokenStore()
  34. // ----------------------------------------------------------------------
  35. // Public
  36. // ----------------------------------------------------------------------
  37. return utils.defineTemplateBodyVisitor(context, {
  38. /** @param {VExpressionContainer} node */
  39. 'VExpressionContainer[expression!=null]'(node) {
  40. const openBrace = template.getFirstToken(node)
  41. const closeBrace = template.getLastToken(node)
  42. if (
  43. !openBrace ||
  44. !closeBrace ||
  45. openBrace.type !== 'VExpressionStart' ||
  46. closeBrace.type !== 'VExpressionEnd'
  47. ) {
  48. return
  49. }
  50. const firstToken = template.getTokenAfter(openBrace, {
  51. includeComments: true
  52. })
  53. const lastToken = template.getTokenBefore(closeBrace, {
  54. includeComments: true
  55. })
  56. if (options === 'always') {
  57. if (openBrace.range[1] === firstToken.range[0]) {
  58. context.report({
  59. node: openBrace,
  60. message: "Expected 1 space after '{{', but not found.",
  61. fix: (fixer) => fixer.insertTextAfter(openBrace, ' ')
  62. })
  63. }
  64. if (closeBrace.range[0] === lastToken.range[1]) {
  65. context.report({
  66. node: closeBrace,
  67. message: "Expected 1 space before '}}', but not found.",
  68. fix: (fixer) => fixer.insertTextBefore(closeBrace, ' ')
  69. })
  70. }
  71. } else {
  72. if (openBrace.range[1] !== firstToken.range[0]) {
  73. context.report({
  74. loc: {
  75. start: openBrace.loc.start,
  76. end: firstToken.loc.start
  77. },
  78. message: "Expected no space after '{{', but found.",
  79. fix: (fixer) =>
  80. fixer.removeRange([openBrace.range[1], firstToken.range[0]])
  81. })
  82. }
  83. if (closeBrace.range[0] !== lastToken.range[1]) {
  84. context.report({
  85. loc: {
  86. start: lastToken.loc.end,
  87. end: closeBrace.loc.end
  88. },
  89. message: "Expected no space before '}}', but found.",
  90. fix: (fixer) =>
  91. fixer.removeRange([lastToken.range[1], closeBrace.range[0]])
  92. })
  93. }
  94. }
  95. }
  96. })
  97. }
  98. }