Skip to content

Commit

Permalink
Add support for defineOptions to `vue/no-duplicate-attr-inheritance…
Browse files Browse the repository at this point in the history
…` rule (#2178)
  • Loading branch information
ota-meshi committed May 22, 2023
1 parent 8494cd5 commit 30931f0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/rules/no-duplicate-attr-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@ module.exports = {
/** @type {string | number | boolean | RegExp | BigInt | null} */
let inheritsAttrs = true

return Object.assign(
utils.executeOnVue(context, (node) => {
const inheritAttrsProp = utils.findProperty(node, 'inheritAttrs')
/** @param {ObjectExpression} node */
function processOptions(node) {
const inheritAttrsProp = utils.findProperty(node, 'inheritAttrs')

if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') {
inheritsAttrs = inheritAttrsProp.value.value
if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') {
inheritsAttrs = inheritAttrsProp.value.value
}
}

return utils.compositingVisitors(
utils.executeOnVue(context, processOptions),
utils.defineScriptSetupVisitor(context, {
onDefineOptionsEnter(node) {
if (node.arguments.length === 0) return
const define = node.arguments[0]
if (define.type !== 'ObjectExpression') return
processOptions(define)
}
}),
utils.defineTemplateBodyVisitor(context, {
Expand Down
52 changes: 52 additions & 0 deletions tests/lib/rules/no-duplicate-attr-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
export default { }
</script>
`
},
{
filename: 'test.vue',
code: `
<script>
export default { inheritAttrs: false }
</script>
<script setup>
</script>
<template><div v-bind="$attrs" /></template>
`
},
{
filename: 'test.vue',
code: `
<script setup>
defineOptions({ inheritAttrs: false })
</script>
<template><div v-bind="$attrs" /></template>
`
}
],

Expand All @@ -99,6 +119,38 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
</script>
`,
errors: ['Set "inheritAttrs" to false.']
},
{
filename: 'test.vue',
code: `
<script>
export default { inheritAttrs: true }
</script>
<script setup>
</script>
<template><div v-bind="$attrs" /></template>
`,
errors: [
{
message: 'Set "inheritAttrs" to false.',
line: 7
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
defineOptions({ inheritAttrs: true })
</script>
<template><div v-bind="$attrs" /></template>
`,
errors: [
{
message: 'Set "inheritAttrs" to false.',
line: 5
}
]
}
]
})

0 comments on commit 30931f0

Please sign in to comment.