first-attribute-linebreak.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @fileoverview Enforce the location of first attribute
  3. * @author Yosuke Ota
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. module.exports = {
  8. meta: {
  9. type: 'layout',
  10. docs: {
  11. description: 'enforce the location of first attribute',
  12. categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
  13. url: 'https://eslint.vuejs.org/rules/first-attribute-linebreak.html'
  14. },
  15. fixable: 'whitespace',
  16. schema: [
  17. {
  18. type: 'object',
  19. properties: {
  20. multiline: { enum: ['below', 'beside', 'ignore'] },
  21. singleline: { enum: ['below', 'beside', 'ignore'] }
  22. },
  23. additionalProperties: false
  24. }
  25. ],
  26. messages: {
  27. expected: 'Expected a linebreak before this attribute.',
  28. unexpected: 'Expected no linebreak before this attribute.'
  29. }
  30. },
  31. /** @param {RuleContext} context */
  32. create(context) {
  33. /** @type {"below" | "beside" | "ignore"} */
  34. const singleline =
  35. (context.options[0] && context.options[0].singleline) || 'ignore'
  36. /** @type {"below" | "beside" | "ignore"} */
  37. const multiline =
  38. (context.options[0] && context.options[0].multiline) || 'below'
  39. const sourceCode = context.getSourceCode()
  40. const template =
  41. sourceCode.parserServices.getTemplateBodyTokenStore &&
  42. sourceCode.parserServices.getTemplateBodyTokenStore()
  43. /**
  44. * Report attribute
  45. * @param {VAttribute | VDirective} firstAttribute
  46. * @param { "below" | "beside"} location
  47. */
  48. function report(firstAttribute, location) {
  49. context.report({
  50. node: firstAttribute,
  51. messageId: location === 'beside' ? 'unexpected' : 'expected',
  52. fix(fixer) {
  53. const prevToken = template.getTokenBefore(firstAttribute, {
  54. includeComments: true
  55. })
  56. return fixer.replaceTextRange(
  57. [prevToken.range[1], firstAttribute.range[0]],
  58. location === 'beside' ? ' ' : '\n'
  59. )
  60. }
  61. })
  62. }
  63. return utils.defineTemplateBodyVisitor(context, {
  64. VStartTag(node) {
  65. const firstAttribute = node.attributes[0]
  66. if (!firstAttribute) return
  67. const lastAttribute = node.attributes[node.attributes.length - 1]
  68. const location =
  69. firstAttribute.loc.start.line === lastAttribute.loc.end.line
  70. ? singleline
  71. : multiline
  72. if (location === 'ignore') {
  73. return
  74. }
  75. if (location === 'beside') {
  76. if (node.loc.start.line === firstAttribute.loc.start.line) {
  77. return
  78. }
  79. } else {
  80. if (node.loc.start.line < firstAttribute.loc.start.line) {
  81. return
  82. }
  83. }
  84. report(firstAttribute, location)
  85. }
  86. })
  87. }
  88. }