max-attributes-per-line.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @fileoverview Define the number of attributes allows per line
  3. * @author Filipa Lacerda
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Rule Definition
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. module.exports = {
  11. meta: {
  12. type: 'layout',
  13. docs: {
  14. description: 'enforce the maximum number of attributes per line',
  15. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  16. url: 'https://eslint.vuejs.org/rules/max-attributes-per-line.html'
  17. },
  18. fixable: 'whitespace', // or "code" or "whitespace"
  19. schema: [
  20. {
  21. type: 'object',
  22. properties: {
  23. singleline: {
  24. anyOf: [
  25. {
  26. type: 'number',
  27. minimum: 1
  28. },
  29. {
  30. type: 'object',
  31. properties: {
  32. max: {
  33. type: 'number',
  34. minimum: 1
  35. }
  36. },
  37. additionalProperties: false
  38. }
  39. ]
  40. },
  41. multiline: {
  42. anyOf: [
  43. {
  44. type: 'number',
  45. minimum: 1
  46. },
  47. {
  48. type: 'object',
  49. properties: {
  50. max: {
  51. type: 'number',
  52. minimum: 1
  53. }
  54. },
  55. additionalProperties: false
  56. }
  57. ]
  58. }
  59. },
  60. additionalProperties: false
  61. }
  62. ]
  63. },
  64. /** @param {RuleContext} context */
  65. create(context) {
  66. const sourceCode = context.getSourceCode()
  67. const configuration = parseOptions(context.options[0])
  68. const multilineMaximum = configuration.multiline
  69. const singlelinemMaximum = configuration.singleline
  70. const template =
  71. context.parserServices.getTemplateBodyTokenStore &&
  72. context.parserServices.getTemplateBodyTokenStore()
  73. return utils.defineTemplateBodyVisitor(context, {
  74. VStartTag(node) {
  75. const numberOfAttributes = node.attributes.length
  76. if (!numberOfAttributes) return
  77. if (utils.isSingleLine(node)) {
  78. if (numberOfAttributes > singlelinemMaximum) {
  79. showErrors(node.attributes.slice(singlelinemMaximum))
  80. }
  81. }
  82. if (!utils.isSingleLine(node)) {
  83. groupAttrsByLine(node.attributes)
  84. .filter((attrs) => attrs.length > multilineMaximum)
  85. .forEach((attrs) => showErrors(attrs.splice(multilineMaximum)))
  86. }
  87. }
  88. })
  89. // ----------------------------------------------------------------------
  90. // Helpers
  91. // ----------------------------------------------------------------------
  92. /**
  93. * @param {any} options
  94. */
  95. function parseOptions(options) {
  96. const defaults = {
  97. singleline: 1,
  98. multiline: 1
  99. }
  100. if (options) {
  101. if (typeof options.singleline === 'number') {
  102. defaults.singleline = options.singleline
  103. } else if (typeof options.singleline === 'object') {
  104. if (typeof options.singleline.max === 'number') {
  105. defaults.singleline = options.singleline.max
  106. }
  107. }
  108. if (options.multiline) {
  109. if (typeof options.multiline === 'number') {
  110. defaults.multiline = options.multiline
  111. } else if (typeof options.multiline === 'object') {
  112. if (typeof options.multiline.max === 'number') {
  113. defaults.multiline = options.multiline.max
  114. }
  115. }
  116. }
  117. }
  118. return defaults
  119. }
  120. /**
  121. * @param {(VDirective | VAttribute)[]} attributes
  122. */
  123. function showErrors(attributes) {
  124. attributes.forEach((prop, i) => {
  125. context.report({
  126. node: prop,
  127. loc: prop.loc,
  128. message: "'{{name}}' should be on a new line.",
  129. data: { name: sourceCode.getText(prop.key) },
  130. fix(fixer) {
  131. if (i !== 0) return null
  132. // Find the closest token before the current prop
  133. // that is not a white space
  134. const prevToken = /** @type {Token} */ (
  135. template.getTokenBefore(prop, {
  136. filter: (token) => token.type !== 'HTMLWhitespace'
  137. })
  138. )
  139. /** @type {Range} */
  140. const range = [prevToken.range[1], prop.range[0]]
  141. return fixer.replaceTextRange(range, '\n')
  142. }
  143. })
  144. })
  145. }
  146. /**
  147. * @param {(VDirective | VAttribute)[]} attributes
  148. */
  149. function groupAttrsByLine(attributes) {
  150. const propsPerLine = [[attributes[0]]]
  151. attributes.reduce((previous, current) => {
  152. if (previous.loc.end.line === current.loc.start.line) {
  153. propsPerLine[propsPerLine.length - 1].push(current)
  154. } else {
  155. propsPerLine.push([current])
  156. }
  157. return current
  158. })
  159. return propsPerLine
  160. }
  161. }
  162. }