first-attribute-linebreak.js 2.9 KB

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