valid-template-root.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. const utils = require('../utils')
  8. module.exports = {
  9. meta: {
  10. type: 'problem',
  11. docs: {
  12. description: 'enforce valid template root',
  13. categories: ['vue3-essential', 'essential'],
  14. url: 'https://eslint.vuejs.org/rules/valid-template-root.html'
  15. },
  16. fixable: null,
  17. schema: []
  18. },
  19. /** @param {RuleContext} context */
  20. create(context) {
  21. const sourceCode = context.getSourceCode()
  22. return {
  23. /** @param {Program} program */
  24. Program(program) {
  25. const element = program.templateBody
  26. if (element == null) {
  27. return
  28. }
  29. const hasSrc = utils.hasAttribute(element, 'src')
  30. const rootElements = []
  31. for (const child of element.children) {
  32. if (sourceCode.getText(child).trim() !== '') {
  33. rootElements.push(child)
  34. }
  35. }
  36. if (hasSrc && rootElements.length > 0) {
  37. for (const element of rootElements) {
  38. context.report({
  39. node: element,
  40. loc: element.loc,
  41. message:
  42. "The template root with 'src' attribute is required to be empty."
  43. })
  44. }
  45. } else if (rootElements.length === 0 && !hasSrc) {
  46. context.report({
  47. node: element,
  48. loc: element.loc,
  49. message: 'The template requires child element.'
  50. })
  51. }
  52. }
  53. }
  54. }
  55. }