max-attributes-per-line.js 4.6 KB

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