no-restricted-html-elements.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author Doug Wade <douglas.b.wade@gmail.com>
  3. */
  4. 'use strict'
  5. const utils = require('../utils')
  6. module.exports = {
  7. meta: {
  8. type: 'suggestion',
  9. docs: {
  10. description: 'disallow specific HTML elements',
  11. categories: undefined,
  12. url: 'https://eslint.vuejs.org/rules/no-restricted-html-elements.html'
  13. },
  14. fixable: null,
  15. schema: {
  16. type: 'array',
  17. items: {
  18. oneOf: [
  19. { type: 'string' },
  20. {
  21. type: 'object',
  22. properties: {
  23. element: { type: 'string' },
  24. message: { type: 'string', minLength: 1 }
  25. },
  26. required: ['element'],
  27. additionalProperties: false
  28. }
  29. ]
  30. },
  31. uniqueItems: true,
  32. minItems: 0
  33. }
  34. },
  35. /**
  36. * @param {RuleContext} context - The rule context.
  37. * @returns {RuleListener} AST event handlers.
  38. */
  39. create(context) {
  40. return utils.defineTemplateBodyVisitor(context, {
  41. /**
  42. * @param {VElement} node
  43. */
  44. VElement(node) {
  45. if (!utils.isHtmlElementNode(node)) {
  46. return
  47. }
  48. context.options.forEach((option) => {
  49. const message =
  50. option.message ||
  51. `Unexpected use of forbidden HTML element ${node.rawName}.`
  52. const element = option.element || option
  53. if (element === node.rawName) {
  54. context.report({
  55. message,
  56. node: node.startTag
  57. })
  58. }
  59. })
  60. }
  61. })
  62. }
  63. }