valid-v-show.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 `v-show` directives',
  19. categories: ['vue3-essential', 'essential'],
  20. url: 'https://eslint.vuejs.org/rules/valid-v-show.html'
  21. },
  22. fixable: null,
  23. schema: [],
  24. messages: {
  25. unexpectedArgument: "'v-show' directives require no argument.",
  26. unexpectedModifier: "'v-show' directives require no modifier.",
  27. expectedValue: "'v-show' directives require that attribute value.",
  28. unexpectedTemplate:
  29. "'v-show' directives cannot be put on <template> tags."
  30. }
  31. },
  32. /** @param {RuleContext} context */
  33. create(context) {
  34. return utils.defineTemplateBodyVisitor(context, {
  35. /** @param {VDirective} node */
  36. "VAttribute[directive=true][key.name.name='show']"(node) {
  37. if (node.key.argument) {
  38. context.report({
  39. node: node.key.argument,
  40. messageId: 'unexpectedArgument'
  41. })
  42. }
  43. if (node.key.modifiers.length > 0) {
  44. context.report({
  45. node,
  46. loc: {
  47. start: node.key.modifiers[0].loc.start,
  48. end: node.key.modifiers[node.key.modifiers.length - 1].loc.end
  49. },
  50. messageId: 'unexpectedModifier'
  51. })
  52. }
  53. if (!node.value || utils.isEmptyValueDirective(node, context)) {
  54. context.report({
  55. node,
  56. messageId: 'expectedValue'
  57. })
  58. }
  59. if (node.parent.parent.name === 'template') {
  60. context.report({
  61. node,
  62. messageId: 'unexpectedTemplate'
  63. })
  64. }
  65. }
  66. })
  67. }
  68. }