Skip to content

Commit ffb85b3

Browse files
authored
Fix false negatives for with withDefaults in vue/no-mutating-props rule. (#1537)
1 parent 2eafd04 commit ffb85b3

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Diff for: lib/rules/no-mutating-props.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,27 @@ module.exports = {
204204
object: node
205205
}
206206

207+
let target = node
207208
if (
208-
!node.parent ||
209-
node.parent.type !== 'VariableDeclarator' ||
210-
node.parent.init !== node
209+
target.parent &&
210+
target.parent.type === 'CallExpression' &&
211+
target.parent.arguments[0] === target &&
212+
target.parent.callee.type === 'Identifier' &&
213+
target.parent.callee.name === 'withDefaults'
214+
) {
215+
target = target.parent
216+
}
217+
218+
if (
219+
!target.parent ||
220+
target.parent.type !== 'VariableDeclarator' ||
221+
target.parent.init !== target
211222
) {
212223
return
213224
}
214225

215226
for (const { node: prop, path } of iteratePatternProperties(
216-
node.parent.id,
227+
target.parent.id,
217228
[]
218229
)) {
219230
verifyPropVariable(prop, path)

Diff for: tests/lib/rules/no-mutating-props.js

+20
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,26 @@ ruleTester.run('no-mutating-props', rule, {
784784
line: 6
785785
}
786786
]
787+
},
788+
{
789+
filename: 'test.vue',
790+
code: `
791+
<script setup lang="ts">
792+
const props = withDefaults(defineProps<Props>(), {
793+
msg: 'hello'
794+
})
795+
props.value++
796+
</script>
797+
`,
798+
parserOptions: {
799+
parser: require.resolve('@typescript-eslint/parser')
800+
},
801+
errors: [
802+
{
803+
message: 'Unexpected mutation of "value" prop.',
804+
line: 6
805+
}
806+
]
787807
}
788808
]
789809
})

0 commit comments

Comments
 (0)