Skip to content

Commit ccb133b

Browse files
committed
fix: close #1
1 parent 3912c53 commit ccb133b

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ runTest(
77
'vue',
88
'vue'
99
)
10+
runTest(
11+
'template element replace slot="xxx" to v-slot:xxx',
12+
'slot-attribute',
13+
'template-slot-attribute',
14+
'vue',
15+
'vue'
16+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<div class="hello">
3+
<base-layout>
4+
<template slot="title">title-content</template>
5+
</base-layout>
6+
</div>
7+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<div class="hello">
3+
<base-layout>
4+
<template v-slot:title>title-content</template>
5+
</base-layout>
6+
</div>
7+
</template>

vue-transformations/slot-attribute.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,28 @@ function findNodes(context: any): Node[] {
4444
*/
4545
function fix(node: Node): Operation[] {
4646
let fixOperations: Operation[] = []
47-
48-
const target: any = node!.parent!.parent
47+
const element: any = node!.parent!.parent
4948
// @ts-ignore
5049
const slotValue: string = node!.value!.value
5150

52-
// remove v-slot:${slotValue}
53-
fixOperations.push(OperationUtils.remove(node))
54-
// add <template v-slot:${slotValue}>
55-
fixOperations.push(
56-
OperationUtils.insertTextBefore(target, `<template v-slot:${slotValue}>`)
57-
)
58-
// add </template>
59-
fixOperations.push(OperationUtils.insertTextAfter(target, `</template>`))
51+
if (
52+
element != null &&
53+
element != undefined &&
54+
element.type == 'VElement' &&
55+
element.name == 'template'
56+
) {
57+
// template element replace slot="xxx" to v-slot:xxx
58+
fixOperations.push(OperationUtils.replaceText(node, `v-slot:${slotValue}`))
59+
} else {
60+
// remove v-slot:${slotValue}
61+
fixOperations.push(OperationUtils.remove(node))
62+
// 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>`))
68+
}
6069

6170
return fixOperations
6271
}

0 commit comments

Comments
 (0)