valid-template-root.js 3.5 KB

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