script-setup-uses-vars.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // eslint-disable-next-line eslint-plugin/prefer-message-ids
  20. meta: {
  21. type: 'problem',
  22. docs: {
  23. description:
  24. 'prevent `<script setup>` variables used in `<template>` to be marked as unused', // eslint-disable-line eslint-plugin/require-meta-docs-description
  25. categories: undefined,
  26. url: 'https://eslint.vuejs.org/rules/script-setup-uses-vars.html'
  27. },
  28. schema: [],
  29. deprecated: true
  30. },
  31. /**
  32. * @param {RuleContext} context - The rule context.
  33. * @returns {RuleListener} AST event handlers.
  34. */
  35. create(context) {
  36. if (!utils.isScriptSetup(context)) {
  37. return {}
  38. }
  39. /** @type {Set<string>} */
  40. const scriptVariableNames = new Set()
  41. const globalScope = context.getSourceCode().scopeManager.globalScope
  42. if (globalScope) {
  43. for (const variable of globalScope.variables) {
  44. scriptVariableNames.add(variable.name)
  45. }
  46. const moduleScope = globalScope.childScopes.find(
  47. (scope) => scope.type === 'module'
  48. )
  49. for (const variable of (moduleScope && moduleScope.variables) || []) {
  50. scriptVariableNames.add(variable.name)
  51. }
  52. }
  53. /**
  54. * @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L333
  55. * @param {string} name
  56. */
  57. function markSetupReferenceVariableAsUsed(name) {
  58. if (scriptVariableNames.has(name)) {
  59. context.markVariableAsUsed(name)
  60. return true
  61. }
  62. const camelName = camelize(name)
  63. if (scriptVariableNames.has(camelName)) {
  64. context.markVariableAsUsed(camelName)
  65. return true
  66. }
  67. const pascalName = casing.capitalize(camelName)
  68. if (scriptVariableNames.has(pascalName)) {
  69. context.markVariableAsUsed(pascalName)
  70. return true
  71. }
  72. return false
  73. }
  74. return utils.defineTemplateBodyVisitor(
  75. context,
  76. {
  77. VExpressionContainer(node) {
  78. for (const ref of node.references.filter(
  79. (ref) => ref.variable == null
  80. )) {
  81. context.markVariableAsUsed(ref.id.name)
  82. }
  83. },
  84. VElement(node) {
  85. if (
  86. (!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
  87. (node.rawName === node.name &&
  88. (utils.isHtmlWellKnownElementName(node.rawName) ||
  89. utils.isSvgWellKnownElementName(node.rawName))) ||
  90. utils.isBuiltInComponentName(node.rawName)
  91. ) {
  92. return
  93. }
  94. if (!markSetupReferenceVariableAsUsed(node.rawName)) {
  95. // Check namespace
  96. // https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L304
  97. const dotIndex = node.rawName.indexOf('.')
  98. if (dotIndex > 0) {
  99. markSetupReferenceVariableAsUsed(node.rawName.slice(0, dotIndex))
  100. }
  101. }
  102. },
  103. /** @param {VDirective} node */
  104. 'VAttribute[directive=true]'(node) {
  105. if (utils.isBuiltInDirectiveName(node.key.name.name)) {
  106. return
  107. }
  108. markSetupReferenceVariableAsUsed(`v-${node.key.name.rawName}`)
  109. },
  110. /** @param {VAttribute} node */
  111. 'VAttribute[directive=false]'(node) {
  112. if (node.key.name === 'ref' && node.value) {
  113. context.markVariableAsUsed(node.value.value)
  114. }
  115. }
  116. },
  117. {
  118. Program() {
  119. const styleVars = getStyleVariablesContext(context)
  120. if (styleVars) {
  121. for (const ref of styleVars.references) {
  122. context.markVariableAsUsed(ref.id.name)
  123. }
  124. }
  125. }
  126. },
  127. {
  128. templateBodyTriggerSelector: 'Program'
  129. }
  130. )
  131. }
  132. }