Skip to content

Commit 16aba2c

Browse files
fix(no-setup-props-reactivity-loss): false negative for conditional expressions (#2394)
Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
1 parent 7bdefb2 commit 16aba2c

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Diff for: lib/rules/no-setup-props-reactivity-loss.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ module.exports = {
7878
if (
7979
left.type !== 'ArrayPattern' &&
8080
left.type !== 'ObjectPattern' &&
81-
rightNode.type !== 'MemberExpression'
81+
rightNode.type !== 'MemberExpression' &&
82+
rightNode.type !== 'ConditionalExpression'
8283
) {
8384
return
8485
}
@@ -91,6 +92,14 @@ module.exports = {
9192
if (rightId.type === 'Identifier' && propsReferences.refs.has(rightId)) {
9293
report(left, 'getProperty', propsReferences.scopeName)
9394
}
95+
if (
96+
rightId.type === 'ConditionalExpression' &&
97+
(isPropsMemberAccessed(rightId.test, propsReferences) ||
98+
isPropsMemberAccessed(rightId.consequent, propsReferences) ||
99+
isPropsMemberAccessed(rightId.alternate, propsReferences))
100+
) {
101+
report(right, 'getProperty', propsReferences.scopeName)
102+
}
94103
}
95104

96105
/**

Diff for: tests/lib/rules/no-setup-props-reactivity-loss.js

+37
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ tester.run('no-setup-props-reactivity-loss', rule, {
113113
</script>
114114
`
115115
},
116+
{
117+
filename: 'test.vue',
118+
code: `
119+
<script>
120+
export default {
121+
setup(props) {
122+
watch(
123+
() => props.count,
124+
() => {
125+
const test = props.count ? true : false
126+
console.log(test)
127+
}
128+
)
129+
130+
return () => {
131+
return h('div', props.count ? true : false)
132+
}
133+
}
134+
}
135+
</script>
136+
`
137+
},
116138
{
117139
filename: 'test.vue',
118140
code: `
@@ -680,6 +702,21 @@ tester.run('no-setup-props-reactivity-loss', rule, {
680702
line: 6
681703
}
682704
]
705+
},
706+
{
707+
filename: 'test.vue',
708+
code: `
709+
<script setup>
710+
const props = defineProps({ count: Number })
711+
const buildCounter = props.count ? 1 : undefined
712+
</script>
713+
`,
714+
errors: [
715+
{
716+
messageId: 'getProperty',
717+
line: 4
718+
}
719+
]
683720
}
684721
]
685722
})

0 commit comments

Comments
 (0)