Skip to content

Commit db8717f

Browse files
committed
Improve the vue/no-setup-props-destructure rule
1 parent 6dad5c2 commit db8717f

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

lib/rules/no-setup-props-destructure.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ module.exports = {
2323
'Getting a value from the `props` in root scope of `{{scopeName}}` will cause the value to lose reactivity.'
2424
}
2525
},
26-
/** @param {RuleContext} context */
26+
/**
27+
* @param {RuleContext} context
28+
* @returns {RuleListener}
29+
**/
2730
create(context) {
2831
/**
2932
* @typedef {object} ScopePropsReferences
@@ -59,13 +62,38 @@ module.exports = {
5962
}
6063

6164
const rightNode = utils.skipChainExpression(right)
65+
66+
if (rightNode.type === 'CallExpression') {
67+
const propRefs = [...propsReferences.refs.values()]
68+
const isPropsMemberAccessed = propRefs.some((props) => {
69+
const isPropsInCallExpression = utils.inRange(rightNode.range, props)
70+
71+
if (isPropsInCallExpression) {
72+
const isPropMemberExpression =
73+
props.parent.type === 'MemberExpression' &&
74+
props.parent.object === props
75+
76+
if (isPropMemberExpression) {
77+
return true
78+
}
79+
}
80+
81+
return false
82+
})
83+
84+
if (isPropsMemberAccessed) {
85+
return report(left, 'getProperty', propsReferences.scopeName)
86+
}
87+
}
88+
6289
if (
6390
left.type !== 'ArrayPattern' &&
6491
left.type !== 'ObjectPattern' &&
6592
rightNode.type !== 'MemberExpression'
6693
) {
6794
return
6895
}
96+
6997
/** @type {Expression | Super} */
7098
let rightId = rightNode
7199
while (rightId.type === 'MemberExpression') {
@@ -114,6 +142,11 @@ module.exports = {
114142
}
115143
const propsReferenceIds = new Set()
116144
for (const reference of variable.references) {
145+
// If reference is in another scope, we can't check it.
146+
if (reference.from !== context.getScope()) {
147+
continue
148+
}
149+
117150
if (!reference.isRead()) {
118151
continue
119152
}

tests/lib/rules/no-setup-props-destructure.js

+76
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ tester.run('no-setup-props-destructure', rule, {
204204
line: 4
205205
}
206206
]
207+
},
208+
{
209+
filename: 'test.vue',
210+
code: `
211+
<script>
212+
export default {
213+
setup: (props) => {
214+
const count = computed(() => props.count)
215+
}
216+
}
217+
</script>
218+
`
219+
},
220+
{
221+
filename: 'test.vue',
222+
code: `
223+
<script setup>
224+
const props = defineProps({ count: Number })
225+
const count = computed(() => props.count)
226+
</script>
227+
`
207228
}
208229
],
209230
invalid: [
@@ -410,6 +431,18 @@ tester.run('no-setup-props-destructure', rule, {
410431
{
411432
messageId: 'getProperty',
412433
line: 7
434+
},
435+
{
436+
messageId: 'getProperty',
437+
line: 9
438+
},
439+
{
440+
messageId: 'getProperty',
441+
line: 10
442+
},
443+
{
444+
messageId: 'getProperty',
445+
line: 11
413446
}
414447
]
415448
},
@@ -524,6 +557,49 @@ tester.run('no-setup-props-destructure', rule, {
524557
line: 5
525558
}
526559
]
560+
},
561+
{
562+
filename: 'test.vue',
563+
code: `
564+
<script>
565+
export default {
566+
setup: (props) => {
567+
const count = ref(props.count)
568+
count = fn(props.count)
569+
}
570+
}
571+
</script>
572+
`,
573+
errors: [
574+
{
575+
messageId: 'getProperty',
576+
line: 5
577+
},
578+
{
579+
messageId: 'getProperty',
580+
line: 6
581+
}
582+
]
583+
},
584+
{
585+
filename: 'test.vue',
586+
code: `
587+
<script setup>
588+
const props = defineProps({ count: Number })
589+
const count = ref(props.count)
590+
count = fn(props.count)
591+
</script>
592+
`,
593+
errors: [
594+
{
595+
messageId: 'getProperty',
596+
line: 4
597+
},
598+
{
599+
messageId: 'getProperty',
600+
line: 5
601+
}
602+
]
527603
}
528604
]
529605
})

0 commit comments

Comments
 (0)