Skip to content

Commit 0fc9493

Browse files
committed
fix: fixes #2
1 parent 3e116ef commit 0fc9493

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

vue-transformations/__test__/v-for-template-key.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+
':key is not the attribute of v-for template child',
12+
'v-for-template-key',
13+
'without-v-for-template',
14+
'vue',
15+
'vue'
16+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div>
3+
<div>
4+
<switch-roles @change="handleRolesChange" />
5+
<div :key="key">
6+
<span v-permission="['button']">
7+
<el-button type="button">button</el-button>
8+
</span>
9+
</div>
10+
</div>
11+
</div>
12+
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div>
3+
<div>
4+
<switch-roles @change="handleRolesChange" />
5+
<div :key="key">
6+
<span v-permission="['button']">
7+
<el-button type="button">button</el-button>
8+
</span>
9+
</div>
10+
</div>
11+
</div>
12+
</template>

vue-transformations/v-for-template-key.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,31 @@ function fix(node: any): Operation[] {
6666
const target: any = node!.parent!.parent
6767

6868
// The current node has no attribute that is v-for
69-
let havaForBrother: boolean = false
69+
let hasForAttr: boolean = false
7070
target.startTag.attributes
7171
.filter(
7272
(attr: any) =>
7373
attr.type === 'VAttribute' &&
7474
attr.key.type === 'VDirectiveKey' &&
7575
attr.key.name.name === 'for'
7676
)
77-
.forEach((element: any) => {
78-
havaForBrother = true
77+
.forEach(() => {
78+
hasForAttr = true
7979
})
80-
if (havaForBrother) {
80+
if (hasForAttr) {
8181
return fixOperations
8282
}
8383

8484
let elder: any = null
85-
let elderHasKey: any = false
86-
let tmp: any = target.parent
85+
let elderHasKey: boolean = false
86+
let elderHasFor: boolean = false
87+
let tmp: any = target
8788
// find template parent
8889
while (elder == null && tmp != null) {
8990
elderHasKey = false
90-
if (tmp.type != 'VElement' || tmp.name != 'template') {
91-
tmp = tmp.parent
91+
elderHasFor = false
92+
tmp = tmp.parent
93+
if (tmp == null || tmp.type != 'VElement' || tmp.name != 'template') {
9294
continue
9395
}
9496

@@ -97,36 +99,42 @@ function fix(node: any): Operation[] {
9799
(attr: any) =>
98100
attr.type === 'VAttribute' &&
99101
attr.key.type === 'VDirectiveKey' &&
100-
attr.key.name.name === 'bind' &&
101-
attr.key.argument?.type === 'VIdentifier' &&
102-
attr.key.argument?.name === 'key'
102+
attr.key.name.name === 'for'
103103
)
104104
.forEach((element: any) => {
105-
elderHasKey = true
105+
elderHasFor = true
106+
elder = element
106107
})
107108

108-
if (elderHasKey) {
109-
break
110-
}
111-
112109
tmp.startTag.attributes
113110
.filter(
114111
(attr: any) =>
115112
attr.type === 'VAttribute' &&
116113
attr.key.type === 'VDirectiveKey' &&
117-
attr.key.name.name === 'for'
114+
attr.key.name.name === 'bind' &&
115+
attr.key.argument?.type === 'VIdentifier' &&
116+
attr.key.argument?.name === 'key'
118117
)
119-
.forEach((element: any) => {
120-
elder = element
118+
.forEach(() => {
119+
elderHasKey = true
121120
})
121+
122+
if (elderHasFor) {
123+
break
124+
}
125+
}
126+
127+
if (!elderHasFor) {
128+
return fixOperations
122129
}
123130

124131
let expression: string = getExpression(node.value)
125132

126133
fixOperations.push(OperationUtils.remove(node))
127134
if (
135+
!elderHasKey &&
128136
util.inspect(operatingParentElements).indexOf(util.inspect(elder.range)) ==
129-
-1
137+
-1
130138
) {
131139
operatingParentElements.push(elder.range)
132140
fixOperations.push(

0 commit comments

Comments
 (0)