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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @author Yosuke Ota
  3. * issue https://github.com/vuejs/eslint-plugin-vue/issues/460
  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: 'disallow spaces around equal signs in attribute',
  18. category: 'strongly-recommended',
  19. url: 'https://eslint.vuejs.org/rules/no-spaces-around-equal-signs-in-attribute.html'
  20. },
  21. fixable: 'whitespace',
  22. schema: []
  23. },
  24. create (context) {
  25. const sourceCode = context.getSourceCode()
  26. return utils.defineTemplateBodyVisitor(context, {
  27. 'VAttribute' (node) {
  28. if (!node.value) {
  29. return
  30. }
  31. const range = [node.key.range[1], node.value.range[0]]
  32. const eqText = sourceCode.text.slice(range[0], range[1])
  33. const expect = eqText.trim()
  34. if (eqText !== expect) {
  35. context.report({
  36. node: node.key,
  37. loc: {
  38. start: node.key.loc.end,
  39. end: node.value.loc.start
  40. },
  41. message: 'Unexpected spaces found around equal signs.',
  42. data: {},
  43. fix: fixer => fixer.replaceTextRange(range, expect)
  44. })
  45. }
  46. }
  47. })
  48. }
  49. }