no-duplicate-attributes.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /**
  9. * Get the name of the given attribute node.
  10. * @param {VAttribute | VDirective} attribute The attribute node to get.
  11. * @returns {string | null} The name of the attribute.
  12. */
  13. function getName(attribute) {
  14. if (!attribute.directive) {
  15. return attribute.key.name
  16. }
  17. if (attribute.key.name.name === 'bind') {
  18. return (
  19. (attribute.key.argument &&
  20. attribute.key.argument.type === 'VIdentifier' &&
  21. attribute.key.argument.name) ||
  22. null
  23. )
  24. }
  25. return null
  26. }
  27. module.exports = {
  28. meta: {
  29. type: 'problem',
  30. docs: {
  31. description: 'disallow duplication of attributes',
  32. categories: ['vue3-essential', 'vue2-essential'],
  33. url: 'https://eslint.vuejs.org/rules/no-duplicate-attributes.html'
  34. },
  35. fixable: null,
  36. schema: [
  37. {
  38. type: 'object',
  39. properties: {
  40. allowCoexistClass: {
  41. type: 'boolean'
  42. },
  43. allowCoexistStyle: {
  44. type: 'boolean'
  45. }
  46. },
  47. additionalProperties: false
  48. }
  49. ],
  50. messages: {
  51. duplicateAttribute: "Duplicate attribute '{{name}}'."
  52. }
  53. },
  54. /** @param {RuleContext} context */
  55. create(context) {
  56. const options = context.options[0] || {}
  57. const allowCoexistStyle = options.allowCoexistStyle !== false
  58. const allowCoexistClass = options.allowCoexistClass !== false
  59. /** @type {Set<string>} */
  60. const directiveNames = new Set()
  61. /** @type {Set<string>} */
  62. const attributeNames = new Set()
  63. /**
  64. * @param {string} name
  65. * @param {boolean} isDirective
  66. */
  67. function isDuplicate(name, isDirective) {
  68. if (
  69. (allowCoexistStyle && name === 'style') ||
  70. (allowCoexistClass && name === 'class')
  71. ) {
  72. return isDirective ? directiveNames.has(name) : attributeNames.has(name)
  73. }
  74. return directiveNames.has(name) || attributeNames.has(name)
  75. }
  76. return utils.defineTemplateBodyVisitor(context, {
  77. VStartTag() {
  78. directiveNames.clear()
  79. attributeNames.clear()
  80. },
  81. VAttribute(node) {
  82. const name = getName(node)
  83. if (name == null) {
  84. return
  85. }
  86. if (isDuplicate(name, node.directive)) {
  87. context.report({
  88. node,
  89. loc: node.loc,
  90. messageId: 'duplicateAttribute',
  91. data: { name }
  92. })
  93. }
  94. if (node.directive) {
  95. directiveNames.add(name)
  96. } else {
  97. attributeNames.add(name)
  98. }
  99. }
  100. })
  101. }
  102. }