From d9a0889845f2d3b36958802f0aaf969df0643cdd Mon Sep 17 00:00:00 2001 From: thesheppard Date: Sat, 3 Feb 2024 21:55:18 +0200 Subject: [PATCH 1/3] fix: false negative for setup props no reactivity loss --- lib/rules/no-setup-props-reactivity-loss.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-setup-props-reactivity-loss.js b/lib/rules/no-setup-props-reactivity-loss.js index fc0406ba9..b8e896075 100644 --- a/lib/rules/no-setup-props-reactivity-loss.js +++ b/lib/rules/no-setup-props-reactivity-loss.js @@ -78,7 +78,8 @@ module.exports = { if ( left.type !== 'ArrayPattern' && left.type !== 'ObjectPattern' && - rightNode.type !== 'MemberExpression' + rightNode.type !== 'MemberExpression' && + rightNode.type !== 'ConditionalExpression' ) { return } @@ -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) + } } /** From 6ced0166c48f5b208406c2ddb33b705852d4b456 Mon Sep 17 00:00:00 2001 From: thesheppard Date: Sat, 3 Feb 2024 21:56:01 +0200 Subject: [PATCH 2/3] docs: false negative for setup props no reactivity loss --- docs/rules/no-setup-props-reactivity-loss.md | 67 +++++++++++++++---- .../rules/no-setup-props-reactivity-loss.js | 37 ++++++++++ 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/docs/rules/no-setup-props-reactivity-loss.md b/docs/rules/no-setup-props-reactivity-loss.md index ade3ee62c..28f3aaf63 100644 --- a/docs/rules/no-setup-props-reactivity-loss.md +++ b/docs/rules/no-setup-props-reactivity-loss.md @@ -5,13 +5,14 @@ title: vue/no-setup-props-reactivity-loss description: disallow usages that lose the reactivity of `props` passed to `setup` since: v9.17.0 --- + # vue/no-setup-props-reactivity-loss > disallow usages that lose the reactivity of `props` passed to `setup` ## :book: Rule Details -This rule reports the destructuring or member expression of `props` passed to `setup` causing the value to lose reactivity. +This rule reports the destructuring, member expression or conditional expression of `props` passed to `setup` causing the value to lose reactivity. @@ -20,9 +21,12 @@ This rule reports the destructuring or member expression of `props` passed to `s export default { /* ✓ GOOD */ setup(props) { - watch(() => props.count, () => { - console.log(props.count) - }) + watch( + () => props.count, + () => { + console.log(props.count) + } + ) return () => { return h('div', props.count) @@ -43,9 +47,13 @@ Destructuring the `props` passed to `setup` will cause the value to lose reactiv export default { /* ✗ BAD */ setup({ count }) { - watch(() => count, () => { // not going to detect changes - console.log(count) - }) + watch( + () => count, + () => { + // not going to detect changes + console.log(count) + } + ) return () => { return h('div', count) // not going to update @@ -68,11 +76,14 @@ export default { /* ✗ BAD */ const { count } = props - watch(() => props.count, () => { - /* ✓ GOOD */ - const { count } = props - console.log(count) - }) + watch( + () => props.count, + () => { + /* ✓ GOOD */ + const { count } = props + console.log(count) + } + ) return () => { /* ✓ GOOD */ @@ -86,6 +97,38 @@ export default { +Also, using a conditonal expression in root scope of `setup()` should error, but ok inside callbacks or returned render functions: + + + +```vue + +``` + + + ## :wrench: Options Nothing. diff --git a/tests/lib/rules/no-setup-props-reactivity-loss.js b/tests/lib/rules/no-setup-props-reactivity-loss.js index 408ccb09e..6321ea573 100644 --- a/tests/lib/rules/no-setup-props-reactivity-loss.js +++ b/tests/lib/rules/no-setup-props-reactivity-loss.js @@ -113,6 +113,28 @@ tester.run('no-setup-props-reactivity-loss', rule, { ` }, + { + filename: 'test.vue', + code: ` + + ` + }, { filename: 'test.vue', code: ` @@ -680,6 +702,21 @@ tester.run('no-setup-props-reactivity-loss', rule, { line: 6 } ] + }, + { + filename: 'test.vue', + code: ` + + `, + errors: [ + { + messageId: 'getProperty', + line: 4 + } + ] } ] }) From 5d425c64659f530cae12492996a25d0f91756b03 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Mon, 5 Feb 2024 16:58:27 +0100 Subject: [PATCH 3/3] Discard changes to docs/rules/no-setup-props-reactivity-loss.md --- docs/rules/no-setup-props-reactivity-loss.md | 67 ++++---------------- 1 file changed, 12 insertions(+), 55 deletions(-) diff --git a/docs/rules/no-setup-props-reactivity-loss.md b/docs/rules/no-setup-props-reactivity-loss.md index 28f3aaf63..ade3ee62c 100644 --- a/docs/rules/no-setup-props-reactivity-loss.md +++ b/docs/rules/no-setup-props-reactivity-loss.md @@ -5,14 +5,13 @@ title: vue/no-setup-props-reactivity-loss description: disallow usages that lose the reactivity of `props` passed to `setup` since: v9.17.0 --- - # vue/no-setup-props-reactivity-loss > disallow usages that lose the reactivity of `props` passed to `setup` ## :book: Rule Details -This rule reports the destructuring, member expression or conditional expression of `props` passed to `setup` causing the value to lose reactivity. +This rule reports the destructuring or member expression of `props` passed to `setup` causing the value to lose reactivity. @@ -21,12 +20,9 @@ This rule reports the destructuring, member expression or conditional expression export default { /* ✓ GOOD */ setup(props) { - watch( - () => props.count, - () => { - console.log(props.count) - } - ) + watch(() => props.count, () => { + console.log(props.count) + }) return () => { return h('div', props.count) @@ -47,13 +43,9 @@ Destructuring the `props` passed to `setup` will cause the value to lose reactiv export default { /* ✗ BAD */ setup({ count }) { - watch( - () => count, - () => { - // not going to detect changes - console.log(count) - } - ) + watch(() => count, () => { // not going to detect changes + console.log(count) + }) return () => { return h('div', count) // not going to update @@ -76,51 +68,16 @@ export default { /* ✗ BAD */ const { count } = props - watch( - () => props.count, - () => { - /* ✓ GOOD */ - const { count } = props - console.log(count) - } - ) - - return () => { + watch(() => props.count, () => { /* ✓ GOOD */ const { count } = props - return h('div', count) - } - } -} - -``` - - - -Also, using a conditonal expression in root scope of `setup()` should error, but ok inside callbacks or returned render functions: - - - -```vue -