Skip to content

Commit 2b2632b

Browse files
committed
fix: fixes #5
1 parent 0aac0c6 commit 2b2632b

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

vue-transformations/__test__/slot-attribute.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ runTest(
2323
'vue',
2424
'vue'
2525
)
26+
27+
runTest(
28+
'The v-slot attribute already exists in the upper template',
29+
'slot-attribute',
30+
'template-with-slot',
31+
'vue',
32+
'vue'
33+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<div class="img-list" ref="box">
3+
<template slot="title">
4+
<div>
5+
<span slot="title">title name</span>
6+
</div>
7+
</template>
8+
</div>
9+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<div class="img-list" ref="box">
3+
<template v-slot:title>
4+
<div>
5+
<span >title name</span>
6+
</div>
7+
</template>
8+
</div>
9+
</template>

vue-transformations/slot-attribute.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,44 @@ function fix(node: Node): Operation[] {
6060
// remove v-slot:${slotValue}
6161
fixOperations.push(OperationUtils.remove(node))
6262
// add <template v-slot:${slotValue}>
63-
fixOperations.push(
64-
OperationUtils.insertTextBefore(element, `<template v-slot:${slotValue}>`)
65-
)
66-
// add </template>
67-
fixOperations.push(OperationUtils.insertTextAfter(element, `</template>`))
63+
64+
let elder: any = null
65+
let hasSlotAttr: boolean = false
66+
let tmp: any = element
67+
// find template parent
68+
while (elder == null && tmp != null) {
69+
hasSlotAttr = false
70+
tmp = tmp.parent
71+
if (tmp == null || tmp.type != 'VElement' || tmp.name != 'template') {
72+
continue
73+
}
74+
75+
elder = element
76+
tmp.startTag.attributes
77+
.filter(
78+
(attr: any) =>
79+
attr.type === 'VAttribute' &&
80+
attr.key.type === 'VIdentifier' &&
81+
attr.key.name === 'slot'
82+
)
83+
.forEach((element: any) => {
84+
hasSlotAttr = true
85+
})
86+
if (hasSlotAttr) {
87+
break
88+
}
89+
}
90+
91+
if (!hasSlotAttr) {
92+
fixOperations.push(
93+
OperationUtils.insertTextBefore(
94+
element,
95+
`<template v-slot:${slotValue}>`
96+
)
97+
)
98+
// add </template>
99+
fixOperations.push(OperationUtils.insertTextAfter(element, `</template>`))
100+
}
68101
}
69102

70103
return fixOperations

0 commit comments

Comments
 (0)