‘liusuyi’
2023-10-21 94023628bd9c5e6bf724c37371a19b60d338b291
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * @author Yosuke Ota
 * See LICENSE file in root directory for full license.
 */
'use strict'
const { findVariable } = require('eslint-utils')
const utils = require('../utils')
 
module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'disallow destructuring of `props` passed to `setup`',
      categories: ['vue3-essential'],
      url: 'https://eslint.vuejs.org/rules/no-setup-props-destructure.html'
    },
    fixable: null,
    schema: [],
    messages: {
      destructuring:
        'Destructuring the `props` will cause the value to lose reactivity.',
      getProperty:
        'Getting a value from the `props` in root scope of `setup()` will cause the value to lose reactivity.'
    }
  },
  /** @param {RuleContext} context */
  create(context) {
    /** @type {Map<FunctionDeclaration | FunctionExpression | ArrowFunctionExpression, Set<Identifier>>} */
    const setupScopePropsReferenceIds = new Map()
 
    /**
     * @param {ESNode} node
     * @param {string} messageId
     */
    function report(node, messageId) {
      context.report({
        node,
        messageId
      })
    }
 
    /**
     * @param {Pattern} left
     * @param {Expression | null} right
     * @param {Set<Identifier>} propsReferenceIds
     */
    function verify(left, right, propsReferenceIds) {
      if (!right) {
        return
      }
 
      const rightNode = utils.skipChainExpression(right)
      if (
        left.type !== 'ArrayPattern' &&
        left.type !== 'ObjectPattern' &&
        rightNode.type !== 'MemberExpression'
      ) {
        return
      }
      /** @type {Expression | Super} */
      let rightId = rightNode
      while (rightId.type === 'MemberExpression') {
        rightId = utils.skipChainExpression(rightId.object)
      }
      if (rightId.type === 'Identifier' && propsReferenceIds.has(rightId)) {
        report(left, 'getProperty')
      }
    }
    /**
     * @typedef {object} ScopeStack
     * @property {ScopeStack | null} upper
     * @property {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} functionNode
     */
    /**
     * @type {ScopeStack | null}
     */
    let scopeStack = null
 
    return utils.defineVueVisitor(context, {
      ':function'(node) {
        scopeStack = {
          upper: scopeStack,
          functionNode: node
        }
      },
      onSetupFunctionEnter(node) {
        const propsParam = utils.skipDefaultParamValue(node.params[0])
        if (!propsParam) {
          // no arguments
          return
        }
        if (propsParam.type === 'RestElement') {
          // cannot check
          return
        }
        if (
          propsParam.type === 'ArrayPattern' ||
          propsParam.type === 'ObjectPattern'
        ) {
          report(propsParam, 'destructuring')
          return
        }
 
        const variable = findVariable(context.getScope(), propsParam)
        if (!variable) {
          return
        }
        const propsReferenceIds = new Set()
        for (const reference of variable.references) {
          if (!reference.isRead()) {
            continue
          }
 
          propsReferenceIds.add(reference.identifier)
        }
        setupScopePropsReferenceIds.set(node, propsReferenceIds)
      },
      VariableDeclarator(node) {
        if (!scopeStack) {
          return
        }
        const propsReferenceIds = setupScopePropsReferenceIds.get(
          scopeStack.functionNode
        )
        if (!propsReferenceIds) {
          return
        }
        verify(node.id, node.init, propsReferenceIds)
      },
      AssignmentExpression(node) {
        if (!scopeStack) {
          return
        }
        const propsReferenceIds = setupScopePropsReferenceIds.get(
          scopeStack.functionNode
        )
        if (!propsReferenceIds) {
          return
        }
        verify(node.left, node.right, propsReferenceIds)
      },
      ':function:exit'(node) {
        scopeStack = scopeStack && scopeStack.upper
 
        setupScopePropsReferenceIds.delete(node)
      }
    })
  }
}