Skip to content

Commit 2e0f6d5

Browse files
committed
refactor: reuse v-for parsing logic
1 parent 1dd6b6f commit 2e0f6d5

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

Diff for: src/compiler/parser/index.js

+25-17
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import he from 'he'
44
import { parseHTML } from './html-parser'
55
import { parseText } from './text-parser'
66
import { parseFilters } from './filter-parser'
7-
import { cached, no, camelize } from 'shared/util'
87
import { genAssignmentCode } from '../directives/model'
8+
import { extend, cached, no, camelize } from 'shared/util'
99
import { isIE, isEdge, isServerRendering } from 'core/util/env'
1010

1111
import {
@@ -23,7 +23,7 @@ export const onRE = /^@|^v-on:/
2323
export const dirRE = /^v-|^@|^:/
2424
export const forAliasRE = /(.*?)\s+(?:in|of)\s+(.*)/
2525
export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
26-
export const stripParensRE = /^\(|\)$/g
26+
const stripParensRE = /^\(|\)$/g
2727

2828
const argRE = /:(.*)$/
2929
export const bindRE = /^:|^v-bind:/
@@ -355,26 +355,34 @@ function processRef (el) {
355355
export function processFor (el: ASTElement) {
356356
let exp
357357
if ((exp = getAndRemoveAttr(el, 'v-for'))) {
358-
const inMatch = exp.match(forAliasRE)
359-
if (!inMatch) {
360-
process.env.NODE_ENV !== 'production' && warn(
358+
const res = parseFor(exp)
359+
if (res) {
360+
extend(el, res)
361+
} else if (process.env.NODE_ENV !== 'production') {
362+
warn(
361363
`Invalid v-for expression: ${exp}`
362364
)
363-
return
364365
}
365-
el.for = inMatch[2].trim()
366-
const alias = inMatch[1].trim().replace(stripParensRE, '')
367-
const iteratorMatch = alias.match(forIteratorRE)
368-
if (iteratorMatch) {
369-
el.alias = alias.replace(forIteratorRE, '')
370-
el.iterator1 = iteratorMatch[1].trim()
371-
if (iteratorMatch[2]) {
372-
el.iterator2 = iteratorMatch[2].trim()
373-
}
374-
} else {
375-
el.alias = alias
366+
}
367+
}
368+
369+
export function parseFor (exp: string): ?Object {
370+
const inMatch = exp.match(forAliasRE)
371+
if (!inMatch) return
372+
const res = {}
373+
res.for = inMatch[2].trim()
374+
const alias = inMatch[1].trim().replace(stripParensRE, '')
375+
const iteratorMatch = alias.match(forIteratorRE)
376+
if (iteratorMatch) {
377+
res.alias = alias.replace(forIteratorRE, '')
378+
res.iterator1 = iteratorMatch[1].trim()
379+
if (iteratorMatch[2]) {
380+
res.iterator2 = iteratorMatch[2].trim()
376381
}
382+
} else {
383+
res.alias = alias
377384
}
385+
return res
378386
}
379387

380388
function processIf (el) {
+21-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
/* @flow */
22

3-
import { forAliasRE, forIteratorRE, stripParensRE } from 'compiler/parser/index'
3+
import { parseFor } from 'compiler/parser/index'
44
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'
55

66
export function preTransformVFor (el: ASTElement, options: WeexCompilerOptions) {
77
const exp = getAndRemoveAttr(el, 'v-for')
88
if (!exp) {
99
return
1010
}
11-
const inMatch = exp.match(forAliasRE)
12-
if (inMatch) {
13-
const alias = inMatch[1].trim().replace(stripParensRE, '')
14-
const desc: Object = {
15-
'@expression': inMatch[2].trim(),
16-
'@alias': alias
17-
}
18-
const iteratorMatch = alias.match(forIteratorRE)
19-
if (iteratorMatch) {
20-
desc['@alias'] = alias.replace(forIteratorRE, '')
21-
if (iteratorMatch[2]) {
22-
desc['@key'] = iteratorMatch[1].trim()
23-
desc['@index'] = iteratorMatch[2].trim()
24-
} else {
25-
desc['@index'] = iteratorMatch[1].trim()
26-
}
11+
12+
const res = parseFor(exp)
13+
if (!res) {
14+
if (process.env.NODE_ENV !== 'production' && options.warn) {
15+
options.warn(`Invalid v-for expression: ${exp}`)
2716
}
28-
delete el.attrsMap['v-for']
29-
addRawAttr(el, '[[repeat]]', desc)
30-
} else if (process.env.NODE_ENV !== 'production' && options.warn) {
31-
options.warn(`Invalid v-for expression: ${exp}`)
17+
return
3218
}
19+
20+
const desc: Object = {
21+
'@expression': res.for,
22+
'@alias': res.alias
23+
}
24+
if (res.iterator2) {
25+
desc['@key'] = res.iterator1
26+
desc['@index'] = res.iterator2
27+
} else {
28+
desc['@index'] = res.iterator1
29+
}
30+
31+
delete el.attrsMap['v-for']
32+
addRawAttr(el, '[[repeat]]', desc)
3333
}

0 commit comments

Comments
 (0)