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