max-attributes-per-line.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 sourceCode = context.getSourceCode()
  68. const configuration = parseOptions(context.options[0])
  69. const multilineMaximum = configuration.multiline
  70. const singlelinemMaximum = configuration.singleline
  71. const canHaveFirstLine = configuration.allowFirstLine
  72. const template = context.parserServices.getTemplateBodyTokenStore && 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) && numberOfAttributes > singlelinemMaximum) {
  78. showErrors(node.attributes.slice(singlelinemMaximum))
  79. }
  80. if (!utils.isSingleLine(node)) {
  81. if (!canHaveFirstLine && node.attributes[0].loc.start.line === node.loc.start.line) {
  82. showErrors([node.attributes[0]])
  83. }
  84. groupAttrsByLine(node.attributes)
  85. .filter(attrs => attrs.length > multilineMaximum)
  86. .forEach(attrs => showErrors(attrs.splice(multilineMaximum)))
  87. }
  88. }
  89. })
  90. // ----------------------------------------------------------------------
  91. // Helpers
  92. // ----------------------------------------------------------------------
  93. function parseOptions (options) {
  94. const defaults = {
  95. singleline: 1,
  96. multiline: 1,
  97. allowFirstLine: false
  98. }
  99. if (options) {
  100. if (typeof options.singleline === 'number') {
  101. defaults.singleline = options.singleline
  102. } else if (options.singleline && options.singleline.max) {
  103. defaults.singleline = options.singleline.max
  104. }
  105. if (options.multiline) {
  106. if (typeof options.multiline === 'number') {
  107. defaults.multiline = options.multiline
  108. } else if (typeof options.multiline === 'object') {
  109. if (options.multiline.max) {
  110. defaults.multiline = options.multiline.max
  111. }
  112. if (options.multiline.allowFirstLine) {
  113. defaults.allowFirstLine = options.multiline.allowFirstLine
  114. }
  115. }
  116. }
  117. }
  118. return defaults
  119. }
  120. function showErrors (attributes) {
  121. attributes.forEach((prop, i) => {
  122. const fix = (fixer) => {
  123. if (i !== 0) return null
  124. // Find the closest token before the current prop
  125. // that is not a white space
  126. const prevToken = template.getTokenBefore(prop, {
  127. filter: (token) => token.type !== 'HTMLWhitespace'
  128. })
  129. const range = [prevToken.range[1], prop.range[0]]
  130. return fixer.replaceTextRange(range, '\n')
  131. }
  132. context.report({
  133. node: prop,
  134. loc: prop.loc,
  135. message: '\'{{name}}\' should be on a new line.',
  136. data: { name: sourceCode.getText(prop.key) },
  137. fix
  138. })
  139. })
  140. }
  141. function groupAttrsByLine (attributes) {
  142. const propsPerLine = [[attributes[0]]]
  143. attributes.reduce((previous, current) => {
  144. if (previous.loc.end.line === current.loc.start.line) {
  145. propsPerLine[propsPerLine.length - 1].push(current)
  146. } else {
  147. propsPerLine.push([current])
  148. }
  149. return current
  150. })
  151. return propsPerLine
  152. }
  153. }
  154. }