v-slot-style.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const { pascalCase } = require('../utils/casing')
  7. const utils = require('../utils')
  8. /**
  9. * @typedef {Object} Options
  10. * @property {"shorthand" | "longform" | "v-slot"} atComponent The style for the default slot at a custom component directly.
  11. * @property {"shorthand" | "longform" | "v-slot"} default The style for the default slot at a template wrapper.
  12. * @property {"shorthand" | "longform"} named The style for named slots at a template wrapper.
  13. */
  14. /**
  15. * Normalize options.
  16. * @param {any} options The raw options to normalize.
  17. * @returns {Options} The normalized options.
  18. */
  19. function normalizeOptions(options) {
  20. /** @type {Options} */
  21. const normalized = {
  22. atComponent: 'v-slot',
  23. default: 'shorthand',
  24. named: 'shorthand'
  25. }
  26. if (typeof options === 'string') {
  27. normalized.atComponent =
  28. normalized.default =
  29. normalized.named =
  30. /** @type {"shorthand" | "longform"} */ (options)
  31. } else if (options != null) {
  32. /** @type {(keyof Options)[]} */
  33. const keys = ['atComponent', 'default', 'named']
  34. for (const key of keys) {
  35. if (options[key] != null) {
  36. normalized[key] = options[key]
  37. }
  38. }
  39. }
  40. return normalized
  41. }
  42. /**
  43. * Get the expected style.
  44. * @param {Options} options The options that defined expected types.
  45. * @param {VDirective} node The `v-slot` node to check.
  46. * @returns {"shorthand" | "longform" | "v-slot"} The expected style.
  47. */
  48. function getExpectedStyle(options, node) {
  49. const { argument } = node.key
  50. if (
  51. argument == null ||
  52. (argument.type === 'VIdentifier' && argument.name === 'default')
  53. ) {
  54. const element = node.parent.parent
  55. return element.name === 'template' ? options.default : options.atComponent
  56. }
  57. return options.named
  58. }
  59. /**
  60. * Get the expected style.
  61. * @param {VDirective} node The `v-slot` node to check.
  62. * @returns {"shorthand" | "longform" | "v-slot"} The expected style.
  63. */
  64. function getActualStyle(node) {
  65. const { name, argument } = node.key
  66. if (name.rawName === '#') {
  67. return 'shorthand'
  68. }
  69. if (argument != null) {
  70. return 'longform'
  71. }
  72. return 'v-slot'
  73. }
  74. module.exports = {
  75. meta: {
  76. type: 'suggestion',
  77. docs: {
  78. description: 'enforce `v-slot` directive style',
  79. categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
  80. url: 'https://eslint.vuejs.org/rules/v-slot-style.html'
  81. },
  82. fixable: 'code',
  83. schema: [
  84. {
  85. anyOf: [
  86. { enum: ['shorthand', 'longform'] },
  87. {
  88. type: 'object',
  89. properties: {
  90. atComponent: { enum: ['shorthand', 'longform', 'v-slot'] },
  91. default: { enum: ['shorthand', 'longform', 'v-slot'] },
  92. named: { enum: ['shorthand', 'longform'] }
  93. },
  94. additionalProperties: false
  95. }
  96. ]
  97. }
  98. ],
  99. messages: {
  100. expectedShorthand: "Expected '#{{argument}}' instead of '{{actual}}'.",
  101. expectedLongform:
  102. "Expected 'v-slot:{{argument}}' instead of '{{actual}}'.",
  103. expectedVSlot: "Expected 'v-slot' instead of '{{actual}}'."
  104. }
  105. },
  106. /** @param {RuleContext} context */
  107. create(context) {
  108. const sourceCode = context.getSourceCode()
  109. const options = normalizeOptions(context.options[0])
  110. return utils.defineTemplateBodyVisitor(context, {
  111. /** @param {VDirective} node */
  112. "VAttribute[directive=true][key.name.name='slot']"(node) {
  113. const expected = getExpectedStyle(options, node)
  114. const actual = getActualStyle(node)
  115. if (actual === expected) {
  116. return
  117. }
  118. const { name, argument } = node.key
  119. /** @type {Range} */
  120. const range = [name.range[0], (argument || name).range[1]]
  121. const argumentText = argument ? sourceCode.getText(argument) : 'default'
  122. context.report({
  123. node,
  124. messageId: `expected${pascalCase(expected)}`,
  125. data: {
  126. actual: sourceCode.text.slice(range[0], range[1]),
  127. argument: argumentText
  128. },
  129. fix(fixer) {
  130. switch (expected) {
  131. case 'shorthand': {
  132. return fixer.replaceTextRange(range, `#${argumentText}`)
  133. }
  134. case 'longform': {
  135. return fixer.replaceTextRange(range, `v-slot:${argumentText}`)
  136. }
  137. case 'v-slot': {
  138. return fixer.replaceTextRange(range, 'v-slot')
  139. }
  140. default: {
  141. return null
  142. }
  143. }
  144. }
  145. })
  146. }
  147. })
  148. }
  149. }