no-unused-vars.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @fileoverview disallow unused variable definitions of v-for directives or scope attributes.
  3. * @author 薛定谔的猫<hh_2013@foxmail.com>
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. /**
  8. * @typedef {VVariable['kind']} VariableKind
  9. */
  10. /**
  11. * Groups variables by directive kind.
  12. * @param {VElement} node The element node
  13. * @returns { { [kind in VariableKind]?: VVariable[] } } The variables of grouped by directive kind.
  14. */
  15. function groupingVariables(node) {
  16. /** @type { { [kind in VariableKind]?: VVariable[] } } */
  17. const result = {}
  18. for (const variable of node.variables) {
  19. const vars = result[variable.kind] || (result[variable.kind] = [])
  20. vars.push(variable)
  21. }
  22. return result
  23. }
  24. /**
  25. * Checks if the given variable was defined by destructuring.
  26. * @param {VVariable} variable the given variable to check
  27. * @returns {boolean} `true` if the given variable was defined by destructuring.
  28. */
  29. function isDestructuringVar(variable) {
  30. const node = variable.id
  31. /** @type {ASTNode | null} */
  32. let parent = node.parent
  33. while (parent) {
  34. if (
  35. parent.type === 'VForExpression' ||
  36. parent.type === 'VSlotScopeExpression'
  37. ) {
  38. return false
  39. }
  40. if (parent.type === 'Property' || parent.type === 'ArrayPattern') {
  41. return true
  42. }
  43. parent = parent.parent
  44. }
  45. return false
  46. }
  47. module.exports = {
  48. meta: {
  49. type: 'suggestion',
  50. docs: {
  51. description:
  52. 'disallow unused variable definitions of v-for directives or scope attributes',
  53. categories: ['vue3-essential', 'vue2-essential'],
  54. url: 'https://eslint.vuejs.org/rules/no-unused-vars.html'
  55. },
  56. fixable: null,
  57. hasSuggestions: true,
  58. schema: [
  59. {
  60. type: 'object',
  61. properties: {
  62. ignorePattern: {
  63. type: 'string'
  64. }
  65. },
  66. additionalProperties: false
  67. }
  68. ],
  69. messages: {
  70. unusedVariable: "'{{name}}' is defined but never used.",
  71. replaceWithUnderscore:
  72. 'Replace `{{name}}` with `_{{name}}` to ignore the unused variable.'
  73. }
  74. },
  75. /** @param {RuleContext} context */
  76. create(context) {
  77. const option = context.options[0] || {}
  78. const ignorePattern = option.ignorePattern
  79. /** @type {RegExp | null} */
  80. let ignoreRegEx = null
  81. if (ignorePattern) {
  82. ignoreRegEx = new RegExp(ignorePattern, 'u')
  83. }
  84. return utils.defineTemplateBodyVisitor(context, {
  85. /**
  86. * @param {VElement} node
  87. */
  88. VElement(node) {
  89. const vars = groupingVariables(node)
  90. for (const variables of Object.values(vars)) {
  91. if (!variables) {
  92. continue
  93. }
  94. let hasAfterUsed = false
  95. for (let i = variables.length - 1; i >= 0; i--) {
  96. const variable = variables[i]
  97. if (variable.references.length > 0) {
  98. hasAfterUsed = true
  99. continue
  100. }
  101. if (ignoreRegEx != null && ignoreRegEx.test(variable.id.name)) {
  102. continue
  103. }
  104. if (hasAfterUsed && !isDestructuringVar(variable)) {
  105. // If a variable after the variable is used, it will be skipped.
  106. continue
  107. }
  108. context.report({
  109. node: variable.id,
  110. loc: variable.id.loc,
  111. messageId: 'unusedVariable',
  112. data: {
  113. name: variable.id.name
  114. },
  115. suggest:
  116. ignorePattern === '^_'
  117. ? [
  118. {
  119. messageId: 'replaceWithUnderscore',
  120. data: { name: variable.id.name },
  121. fix(fixer) {
  122. return fixer.replaceText(
  123. variable.id,
  124. `_${variable.id.name}`
  125. )
  126. }
  127. }
  128. ]
  129. : []
  130. })
  131. }
  132. }
  133. }
  134. })
  135. }
  136. }