提交 114561e5 编写于 作者: fxy060608's avatar fxy060608

wip(mp): scope

上级 5b70a108
......@@ -23,6 +23,13 @@ describe('compiler: scope', () => {
`<view wx:for="{{a}}" wx:for-item="item"><view wx:for="{{b}}" wx:for-item="item1" data-id="{{item.a}}" data-title="{{item1.a}}"/></view>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, item => { return { a: item.id }; }), b: _vFor(_ctx.item1, item1 => { return { a: item1.title }; }) }
}`
)
assert(
`<view v-for="(item,weekIndex) in weeks" :key="weekIndex" :data-id="item.id"><view v-for="(weeks,weeksIndex) in item" :key="weeksIndex" :data-id="weeks.id"/></view>`,
`<view wx:for="{{a}}" wx:for-item="item" wx:key="b" data-id="{{item.c}}"><view wx:for="{{item.a}}" wx:for-item="weeks" wx:key="a" data-id="{{weeks.b}}"/></view>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.weeks, (item, weekIndex) => { return { a: _vFor(item, (weeks, weeksIndex) => { return { a: weeksIndex, b: weeks.id }; }), b: weekIndex, c: item.id }; }) }
}`
)
})
......
......@@ -26,7 +26,7 @@ function assert(
// expect(res.template).toBe(templateCode)
// expect(res.code).toBe(renderCode)
// console.log(require('util').inspect(res.code, { colors: true, depth: null }))
console.log(require('util').inspect(res, { colors: true, depth: null }))
// console.log(require('util').inspect(res, { colors: true, depth: null }))
console.log(res.code)
expect(res.code).toBe(renderCode)
}
......@@ -34,10 +34,10 @@ function assert(
describe('compiler', () => {
test('scope', () => {
assert(
`<view v-for="item in items"><view v-for="item1 in item1" :data-id="item.id" :data-title="item1.title"/></view>`,
`<view wx:for="{{a}}" wx:for-item="item"><view wx:for="{{b}}" wx:for-item="item1" data-id="{{item.a}}" data-title="{{item1.a}}"/></view>`,
`<view v-for="(item,weekIndex) in weeks" :key="weekIndex" :data-id="item.id"><view v-for="(weeks,weeksIndex) in item" :key="weeksIndex" :data-id="weeks.id"/></view>`,
`<view wx:for="{{a}}" wx:for-item="item" wx:key="b" data-id="{{item.c}}"><view wx:for="{{item.a}}" wx:for-item="weeks" wx:key="a" data-id="{{weeks.b}}"/></view>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, item => { return { a: item.id }; }), b: _vFor(_ctx.item1, item1 => { return { a: item1.title }; }) }
return { a: _vFor(_ctx.weeks, (item, weekIndex) => { return { a: _vFor(item, (weeks, weeksIndex) => { return { a: weeksIndex, b: weeks.id }; }), b: weekIndex, c: item.id }; }) }
}`
)
})
......
......@@ -29,7 +29,7 @@ describe(`compiler: v-for`, () => {
`<view v-for="index in 5" />`,
`<view wx:for="{{a}}" wx:for-item="index"/>`,
`(_ctx, _cache) => {
return { a: _vFor([1, 2, 3, 4, 5], index => { return {}; }) }
return { a: _vFor(5, index => { return {}; }) }
}`
)
})
......@@ -63,7 +63,7 @@ describe(`compiler: v-for`, () => {
test(`value and key`, () => {
assert(
`<view v-for="(item, key) in items" />`,
`<view wx:for="{{a}}" wx:for-item="item" wx:for-index="key"/>`,
`<view wx:for="{{a}}" wx:for-item="item"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, key) => { return {}; }) }
}`
......@@ -72,7 +72,7 @@ describe(`compiler: v-for`, () => {
test(`value, key and index`, () => {
assert(
`<view v-for="(item, key, index) in items" />`,
`<view wx:for="{{a}}" wx:for-item="item" wx:for-index="key"/>`,
`<view wx:for="{{a}}" wx:for-item="item"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, key, index) => { return {}; }) }
}`
......@@ -108,7 +108,7 @@ describe(`compiler: v-for`, () => {
test(`unbracketed value and key`, () => {
assert(
`<view v-for="item, key in items" />`,
`<view wx:for="{{a}}" wx:for-item="item" wx:for-index="key"/>`,
`<view wx:for="{{a}}" wx:for-item="item"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (item, key) => { return {}; }) }
}`
......@@ -117,7 +117,7 @@ describe(`compiler: v-for`, () => {
test(`unbracketed value, key and index`, () => {
assert(
`<view v-for="value, key, index in items" />`,
`<view wx:for="{{a}}" wx:for-item="value" wx:for-index="key"/>`,
`<view wx:for="{{a}}" wx:for-item="value"/>`,
`(_ctx, _cache) => {
return { a: _vFor(_ctx.items, (value, key, index) => { return {}; }) }
}`
......
......@@ -16,9 +16,7 @@ import {
Identifier,
returnStatement,
conditionalExpression,
arrayExpression,
NumericLiteral,
numericLiteral,
isNumericLiteral,
Pattern,
RestElement,
......@@ -80,24 +78,24 @@ export function createVIfSpreadElement(vIfScope: CodegenVIfScope) {
return spreadElement(createVIfConditionalExpression(vIfScope))
}
function numericLiteralToArrayExpr(num: number) {
const elements: NumericLiteral[] = []
for (let i = 0; i < num; i++) {
elements.push(numericLiteral(i + 1))
}
return arrayExpression(elements)
}
// function numericLiteralToArrayExpr(num: number) {
// const elements: NumericLiteral[] = []
// for (let i = 0; i < num; i++) {
// elements.push(numericLiteral(i + 1))
// }
// return arrayExpression(elements)
// }
export function createVForCallExpression(
vForScope: CodegenVForScope,
context: TransformContext
) {
let sourceExpr: Expression = vForScope.sourceExpr!
if (isNumericLiteral(sourceExpr)) {
sourceExpr = numericLiteralToArrayExpr((sourceExpr as NumericLiteral).value)
}
// let sourceExpr: Expression = vForScope.sourceExpr!
// if (isNumericLiteral(sourceExpr)) {
// sourceExpr = numericLiteralToArrayExpr((sourceExpr as NumericLiteral).value)
// }
return callExpression(identifier(context.helperString(V_FOR)), [
sourceExpr,
vForScope.sourceExpr!,
createVForArrowFunctionExpression(vForScope),
])
}
......
......@@ -83,9 +83,6 @@ function genVFor(
if (valueAlias) {
push(` ${directive}for-item="${valueAlias}"`)
}
if (keyAlias) {
push(` ${directive}for-index="${keyAlias}"`)
}
const keyProp = findProp(node, 'key', true)
if (keyProp) {
const key = ((keyProp as DirectiveNode).exp as SimpleExpressionNode).content
......
......@@ -116,9 +116,8 @@ function isReferencedScope(node: Expression, scope: CodegenVForScope) {
return
}
if (
parent &&
knownIds.includes(node.name) &&
isReferenced(node, parent as any)
(!parent || isReferenced(node, parent as any))
) {
referenced = true
return this.skip()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册