multiline-html-element-content-newline.js 6.2 KB

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