no-spaces-around-equal-signs-in-attribute.js 1.7 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. categories: ['vue3-strongly-recommended', '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. /** @param {RuleContext} context */
  25. create(context) {
  26. const sourceCode = context.getSourceCode()
  27. return utils.defineTemplateBodyVisitor(context, {
  28. VAttribute(node) {
  29. if (!node.value) {
  30. return
  31. }
  32. /** @type {Range} */
  33. const range = [node.key.range[1], node.value.range[0]]
  34. const eqText = sourceCode.text.slice(range[0], range[1])
  35. const expect = eqText.trim()
  36. if (eqText !== expect) {
  37. context.report({
  38. node: node.key,
  39. loc: {
  40. start: node.key.loc.end,
  41. end: node.value.loc.start
  42. },
  43. message: 'Unexpected spaces found around equal signs.',
  44. data: {},
  45. fix: (fixer) => fixer.replaceTextRange(range, expect)
  46. })
  47. }
  48. }
  49. })
  50. }
  51. }