no-unused-vars.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Helpers
  12. // ------------------------------------------------------------------------------
  13. /**
  14. * Groups variables by directive kind.
  15. * @param {VElement} node The element node
  16. * @returns { { [kind in VariableKind]?: VVariable[] } } The variables of grouped by directive kind.
  17. */
  18. function groupingVariables(node) {
  19. /** @type { { [kind in VariableKind]?: VVariable[] } } */
  20. const result = {}
  21. for (const variable of node.variables) {
  22. const vars = result[variable.kind] || (result[variable.kind] = [])
  23. vars.push(variable)
  24. }
  25. return result
  26. }
  27. /**
  28. * Checks if the given variable was defined by destructuring.
  29. * @param {VVariable} variable the given variable to check
  30. * @returns {boolean} `true` if the given variable was defined by destructuring.
  31. */
  32. function isDestructuringVar(variable) {
  33. const node = variable.id
  34. /** @type {ASTNode | null} */
  35. let parent = node.parent
  36. while (parent) {
  37. if (
  38. parent.type === 'VForExpression' ||
  39. parent.type === 'VSlotScopeExpression'
  40. ) {
  41. return false
  42. }
  43. if (parent.type === 'Property' || parent.type === 'ArrayPattern') {
  44. return true
  45. }
  46. parent = parent.parent
  47. }
  48. return false
  49. }
  50. // ------------------------------------------------------------------------------
  51. // Rule Definition
  52. // ------------------------------------------------------------------------------
  53. module.exports = {
  54. meta: {
  55. type: 'suggestion',
  56. docs: {
  57. description:
  58. 'disallow unused variable definitions of v-for directives or scope attributes',
  59. categories: ['vue3-essential', 'essential'],
  60. url: 'https://eslint.vuejs.org/rules/no-unused-vars.html'
  61. },
  62. fixable: null,
  63. schema: [
  64. {
  65. type: 'object',
  66. properties: {
  67. ignorePattern: {
  68. type: 'string'
  69. }
  70. },
  71. additionalProperties: false
  72. }
  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) {
  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. message: `'{{name}}' is defined but never used.`,
  112. data: {
  113. name: variable.id.name
  114. },
  115. suggest:
  116. ignorePattern === '^_'
  117. ? [
  118. {
  119. desc: `Replace the ${variable.id.name} with _${variable.id.name}`,
  120. fix(fixer) {
  121. return fixer.replaceText(
  122. variable.id,
  123. `_${variable.id.name}`
  124. )
  125. }
  126. }
  127. ]
  128. : []
  129. })
  130. }
  131. }
  132. }
  133. })
  134. }
  135. }