no-duplicate-attributes.js 2.7 KB

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