Skip to content

Commit 6f24c45

Browse files
committed
fix(no-deprecated-slot-attribute): handle v-for with dynamic slot
1 parent 705d262 commit 6f24c45

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/rules/syntaxes/slot-attribute.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ module.exports = {
5656
// parse error or empty expression
5757
return false
5858
}
59-
const slotName = sourceCode.getText(slotAttr.value.expression).trim()
60-
// If non-Latin characters are included it can not be converted.
61-
// It does not check the space only because `a>b?c:d` should be rejected.
62-
return !/[^a-z]/i.test(slotName)
59+
60+
return slotAttr.value.expression.type === 'Identifier'
6361
}
6462

6563
/**
@@ -102,7 +100,18 @@ module.exports = {
102100
yield fixer.remove(scopeAttr)
103101
}
104102

105-
yield fixer.insertTextBefore(element, `<template ${replaceText}>\n`)
103+
const vFor = startTag.attributes.find(
104+
(attr) => attr.directive && attr.key.name.name === 'for'
105+
)
106+
const vForText = vFor ? `${sourceCode.getText(vFor)} ` : ''
107+
if (vFor) {
108+
yield fixer.remove(vFor)
109+
}
110+
111+
yield fixer.insertTextBefore(
112+
element,
113+
`<template ${vForText}${replaceText}>\n`
114+
)
106115
yield fixer.insertTextAfter(element, `\n</template>`)
107116
}
108117
}

tests/lib/rules/no-deprecated-slot-attribute.js

+25
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,31 @@ tester.run('no-deprecated-slot-attribute', rule, {
643643
}
644644
],
645645
errors: ['`slot` attributes are deprecated.']
646+
},
647+
{
648+
code: `
649+
<template>
650+
<my-component>
651+
<slot
652+
v-for="slot in Object.keys($slots)"
653+
:slot="slot"
654+
:name="slot"
655+
></slot>
656+
</my-component>
657+
</template>`,
658+
output: `
659+
<template>
660+
<my-component>
661+
<template v-for="slot in Object.keys($slots)" v-slot:[slot]>
662+
<slot
663+
664+
665+
:name="slot"
666+
></slot>
667+
</template>
668+
</my-component>
669+
</template>`,
670+
errors: ['`slot` attributes are deprecated.']
646671
}
647672
]
648673
})

0 commit comments

Comments
 (0)