Skip to content

Commit

Permalink
fix: fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jun 18, 2021
1 parent 3e116ef commit 0fc9493
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
7 changes: 7 additions & 0 deletions vue-transformations/__test__/v-for-template-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ runTest(
'vue',
'vue'
)
runTest(
':key is not the attribute of v-for template child',
'v-for-template-key',
'without-v-for-template',
'vue',
'vue'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<div>
<div>
<switch-roles @change="handleRolesChange" />
<div :key="key">
<span v-permission="['button']">
<el-button type="button">button</el-button>
</span>
</div>
</div>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<div>
<div>
<switch-roles @change="handleRolesChange" />
<div :key="key">
<span v-permission="['button']">
<el-button type="button">button</el-button>
</span>
</div>
</div>
</div>
</template>
48 changes: 28 additions & 20 deletions vue-transformations/v-for-template-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,31 @@ function fix(node: any): Operation[] {
const target: any = node!.parent!.parent

// The current node has no attribute that is v-for
let havaForBrother: boolean = false
let hasForAttr: boolean = false
target.startTag.attributes
.filter(
(attr: any) =>
attr.type === 'VAttribute' &&
attr.key.type === 'VDirectiveKey' &&
attr.key.name.name === 'for'
)
.forEach((element: any) => {
havaForBrother = true
.forEach(() => {
hasForAttr = true
})
if (havaForBrother) {
if (hasForAttr) {
return fixOperations
}

let elder: any = null
let elderHasKey: any = false
let tmp: any = target.parent
let elderHasKey: boolean = false
let elderHasFor: boolean = false
let tmp: any = target
// find template parent
while (elder == null && tmp != null) {
elderHasKey = false
if (tmp.type != 'VElement' || tmp.name != 'template') {
tmp = tmp.parent
elderHasFor = false
tmp = tmp.parent
if (tmp == null || tmp.type != 'VElement' || tmp.name != 'template') {
continue
}

Expand All @@ -97,36 +99,42 @@ function fix(node: any): Operation[] {
(attr: any) =>
attr.type === 'VAttribute' &&
attr.key.type === 'VDirectiveKey' &&
attr.key.name.name === 'bind' &&
attr.key.argument?.type === 'VIdentifier' &&
attr.key.argument?.name === 'key'
attr.key.name.name === 'for'
)
.forEach((element: any) => {
elderHasKey = true
elderHasFor = true
elder = element
})

if (elderHasKey) {
break
}

tmp.startTag.attributes
.filter(
(attr: any) =>
attr.type === 'VAttribute' &&
attr.key.type === 'VDirectiveKey' &&
attr.key.name.name === 'for'
attr.key.name.name === 'bind' &&
attr.key.argument?.type === 'VIdentifier' &&
attr.key.argument?.name === 'key'
)
.forEach((element: any) => {
elder = element
.forEach(() => {
elderHasKey = true
})

if (elderHasFor) {
break
}
}

if (!elderHasFor) {
return fixOperations
}

let expression: string = getExpression(node.value)

fixOperations.push(OperationUtils.remove(node))
if (
!elderHasKey &&
util.inspect(operatingParentElements).indexOf(util.inspect(elder.range)) ==
-1
-1
) {
operatingParentElements.push(elder.range)
fixOperations.push(
Expand Down

0 comments on commit 0fc9493

Please sign in to comment.