Skip to content

Commit 2a1ce0d

Browse files
Hanks10100yyx990803
authored andcommitted
feat($compiler): supports compiling v-else-if and v-else to the weex native directive
1 parent 2d09ee3 commit 2a1ce0d

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/platforms/weex/compiler/modules/recycle-list/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { transformVFor } from './v-for'
77

88
let currentRecycleList = null
99

10-
function preTransformNode (el: ASTElement) {
10+
function preTransformNode (el: ASTElement, options: CompilerOptions) {
1111
if (el.tag === 'recycle-list') {
1212
currentRecycleList = el
1313
}
1414
if (currentRecycleList) {
1515
// TODO
1616
transformVBind(el)
17-
transformVIf(el)
18-
transformVFor(el)
17+
transformVIf(el, options) // and v-else-if and v-else
18+
transformVFor(el, options)
1919
}
2020
}
2121

src/platforms/weex/compiler/modules/recycle-list/v-for.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { forAliasRE, forIteratorRE } from 'compiler/parser/index'
44
import { getAndRemoveAttr } from 'compiler/helpers'
55

6-
export function transformVFor (el: ASTElement) {
6+
export function transformVFor (el: ASTElement, options: CompilerOptions) {
77
const exp = getAndRemoveAttr(el, 'v-for')
88
if (!exp) {
99
return
@@ -27,5 +27,7 @@ export function transformVFor (el: ASTElement) {
2727
delete el.attrsMap['v-for']
2828
el.attrsMap['[[repeat]]'] = desc
2929
el.attrsList.push({ name: '[[repeat]]', value: desc })
30+
} else if (process.env.NODE_ENV !== 'production' && options.warn) {
31+
options.warn(`Invalid v-for expression: ${exp}`)
3032
}
3133
}

src/platforms/weex/compiler/modules/recycle-list/v-if.js

+39-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,47 @@
22

33
import { getAndRemoveAttr } from 'compiler/helpers'
44

5-
export function transformVIf (el: ASTElement) {
6-
const exp = getAndRemoveAttr(el, 'v-if')
7-
if (exp) {
5+
function hasConditionDirective (el: ASTElement): boolean {
6+
for (const attr in el.attrsMap) {
7+
if (/^v\-if|v\-else|v\-else\-if$/.test(attr)) {
8+
return true
9+
}
10+
}
11+
return false
12+
}
13+
14+
function getPrevMatch (el: ASTElement): any {
15+
if (el.parent && el.parent.children) {
16+
const prev: Object = el.parent.children[el.parent.children.length - 1]
17+
return prev.attrsMap['[[match]]']
18+
}
19+
}
20+
21+
export function transformVIf (el: ASTElement, options: CompilerOptions) {
22+
if (hasConditionDirective(el)) {
23+
let exp
24+
const ifExp = getAndRemoveAttr(el, 'v-if')
25+
const elseifExp = getAndRemoveAttr(el, 'v-else-if')
26+
if (ifExp) {
27+
exp = ifExp
28+
} else {
29+
const prevMatch = getPrevMatch(el)
30+
if (prevMatch) {
31+
exp = elseifExp
32+
? `!(${prevMatch}) && (${elseifExp})` // v-else-if
33+
: `!(${prevMatch})` // v-else
34+
} else if (process.env.NODE_ENV !== 'production' && options.warn) {
35+
options.warn(
36+
`v-${elseifExp ? ('else-if="' + elseifExp + '"') : 'else'} ` +
37+
`used on element <${el.tag}> without corresponding v-if.`
38+
)
39+
return
40+
}
41+
}
842
el.attrsMap['[[match]]'] = exp
943
el.attrsList.push({ name: '[[match]]', value: exp })
1044
delete el.attrsMap['v-if']
45+
delete el.attrsMap['v-else-if']
46+
delete el.attrsMap['v-else']
1147
}
12-
// TODO: support v-else and v-else-if
1348
}

0 commit comments

Comments
 (0)