Skip to content

Fix false negatives for with withDefaults in vue/no-mutating-props rule. #1537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/rules/no-mutating-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,27 @@ module.exports = {
object: node
}

let target = node
if (
!node.parent ||
node.parent.type !== 'VariableDeclarator' ||
node.parent.init !== node
target.parent &&
target.parent.type === 'CallExpression' &&
target.parent.arguments[0] === target &&
target.parent.callee.type === 'Identifier' &&
target.parent.callee.name === 'withDefaults'
) {
target = target.parent
}

if (
!target.parent ||
target.parent.type !== 'VariableDeclarator' ||
target.parent.init !== target
) {
return
}

for (const { node: prop, path } of iteratePatternProperties(
node.parent.id,
target.parent.id,
[]
)) {
verifyPropVariable(prop, path)
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/no-mutating-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,26 @@ ruleTester.run('no-mutating-props', rule, {
line: 6
}
]
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = withDefaults(defineProps<Props>(), {
msg: 'hello'
})
props.value++
</script>
`,
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
},
errors: [
{
message: 'Unexpected mutation of "value" prop.',
line: 6
}
]
}
]
})