v-bind-style.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2017 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. const utils = require('../utils')
  8. const casing = require('../utils/casing')
  9. /**
  10. * @typedef { VDirectiveKey & { name: VIdentifier & { name: 'bind' }, argument: VExpressionContainer | VIdentifier } } VBindDirectiveKey
  11. * @typedef { VDirective & { key: VBindDirectiveKey } } VBindDirective
  12. */
  13. /**
  14. * @param {string} name
  15. * @returns {string}
  16. */
  17. function kebabCaseToCamelCase(name) {
  18. return casing.isKebabCase(name) ? casing.camelCase(name) : name
  19. }
  20. /**
  21. * @param {VBindDirective} node
  22. * @returns {boolean}
  23. */
  24. function isSameName(node) {
  25. const attrName =
  26. node.key.argument.type === 'VIdentifier' ? node.key.argument.rawName : null
  27. const valueName =
  28. node.value?.expression?.type === 'Identifier'
  29. ? node.value.expression.name
  30. : null
  31. if (!attrName || !valueName) return false
  32. return kebabCaseToCamelCase(attrName) === kebabCaseToCamelCase(valueName)
  33. }
  34. /**
  35. * @param {VBindDirectiveKey} key
  36. * @returns {number}
  37. */
  38. function getCutStart(key) {
  39. const modifiers = key.modifiers
  40. return modifiers.length > 0
  41. ? modifiers[modifiers.length - 1].range[1]
  42. : key.argument.range[1]
  43. }
  44. module.exports = {
  45. meta: {
  46. type: 'suggestion',
  47. docs: {
  48. description: 'enforce `v-bind` directive style',
  49. categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
  50. url: 'https://eslint.vuejs.org/rules/v-bind-style.html'
  51. },
  52. fixable: 'code',
  53. schema: [
  54. { enum: ['shorthand', 'longform'] },
  55. {
  56. type: 'object',
  57. properties: {
  58. sameNameShorthand: { enum: ['always', 'never', 'ignore'] }
  59. },
  60. additionalProperties: false
  61. }
  62. ],
  63. messages: {
  64. expectedLonghand: "Expected 'v-bind' before ':'.",
  65. unexpectedLonghand: "Unexpected 'v-bind' before ':'.",
  66. expectedLonghandForProp: "Expected 'v-bind:' instead of '.'.",
  67. expectedShorthand: 'Expected same-name shorthand.',
  68. unexpectedShorthand: 'Unexpected same-name shorthand.'
  69. }
  70. },
  71. /** @param {RuleContext} context */
  72. create(context) {
  73. const preferShorthand = context.options[0] !== 'longform'
  74. /** @type {"always" | "never" | "ignore"} */
  75. const sameNameShorthand = context.options[1]?.sameNameShorthand || 'ignore'
  76. /** @param {VBindDirective} node */
  77. function checkAttributeStyle(node) {
  78. const shorthandProp = node.key.name.rawName === '.'
  79. const shorthand = node.key.name.rawName === ':' || shorthandProp
  80. if (shorthand === preferShorthand) {
  81. return
  82. }
  83. let messageId = 'expectedLonghand'
  84. if (preferShorthand) {
  85. messageId = 'unexpectedLonghand'
  86. } else if (shorthandProp) {
  87. messageId = 'expectedLonghandForProp'
  88. }
  89. context.report({
  90. node,
  91. loc: node.loc,
  92. messageId,
  93. *fix(fixer) {
  94. if (preferShorthand) {
  95. yield fixer.remove(node.key.name)
  96. } else {
  97. yield fixer.insertTextBefore(node, 'v-bind')
  98. if (shorthandProp) {
  99. // Replace `.` by `:`.
  100. yield fixer.replaceText(node.key.name, ':')
  101. // Insert `.prop` modifier if it doesn't exist.
  102. const modifier = node.key.modifiers[0]
  103. const isAutoGeneratedPropModifier =
  104. modifier.name === 'prop' && modifier.rawName === ''
  105. if (isAutoGeneratedPropModifier) {
  106. yield fixer.insertTextBefore(modifier, '.prop')
  107. }
  108. }
  109. }
  110. }
  111. })
  112. }
  113. /** @param {VBindDirective} node */
  114. function checkAttributeSameName(node) {
  115. if (sameNameShorthand === 'ignore' || !isSameName(node)) return
  116. const preferShorthand = sameNameShorthand === 'always'
  117. const isShorthand = utils.isVBindSameNameShorthand(node)
  118. if (isShorthand === preferShorthand) {
  119. return
  120. }
  121. const messageId = preferShorthand
  122. ? 'expectedShorthand'
  123. : 'unexpectedShorthand'
  124. context.report({
  125. node,
  126. loc: node.loc,
  127. messageId,
  128. *fix(fixer) {
  129. if (preferShorthand) {
  130. /** @type {Range} */
  131. const valueRange = [getCutStart(node.key), node.range[1]]
  132. yield fixer.removeRange(valueRange)
  133. } else if (node.key.argument.type === 'VIdentifier') {
  134. yield fixer.insertTextAfter(
  135. node,
  136. `="${kebabCaseToCamelCase(node.key.argument.rawName)}"`
  137. )
  138. }
  139. }
  140. })
  141. }
  142. return utils.defineTemplateBodyVisitor(context, {
  143. /** @param {VBindDirective} node */
  144. "VAttribute[directive=true][key.name.name='bind'][key.argument!=null]"(
  145. node
  146. ) {
  147. checkAttributeSameName(node)
  148. checkAttributeStyle(node)
  149. }
  150. })
  151. }
  152. }