html-self-closing.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. // ------------------------------------------------------------------------------
  8. // Requirements
  9. // ------------------------------------------------------------------------------
  10. const utils = require('../utils')
  11. // ------------------------------------------------------------------------------
  12. // Helpers
  13. // ------------------------------------------------------------------------------
  14. /**
  15. * These strings wil be displayed in error messages.
  16. */
  17. const ELEMENT_TYPE = Object.freeze({
  18. NORMAL: 'HTML elements',
  19. VOID: 'HTML void elements',
  20. COMPONENT: 'Vue.js custom components',
  21. SVG: 'SVG elements',
  22. MATH: 'MathML elements'
  23. })
  24. /**
  25. * Normalize the given options.
  26. * @param {Object|undefined} options The raw options object.
  27. * @returns {Object} Normalized options.
  28. */
  29. function parseOptions (options) {
  30. return {
  31. [ELEMENT_TYPE.NORMAL]: (options && options.html && options.html.normal) || 'always',
  32. [ELEMENT_TYPE.VOID]: (options && options.html && options.html.void) || 'never',
  33. [ELEMENT_TYPE.COMPONENT]: (options && options.html && options.html.component) || 'always',
  34. [ELEMENT_TYPE.SVG]: (options && options.svg) || 'always',
  35. [ELEMENT_TYPE.MATH]: (options && options.math) || 'always'
  36. }
  37. }
  38. /**
  39. * Get the elementType of the given element.
  40. * @param {VElement} node The element node to get.
  41. * @returns {string} The elementType of the element.
  42. */
  43. function getElementType (node) {
  44. if (utils.isCustomComponent(node)) {
  45. return ELEMENT_TYPE.COMPONENT
  46. }
  47. if (utils.isHtmlElementNode(node)) {
  48. if (utils.isHtmlVoidElementName(node.name)) {
  49. return ELEMENT_TYPE.VOID
  50. }
  51. return ELEMENT_TYPE.NORMAL
  52. }
  53. if (utils.isSvgElementNode(node)) {
  54. return ELEMENT_TYPE.SVG
  55. }
  56. if (utils.isMathMLElementNode(node)) {
  57. return ELEMENT_TYPE.MATH
  58. }
  59. return 'unknown elements'
  60. }
  61. /**
  62. * Check whether the given element is empty or not.
  63. * This ignores whitespaces, doesn't ignore comments.
  64. * @param {VElement} node The element node to check.
  65. * @param {SourceCode} sourceCode The source code object of the current context.
  66. * @returns {boolean} `true` if the element is empty.
  67. */
  68. function isEmpty (node, sourceCode) {
  69. const start = node.startTag.range[1]
  70. const end = (node.endTag != null) ? node.endTag.range[0] : node.range[1]
  71. return sourceCode.text.slice(start, end).trim() === ''
  72. }
  73. // ------------------------------------------------------------------------------
  74. // Rule Definition
  75. // ------------------------------------------------------------------------------
  76. module.exports = {
  77. meta: {
  78. docs: {
  79. description: 'enforce self-closing style',
  80. category: 'strongly-recommended',
  81. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/html-self-closing.md'
  82. },
  83. fixable: 'code',
  84. schema: {
  85. definitions: {
  86. optionValue: {
  87. enum: ['always', 'never', 'any']
  88. }
  89. },
  90. type: 'array',
  91. items: [{
  92. type: 'object',
  93. properties: {
  94. html: {
  95. type: 'object',
  96. properties: {
  97. normal: { $ref: '#/definitions/optionValue' },
  98. void: { $ref: '#/definitions/optionValue' },
  99. component: { $ref: '#/definitions/optionValue' }
  100. },
  101. additionalProperties: false
  102. },
  103. svg: { $ref: '#/definitions/optionValue' },
  104. math: { $ref: '#/definitions/optionValue' }
  105. },
  106. additionalProperties: false
  107. }],
  108. maxItems: 1
  109. }
  110. },
  111. create (context) {
  112. const sourceCode = context.getSourceCode()
  113. const options = parseOptions(context.options[0])
  114. let hasInvalidEOF = false
  115. return utils.defineTemplateBodyVisitor(context, {
  116. 'VElement' (node) {
  117. if (hasInvalidEOF) {
  118. return
  119. }
  120. const elementType = getElementType(node)
  121. const mode = options[elementType]
  122. if (mode === 'always' && !node.startTag.selfClosing && isEmpty(node, sourceCode)) {
  123. context.report({
  124. node,
  125. loc: node.loc,
  126. message: 'Require self-closing on {{elementType}} (<{{name}}>).',
  127. data: { elementType, name: node.rawName },
  128. fix: (fixer) => {
  129. const tokens = context.parserServices.getTemplateBodyTokenStore()
  130. const close = tokens.getLastToken(node.startTag)
  131. if (close.type !== 'HTMLTagClose') {
  132. return null
  133. }
  134. return fixer.replaceTextRange([close.range[0], node.range[1]], '/>')
  135. }
  136. })
  137. }
  138. if (mode === 'never' && node.startTag.selfClosing) {
  139. context.report({
  140. node,
  141. loc: node.loc,
  142. message: 'Disallow self-closing on {{elementType}} (<{{name}}/>).',
  143. data: { elementType, name: node.rawName },
  144. fix: (fixer) => {
  145. const tokens = context.parserServices.getTemplateBodyTokenStore()
  146. const close = tokens.getLastToken(node.startTag)
  147. if (close.type !== 'HTMLSelfClosingTagClose') {
  148. return null
  149. }
  150. if (elementType === ELEMENT_TYPE.VOID) {
  151. return fixer.replaceText(close, '>')
  152. }
  153. return fixer.replaceText(close, `></${node.rawName}>`)
  154. }
  155. })
  156. }
  157. }
  158. }, {
  159. Program (node) {
  160. hasInvalidEOF = utils.hasInvalidEOF(node)
  161. }
  162. })
  163. }
  164. }