valid-v-html.js 1.6 KB

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