valid-v-show.js 1.8 KB

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