Skip to content

Commit

Permalink
feat(transformer): 如果用户没有写在循环中写 index,就生成一个匿名 index
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Apr 15, 2019
1 parent 699ebe2 commit b60f5f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/taro-transformer-wx/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('ref', () => {
expect(refs[0].type).toBe('component')
expect(refs[0].refName).toBe('a')
expect(refs[0].fn).toBe(null)
expect(template).toMatch(/<custom id="[a-zA-Z]{5}" compid=\"{{\$compid__0}}\"><\/custom>/)
expect(template).toMatch(/<custom id="[a-zA-Z]{5}"><\/custom>/)
})

test('字符串模板', () => {
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('ref', () => {
expect(template).toMatch(prettyPrint(`
<block>
<view>
<cover id=\"{{item.$loopState__temp2}}\" wx:for=\"{{loopArray0}}\" wx:for-item=\"item\" wx:for-index=\"index\" compid=\"{{item.$compid__1}}\">
<cover id=\"{{item.$loopState__temp2}}\" wx:for=\"{{loopArray0}}\" wx:for-item=\"item\" wx:for-index=\"index\">
<text wx:for=\"{{item.$original.list}}\" wx:for-item=\"item2\">{{item2}}</text>
</cover>
</view>
Expand Down
12 changes: 10 additions & 2 deletions packages/taro-transformer-wx/__tests__/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ describe('Template', () => {
expect(template).toMatch(
`<test compid=\"{{$compid__0}}\"></test>`
)
expect(inst.state.style).toEqual('color:' + 'red')
expect(inst.testProps).toEqual({
test: 'color:' + 'red'
})
})

test('能在循环中使用, 无 return', () => {
Expand Down Expand Up @@ -558,7 +560,13 @@ describe('Template', () => {
// expect(props.$name).toBe('Custom')
// expect(props.hidden).toBe(true)
expect(template).toMatch(
`<custom wx:for=\"{{array}}\" __triggerObserer=\"{{ _triggerObserer }}\" wx:for-item=\"a1\"></custom>`
prettyPrint(`
<block>
<view>
<custom wx:for=\"{{array}}\" wx:for-item=\"a1\"></custom>
</view>
</block>
`)
)
})
})
Expand Down
8 changes: 5 additions & 3 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ export class RenderParser {
!DEFAULT_Component_SET.has(openingElement.name.name) &&
/[A-Z]/.test(openingElement.name.name.charAt(0))
) {
if (openingElement.attributes.length === 0) {
if (this.isEmptyProps(openingElement.attributes)) {
return
}
const name = `$compid__${genCompid()}`
Expand Down Expand Up @@ -1478,6 +1478,8 @@ export class RenderParser {
}
}

isEmptyProps = (attrs: t.JSXAttribute[]) => attrs.filter(a => ![Adapter.for, Adapter.forIndex, Adapter.forItem, 'id'].includes(a.name.name as string)).length === 0

/**
* jsxDeclarations,
* renderScope,
Expand Down Expand Up @@ -1575,10 +1577,10 @@ export class RenderParser {
!DEFAULT_Component_SET.has(element.name.name) &&
/[A-Z]/.test(element.name.name.charAt(0))
) {
// 如果循环里包含自定义组件
if (element.attributes.length === 0) {
if (this.isEmptyProps(element.attributes)) {
return
}
// 如果循环里包含自定义组件
if (!loops) {
loops = t.arrayExpression([])
findParentLoops(callee, this.loopComponentNames, loops)
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-transformer-wx/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ export function findParentLoops (
}

if (indexId === null || !t.isIdentifier(indexId)) {
indexId = t.identifier(callee.scope.generateUid('anonIdx'))
indexId = t.identifier(callee.scope.generateUid('anonIdx'));
(func as any).params = [(func as any).params[0], indexId]
}

if (!name) {
Expand Down

0 comments on commit b60f5f1

Please sign in to comment.