提交 af70313e 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

wip(uts): v-for

上级 d455d602
......@@ -4,19 +4,19 @@ describe('compiler:v-for', () => {
test('template v-for', () => {
assert(
`<view v-for="item in 10" :key="item">text</view>`,
`createElementVNode(Fragment, null, renderList(10, (item, _key_, _index_):VNode => {
`createElementVNode(Fragment, null, RenderHelpers.renderList(10, (item, _key_, _index_):VNode => {
return createElementVNode("view", new Map<string,any>([["key", item]]), "text")
}), 64 /* STABLE_FRAGMENT */)`
)
assert(
`<view v-for="(item, index) in [1,2,3]" :key="index">text</view>`,
`createElementVNode(Fragment, null, renderList([1,2,3], (item, index, _index_):VNode => {
`createElementVNode(Fragment, null, RenderHelpers.renderList([1,2,3], (item, index, _index_):VNode => {
return createElementVNode("view", new Map<string,any>([["key", index]]), "text")
}), 64 /* STABLE_FRAGMENT */)`
)
assert(
`<view v-for="(item, key, index) in {a:'a',b:'b'}" :key="index">text</view>`,
`createElementVNode(Fragment, null, renderList({a:'a',b:'b'}, (item, key, index):VNode => {
`createElementVNode(Fragment, null, RenderHelpers.renderList({a:'a',b:'b'}, (item, key, index):VNode => {
return createElementVNode("view", new Map<string,any>([["key", index]]), "text")
}), 64 /* STABLE_FRAGMENT */)`
)
......
......@@ -394,23 +394,10 @@ function genCallExpression(node: CallExpression, context: CodegenContext) {
const callee = isString(node.callee) ? node.callee : helper(node.callee)
push(callee + `(`, node)
if (callee === 'renderList') {
if (callee === 'RenderHelpers.renderList') {
node.arguments.forEach((item: any) => {
if (item.type === 18) {
item.returnType = 'VNode'
if (item.params.length === 1) {
const key = { ...item.params[0] }
key.content = '_key_'
item.params.push(key)
const index = { ...item.params[0] }
index.content = '_index_'
item.params.push(index)
} else if (item.params.length === 2) {
const index = { ...item.params[0] }
index.content = '_index_'
item.params.push(index)
}
}
})
}
......
......@@ -8,6 +8,6 @@ export const OPEN_BLOCK = Symbol(`openBlock`)
registerRuntimeHelpers({
[IS_TRUE]: 'isTrue',
[V_SHOW]: 'vShow',
[RENDER_LIST]: 'renderList',
[RENDER_LIST]: 'RenderHelpers.renderList',
[FRAGMENT]: 'Fragment',
})
......@@ -272,6 +272,26 @@ export function processFor(
const { addIdentifiers, removeIdentifiers, scopes } = context
const { source, value, key, index } = parseResult
if (key === undefined) {
parseResult.key = {
constType: 2,
content: '_key_',
isStatic: false,
loc: value?.loc!,
type: 4,
}
}
if (index === undefined) {
parseResult.index = {
constType: 2,
content: '_index_',
isStatic: false,
loc: value?.loc!,
type: 4,
}
}
const forNode: ForNode = {
type: NodeTypes.FOR,
loc: dir.loc,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册