require-toggle-inside-transition.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: 'problem',
  16. docs: {
  17. description:
  18. 'require control the display of the content inside `<transition>`',
  19. categories: ['vue3-essential'],
  20. url:
  21. 'https://eslint.vuejs.org/rules/require-toggle-inside-transition.html'
  22. },
  23. fixable: null,
  24. schema: [],
  25. messages: {
  26. expected:
  27. 'The element inside `<transition>` is expected to have a `v-if` or `v-show` directive.'
  28. }
  29. },
  30. /** @param {RuleContext} context */
  31. create(context) {
  32. /**
  33. * Check if the given element has display control.
  34. * @param {VElement} element The element node to check.
  35. */
  36. function verifyInsideElement(element) {
  37. if (utils.isCustomComponent(element)) {
  38. return
  39. }
  40. if (
  41. !utils.hasDirective(element, 'if') &&
  42. !utils.hasDirective(element, 'show')
  43. ) {
  44. context.report({
  45. node: element.startTag,
  46. loc: element.startTag.loc,
  47. messageId: 'expected'
  48. })
  49. }
  50. }
  51. return utils.defineTemplateBodyVisitor(context, {
  52. /** @param {VElement} node */
  53. "VElement[name='transition'] > VElement"(node) {
  54. if (node.parent.children[0] !== node) {
  55. return
  56. }
  57. verifyInsideElement(node)
  58. }
  59. })
  60. }
  61. }