require-toggle-inside-transition.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. module.exports = {
  8. meta: {
  9. type: 'problem',
  10. docs: {
  11. description:
  12. 'require control the display of the content inside `<transition>`',
  13. categories: ['vue3-essential'],
  14. url: 'https://eslint.vuejs.org/rules/require-toggle-inside-transition.html'
  15. },
  16. fixable: null,
  17. schema: [],
  18. messages: {
  19. expected:
  20. 'The element inside `<transition>` is expected to have a `v-if` or `v-show` directive.'
  21. }
  22. },
  23. /** @param {RuleContext} context */
  24. create(context) {
  25. /**
  26. * Check if the given element has display control.
  27. * @param {VElement} element The element node to check.
  28. */
  29. function verifyInsideElement(element) {
  30. if (utils.isCustomComponent(element)) {
  31. return
  32. }
  33. if (
  34. !utils.hasDirective(element, 'if') &&
  35. !utils.hasDirective(element, 'show')
  36. ) {
  37. context.report({
  38. node: element.startTag,
  39. loc: element.startTag.loc,
  40. messageId: 'expected'
  41. })
  42. }
  43. }
  44. return utils.defineTemplateBodyVisitor(context, {
  45. /** @param {VElement} node */
  46. "VElement[name='transition'] > VElement"(node) {
  47. if (node.parent.children[0] !== node) {
  48. return
  49. }
  50. verifyInsideElement(node)
  51. }
  52. })
  53. }
  54. }