Skip to content

Commit

Permalink
fix(no-setup-props-reactivity-loss): false negative for conditional e…
Browse files Browse the repository at this point in the history
…xpressions (#2394)

Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
  • Loading branch information
thesheppard and FloEdelmann authored Feb 18, 2024
1 parent 7bdefb2 commit 16aba2c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rules/no-setup-props-reactivity-loss.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ module.exports = {
if (
left.type !== 'ArrayPattern' &&
left.type !== 'ObjectPattern' &&
rightNode.type !== 'MemberExpression'
rightNode.type !== 'MemberExpression' &&
rightNode.type !== 'ConditionalExpression'
) {
return
}
Expand All @@ -91,6 +92,14 @@ module.exports = {
if (rightId.type === 'Identifier' && propsReferences.refs.has(rightId)) {
report(left, 'getProperty', propsReferences.scopeName)
}
if (
rightId.type === 'ConditionalExpression' &&
(isPropsMemberAccessed(rightId.test, propsReferences) ||
isPropsMemberAccessed(rightId.consequent, propsReferences) ||
isPropsMemberAccessed(rightId.alternate, propsReferences))
) {
report(right, 'getProperty', propsReferences.scopeName)
}
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/no-setup-props-reactivity-loss.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ tester.run('no-setup-props-reactivity-loss', rule, {
</script>
`
},
{
filename: 'test.vue',
code: `
<script>
export default {
setup(props) {
watch(
() => props.count,
() => {
const test = props.count ? true : false
console.log(test)
}
)
return () => {
return h('div', props.count ? true : false)
}
}
}
</script>
`
},
{
filename: 'test.vue',
code: `
Expand Down Expand Up @@ -680,6 +702,21 @@ tester.run('no-setup-props-reactivity-loss', rule, {
line: 6
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
const props = defineProps({ count: Number })
const buildCounter = props.count ? 1 : undefined
</script>
`,
errors: [
{
messageId: 'getProperty',
line: 4
}
]
}
]
})

0 comments on commit 16aba2c

Please sign in to comment.