singleline-html-element-content-newline.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. const casing = require('../utils/casing')
  11. const INLINE_ELEMENTS = require('../utils/inline-non-void-elements.json')
  12. // ------------------------------------------------------------------------------
  13. // Helpers
  14. // ------------------------------------------------------------------------------
  15. /**
  16. * @param {VElement & { endTag: VEndTag } } element
  17. */
  18. function isSinglelineElement(element) {
  19. return element.loc.start.line === element.endTag.loc.start.line
  20. }
  21. /**
  22. * @param {any} options
  23. */
  24. function parseOptions(options) {
  25. return Object.assign(
  26. {
  27. ignores: ['pre', 'textarea'].concat(INLINE_ELEMENTS),
  28. ignoreWhenNoAttributes: true,
  29. ignoreWhenEmpty: true
  30. },
  31. options
  32. )
  33. }
  34. /**
  35. * Check whether the given element is empty or not.
  36. * This ignores whitespaces, doesn't ignore comments.
  37. * @param {VElement & { endTag: VEndTag } } node The element node to check.
  38. * @param {SourceCode} sourceCode The source code object of the current context.
  39. * @returns {boolean} `true` if the element is empty.
  40. */
  41. function isEmpty(node, sourceCode) {
  42. const start = node.startTag.range[1]
  43. const end = node.endTag.range[0]
  44. return sourceCode.text.slice(start, end).trim() === ''
  45. }
  46. // ------------------------------------------------------------------------------
  47. // Rule Definition
  48. // ------------------------------------------------------------------------------
  49. module.exports = {
  50. meta: {
  51. type: 'layout',
  52. docs: {
  53. description:
  54. 'require a line break before and after the contents of a singleline element',
  55. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  56. url: 'https://eslint.vuejs.org/rules/singleline-html-element-content-newline.html'
  57. },
  58. fixable: 'whitespace',
  59. schema: [
  60. {
  61. type: 'object',
  62. properties: {
  63. ignoreWhenNoAttributes: {
  64. type: 'boolean'
  65. },
  66. ignoreWhenEmpty: {
  67. type: 'boolean'
  68. },
  69. ignores: {
  70. type: 'array',
  71. items: { type: 'string' },
  72. uniqueItems: true,
  73. additionalItems: false
  74. }
  75. },
  76. additionalProperties: false
  77. }
  78. ],
  79. messages: {
  80. unexpectedAfterClosingBracket:
  81. 'Expected 1 line break after opening tag (`<{{name}}>`), but no line breaks found.',
  82. unexpectedBeforeOpeningBracket:
  83. 'Expected 1 line break before closing tag (`</{{name}}>`), but no line breaks found.'
  84. }
  85. },
  86. /** @param {RuleContext} context */
  87. create(context) {
  88. const options = parseOptions(context.options[0])
  89. const ignores = options.ignores
  90. const ignoreWhenNoAttributes = options.ignoreWhenNoAttributes
  91. const ignoreWhenEmpty = options.ignoreWhenEmpty
  92. const template =
  93. context.parserServices.getTemplateBodyTokenStore &&
  94. context.parserServices.getTemplateBodyTokenStore()
  95. const sourceCode = context.getSourceCode()
  96. /** @type {VElement | null} */
  97. let inIgnoreElement = null
  98. /** @param {VElement} node */
  99. function isIgnoredElement(node) {
  100. return (
  101. ignores.includes(node.name) ||
  102. ignores.includes(casing.pascalCase(node.rawName)) ||
  103. ignores.includes(casing.kebabCase(node.rawName))
  104. )
  105. }
  106. return utils.defineTemplateBodyVisitor(context, {
  107. /** @param {VElement} node */
  108. VElement(node) {
  109. if (inIgnoreElement) {
  110. return
  111. }
  112. if (isIgnoredElement(node)) {
  113. // ignore element name
  114. inIgnoreElement = node
  115. return
  116. }
  117. if (node.startTag.selfClosing || !node.endTag) {
  118. // self closing
  119. return
  120. }
  121. const elem = /** @type {VElement & { endTag: VEndTag } } */ (node)
  122. if (!isSinglelineElement(elem)) {
  123. return
  124. }
  125. if (ignoreWhenNoAttributes && elem.startTag.attributes.length === 0) {
  126. return
  127. }
  128. /** @type {SourceCode.CursorWithCountOptions} */
  129. const getTokenOption = {
  130. includeComments: true,
  131. filter: (token) => token.type !== 'HTMLWhitespace'
  132. }
  133. if (
  134. ignoreWhenEmpty &&
  135. elem.children.length === 0 &&
  136. template.getFirstTokensBetween(
  137. elem.startTag,
  138. elem.endTag,
  139. getTokenOption
  140. ).length === 0
  141. ) {
  142. return
  143. }
  144. const contentFirst = /** @type {Token} */ (
  145. template.getTokenAfter(elem.startTag, getTokenOption)
  146. )
  147. const contentLast = /** @type {Token} */ (
  148. template.getTokenBefore(elem.endTag, getTokenOption)
  149. )
  150. context.report({
  151. node: template.getLastToken(elem.startTag),
  152. loc: {
  153. start: elem.startTag.loc.end,
  154. end: contentFirst.loc.start
  155. },
  156. messageId: 'unexpectedAfterClosingBracket',
  157. data: {
  158. name: elem.rawName
  159. },
  160. fix(fixer) {
  161. /** @type {Range} */
  162. const range = [elem.startTag.range[1], contentFirst.range[0]]
  163. return fixer.replaceTextRange(range, '\n')
  164. }
  165. })
  166. if (isEmpty(elem, sourceCode)) {
  167. return
  168. }
  169. context.report({
  170. node: template.getFirstToken(elem.endTag),
  171. loc: {
  172. start: contentLast.loc.end,
  173. end: elem.endTag.loc.start
  174. },
  175. messageId: 'unexpectedBeforeOpeningBracket',
  176. data: {
  177. name: elem.rawName
  178. },
  179. fix(fixer) {
  180. /** @type {Range} */
  181. const range = [contentLast.range[1], elem.endTag.range[0]]
  182. return fixer.replaceTextRange(range, '\n')
  183. }
  184. })
  185. },
  186. /** @param {VElement} node */
  187. 'VElement:exit'(node) {
  188. if (inIgnoreElement === node) {
  189. inIgnoreElement = null
  190. }
  191. }
  192. })
  193. }
  194. }