no-spaces-around-equal-signs-in-attribute.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @author Yosuke Ota
  3. * issue https://github.com/vuejs/eslint-plugin-vue/issues/460
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. module.exports = {
  8. meta: {
  9. type: 'layout',
  10. docs: {
  11. description: 'disallow spaces around equal signs in attribute',
  12. categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
  13. url: 'https://eslint.vuejs.org/rules/no-spaces-around-equal-signs-in-attribute.html'
  14. },
  15. fixable: 'whitespace',
  16. schema: [],
  17. messages: {
  18. unexpectedSpaces: 'Unexpected spaces found around equal signs.'
  19. }
  20. },
  21. /** @param {RuleContext} context */
  22. create(context) {
  23. const sourceCode = context.getSourceCode()
  24. return utils.defineTemplateBodyVisitor(context, {
  25. VAttribute(node) {
  26. if (!node.value) {
  27. return
  28. }
  29. /** @type {Range} */
  30. const range = [node.key.range[1], node.value.range[0]]
  31. const eqText = sourceCode.text.slice(range[0], range[1])
  32. const expect = eqText.trim()
  33. if (eqText !== expect) {
  34. context.report({
  35. node: node.key,
  36. loc: {
  37. start: node.key.loc.end,
  38. end: node.value.loc.start
  39. },
  40. messageId: 'unexpectedSpaces',
  41. fix: (fixer) => fixer.replaceTextRange(range, expect)
  42. })
  43. }
  44. }
  45. })
  46. }
  47. }