script-setup-uses-vars.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const { getStyleVariablesContext } = require('../utils/style-variables')
  7. const utils = require('../utils')
  8. const casing = require('../utils/casing')
  9. /**
  10. * `casing.camelCase()` converts the beginning to lowercase,
  11. * but does not convert the case of the beginning character when converting with Vue3.
  12. * @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/shared/src/index.ts#L116
  13. * @param {string} str
  14. */
  15. function camelize(str) {
  16. return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
  17. }
  18. module.exports = {
  19. meta: {
  20. type: 'problem',
  21. docs: {
  22. description:
  23. 'prevent `<script setup>` variables used in `<template>` to be marked as unused', // eslint-disable-line eslint-plugin/require-meta-docs-description
  24. categories: undefined,
  25. url: 'https://eslint.vuejs.org/rules/script-setup-uses-vars.html'
  26. },
  27. deprecated: true,
  28. schema: []
  29. },
  30. /**
  31. * @param {RuleContext} context - The rule context.
  32. * @returns {RuleListener} AST event handlers.
  33. */
  34. create(context) {
  35. if (!utils.isScriptSetup(context)) {
  36. return {}
  37. }
  38. /** @type {Set<string>} */
  39. const scriptVariableNames = new Set()
  40. const globalScope = context.getSourceCode().scopeManager.globalScope
  41. if (globalScope) {
  42. for (const variable of globalScope.variables) {
  43. scriptVariableNames.add(variable.name)
  44. }
  45. const moduleScope = globalScope.childScopes.find(
  46. (scope) => scope.type === 'module'
  47. )
  48. for (const variable of (moduleScope && moduleScope.variables) || []) {
  49. scriptVariableNames.add(variable.name)
  50. }
  51. }
  52. /**
  53. * @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L333
  54. * @param {string} name
  55. */
  56. function markSetupReferenceVariableAsUsed(name) {
  57. if (scriptVariableNames.has(name)) {
  58. context.markVariableAsUsed(name)
  59. return true
  60. }
  61. const camelName = camelize(name)
  62. if (scriptVariableNames.has(camelName)) {
  63. context.markVariableAsUsed(camelName)
  64. return true
  65. }
  66. const pascalName = casing.capitalize(camelName)
  67. if (scriptVariableNames.has(pascalName)) {
  68. context.markVariableAsUsed(pascalName)
  69. return true
  70. }
  71. return false
  72. }
  73. return utils.defineTemplateBodyVisitor(
  74. context,
  75. {
  76. VExpressionContainer(node) {
  77. for (const ref of node.references.filter(
  78. (ref) => ref.variable == null
  79. )) {
  80. context.markVariableAsUsed(ref.id.name)
  81. }
  82. },
  83. VElement(node) {
  84. if (
  85. (!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
  86. (node.rawName === node.name &&
  87. (utils.isHtmlWellKnownElementName(node.rawName) ||
  88. utils.isSvgWellKnownElementName(node.rawName))) ||
  89. utils.isBuiltInComponentName(node.rawName)
  90. ) {
  91. return
  92. }
  93. if (!markSetupReferenceVariableAsUsed(node.rawName)) {
  94. // Check namespace
  95. // https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L304
  96. const dotIndex = node.rawName.indexOf('.')
  97. if (dotIndex > 0) {
  98. markSetupReferenceVariableAsUsed(node.rawName.slice(0, dotIndex))
  99. }
  100. }
  101. },
  102. /** @param {VDirective} node */
  103. 'VAttribute[directive=true]'(node) {
  104. if (utils.isBuiltInDirectiveName(node.key.name.name)) {
  105. return
  106. }
  107. markSetupReferenceVariableAsUsed(`v-${node.key.name.rawName}`)
  108. },
  109. /** @param {VAttribute} node */
  110. 'VAttribute[directive=false]'(node) {
  111. if (node.key.name === 'ref' && node.value) {
  112. context.markVariableAsUsed(node.value.value)
  113. }
  114. }
  115. },
  116. {
  117. Program() {
  118. const styleVars = getStyleVariablesContext(context)
  119. if (styleVars) {
  120. for (const ref of styleVars.references) {
  121. context.markVariableAsUsed(ref.id.name)
  122. }
  123. }
  124. }
  125. },
  126. {
  127. templateBodyTriggerSelector: 'Program'
  128. }
  129. )
  130. }
  131. }