max-attributes-per-line.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. category: '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. allowFirstLine: {
  55. type: 'boolean'
  56. }
  57. },
  58. additionalProperties: false
  59. }
  60. ]
  61. }
  62. }
  63. }
  64. ]
  65. },
  66. create: function (context) {
  67. const configuration = parseOptions(context.options[0])
  68. const multilineMaximum = configuration.multiline
  69. const singlelinemMaximum = configuration.singleline
  70. const canHaveFirstLine = configuration.allowFirstLine
  71. const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
  72. return utils.defineTemplateBodyVisitor(context, {
  73. 'VStartTag' (node) {
  74. const numberOfAttributes = node.attributes.length
  75. if (!numberOfAttributes) return
  76. if (utils.isSingleLine(node) && numberOfAttributes > singlelinemMaximum) {
  77. showErrors(node.attributes.slice(singlelinemMaximum))
  78. }
  79. if (!utils.isSingleLine(node)) {
  80. if (!canHaveFirstLine && node.attributes[0].loc.start.line === node.loc.start.line) {
  81. showErrors([node.attributes[0]])
  82. }
  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. function parseOptions (options) {
  93. const defaults = {
  94. singleline: 1,
  95. multiline: 1,
  96. allowFirstLine: false
  97. }
  98. if (options) {
  99. if (typeof options.singleline === 'number') {
  100. defaults.singleline = options.singleline
  101. } else if (options.singleline && options.singleline.max) {
  102. defaults.singleline = options.singleline.max
  103. }
  104. if (options.multiline) {
  105. if (typeof options.multiline === 'number') {
  106. defaults.multiline = options.multiline
  107. } else if (typeof options.multiline === 'object') {
  108. if (options.multiline.max) {
  109. defaults.multiline = options.multiline.max
  110. }
  111. if (options.multiline.allowFirstLine) {
  112. defaults.allowFirstLine = options.multiline.allowFirstLine
  113. }
  114. }
  115. }
  116. }
  117. return defaults
  118. }
  119. function getPropData (prop) {
  120. let propType = 'Attribute'
  121. let propName = prop.key.name
  122. if (utils.isBindingAttribute(prop)) {
  123. propType = 'Binding'
  124. propName = prop.key.raw.argument
  125. } else if (utils.isEventAttribute(prop)) {
  126. propType = 'Event'
  127. propName = prop.key.raw.argument
  128. } else if (prop.directive) {
  129. propType = 'Directive'
  130. }
  131. return { propType, propName }
  132. }
  133. function showErrors (attributes) {
  134. attributes.forEach((prop, i) => {
  135. const fix = (fixer) => {
  136. if (i !== 0) return null
  137. // Find the closest token before the current prop
  138. // that is not a white space
  139. const prevToken = template.getTokenBefore(prop, {
  140. filter: (token) => token.type !== 'HTMLWhitespace'
  141. })
  142. const range = [prevToken.range[1], prop.range[0]]
  143. return fixer.replaceTextRange(range, '\n')
  144. }
  145. context.report({
  146. node: prop,
  147. loc: prop.loc,
  148. message: '{{propType}} "{{propName}}" should be on a new line.',
  149. data: getPropData(prop),
  150. fix
  151. })
  152. })
  153. }
  154. function groupAttrsByLine (attributes) {
  155. const propsPerLine = [[attributes[0]]]
  156. attributes.reduce((previous, current) => {
  157. if (previous.loc.end.line === current.loc.start.line) {
  158. propsPerLine[propsPerLine.length - 1].push(current)
  159. } else {
  160. propsPerLine.push([current])
  161. }
  162. return current
  163. })
  164. return propsPerLine
  165. }
  166. }
  167. }