require-toggle-inside-transition.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: 'https://eslint.vuejs.org/rules/require-toggle-inside-transition.html'
  21. },
  22. fixable: null,
  23. schema: [],
  24. messages: {
  25. expected:
  26. 'The element inside `<transition>` is expected to have a `v-if` or `v-show` directive.'
  27. }
  28. },
  29. /** @param {RuleContext} context */
  30. create(context) {
  31. /**
  32. * Check if the given element has display control.
  33. * @param {VElement} element The element node to check.
  34. */
  35. function verifyInsideElement(element) {
  36. if (utils.isCustomComponent(element)) {
  37. return
  38. }
  39. if (
  40. !utils.hasDirective(element, 'if') &&
  41. !utils.hasDirective(element, 'show')
  42. ) {
  43. context.report({
  44. node: element.startTag,
  45. loc: element.startTag.loc,
  46. messageId: 'expected'
  47. })
  48. }
  49. }
  50. return utils.defineTemplateBodyVisitor(context, {
  51. /** @param {VElement} node */
  52. "VElement[name='transition'] > VElement"(node) {
  53. if (node.parent.children[0] !== node) {
  54. return
  55. }
  56. verifyInsideElement(node)
  57. }
  58. })
  59. }
  60. }