no-unused-vars.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: 'disallow unused variable definitions of v-for directives or scope attributes',
  14. category: 'essential',
  15. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/no-unused-vars.md'
  16. },
  17. fixable: null,
  18. schema: []
  19. },
  20. create (context) {
  21. return utils.defineTemplateBodyVisitor(context, {
  22. VElement (node) {
  23. const variables = node.variables
  24. for (
  25. let i = variables.length - 1;
  26. i >= 0 && !variables[i].references.length;
  27. i--
  28. ) {
  29. const variable = variables[i]
  30. context.report({
  31. node: variable.id,
  32. loc: variable.id.loc,
  33. message: `'{{name}}' is defined but never used.`,
  34. data: variable.id
  35. })
  36. }
  37. }
  38. })
  39. }
  40. }