valid-template-root.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2017 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. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: 'enforce valid template root',
  18. category: 'essential',
  19. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/valid-template-root.md'
  20. },
  21. fixable: null,
  22. schema: []
  23. },
  24. create (context) {
  25. const sourceCode = context.getSourceCode()
  26. return {
  27. Program (program) {
  28. const element = program.templateBody
  29. if (element == null) {
  30. return
  31. }
  32. const hasSrc = utils.hasAttribute(element, 'src')
  33. const rootElements = []
  34. let extraText = null
  35. let extraElement = null
  36. let vIf = false
  37. for (const child of element.children) {
  38. if (child.type === 'VElement') {
  39. if (rootElements.length === 0 && !hasSrc) {
  40. rootElements.push(child)
  41. vIf = utils.hasDirective(child, 'if')
  42. } else if (vIf && utils.hasDirective(child, 'else-if')) {
  43. rootElements.push(child)
  44. } else if (vIf && utils.hasDirective(child, 'else')) {
  45. rootElements.push(child)
  46. vIf = false
  47. } else {
  48. extraElement = child
  49. }
  50. } else if (sourceCode.getText(child).trim() !== '') {
  51. extraText = child
  52. }
  53. }
  54. if (hasSrc && (extraText != null || extraElement != null)) {
  55. context.report({
  56. node: extraText || extraElement,
  57. loc: (extraText || extraElement).loc,
  58. message: "The template root with 'src' attribute is required to be empty."
  59. })
  60. } else if (extraText != null) {
  61. context.report({
  62. node: extraText,
  63. loc: extraText.loc,
  64. message: 'The template root requires an element rather than texts.'
  65. })
  66. } else if (extraElement != null) {
  67. context.report({
  68. node: extraElement,
  69. loc: extraElement.loc,
  70. message: 'The template root requires exactly one element.'
  71. })
  72. } else if (rootElements.length === 0 && !hasSrc) {
  73. context.report({
  74. node: element,
  75. loc: element.loc,
  76. message: 'The template root requires exactly one element.'
  77. })
  78. } else {
  79. for (const element of rootElements) {
  80. const tag = element.startTag
  81. const name = element.name
  82. if (name === 'template' || name === 'slot') {
  83. context.report({
  84. node: tag,
  85. loc: tag.loc,
  86. message: "The template root disallows '<{{name}}>' elements.",
  87. data: { name }
  88. })
  89. }
  90. if (utils.hasDirective(element, 'for')) {
  91. context.report({
  92. node: tag,
  93. loc: tag.loc,
  94. message: "The template root disallows 'v-for' directives."
  95. })
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }