prefer-use-template-ref.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author Thomasan1999
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. /**
  8. * @typedef ScriptRef
  9. * @type {{node: Expression, ref: string}}
  10. */
  11. /**
  12. * @param declarator {VariableDeclarator}
  13. * @returns {ScriptRef}
  14. * */
  15. function convertDeclaratorToScriptRef(declarator) {
  16. return {
  17. // @ts-ignore
  18. node: declarator.init,
  19. // @ts-ignore
  20. ref: declarator.id.name
  21. }
  22. }
  23. /**
  24. * @param body {(Statement | ModuleDeclaration)[]}
  25. * @returns {ScriptRef[]}
  26. * */
  27. function getScriptRefsFromSetupFunction(body) {
  28. /** @type {VariableDeclaration[]} */
  29. const variableDeclarations = body.filter(
  30. (child) => child.type === 'VariableDeclaration'
  31. )
  32. const variableDeclarators = variableDeclarations.map(
  33. (declaration) => declaration.declarations[0]
  34. )
  35. const refDeclarators = variableDeclarators.filter((declarator) =>
  36. // @ts-ignore
  37. ['ref', 'shallowRef'].includes(declarator.init?.callee?.name)
  38. )
  39. return refDeclarators.map(convertDeclaratorToScriptRef)
  40. }
  41. /** @type {import("eslint").Rule.RuleModule} */
  42. module.exports = {
  43. meta: {
  44. type: 'suggestion',
  45. docs: {
  46. description:
  47. 'require using `useTemplateRef` instead of `ref`/`shallowRef` for template refs',
  48. categories: undefined,
  49. url: 'https://eslint.vuejs.org/rules/prefer-use-template-ref.html'
  50. },
  51. schema: [],
  52. messages: {
  53. preferUseTemplateRef: "Replace '{{name}}' with 'useTemplateRef'."
  54. }
  55. },
  56. /** @param {RuleContext} context */
  57. create(context) {
  58. /** @type Set<string> */
  59. const templateRefs = new Set()
  60. /**
  61. * @type ScriptRef[] */
  62. const scriptRefs = []
  63. return utils.compositingVisitors(
  64. utils.defineTemplateBodyVisitor(context, {
  65. 'VAttribute[directive=false]'(node) {
  66. if (node.key.name === 'ref' && node.value?.value) {
  67. templateRefs.add(node.value.value)
  68. }
  69. }
  70. }),
  71. utils.defineVueVisitor(context, {
  72. onSetupFunctionEnter(node) {
  73. if (node.type === 'ArrowFunctionExpression' && node.expression) {
  74. return
  75. }
  76. const newScriptRefs = getScriptRefsFromSetupFunction(node.body.body)
  77. scriptRefs.push(...newScriptRefs)
  78. }
  79. }),
  80. utils.defineScriptSetupVisitor(context, {
  81. Program(node) {
  82. const newScriptRefs = getScriptRefsFromSetupFunction(node.body)
  83. scriptRefs.push(...newScriptRefs)
  84. }
  85. }),
  86. {
  87. 'Program:exit'() {
  88. for (const templateRef of templateRefs) {
  89. const scriptRef = scriptRefs.find(
  90. (scriptRef) => scriptRef.ref === templateRef
  91. )
  92. if (!scriptRef) {
  93. continue
  94. }
  95. context.report({
  96. node: scriptRef.node,
  97. messageId: 'preferUseTemplateRef',
  98. data: {
  99. // @ts-ignore
  100. name: scriptRef.node?.callee?.name
  101. }
  102. })
  103. }
  104. }
  105. }
  106. )
  107. }
  108. }