Skip to content

Commit

Permalink
refactor(transformer): 如果自定义组件没有任何 props 就不需要生成 id
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Apr 15, 2019
1 parent 89a0273 commit 699ebe2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
56 changes: 28 additions & 28 deletions packages/taro-transformer-wx/__tests__/control-flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ describe('if statement', () => {
<block>
<block wx:if=\"{{a}}\">
<block wx:if=\"{{c}}\">
<aa compid=\"{{$compid__0}}\"></aa>
<aa></aa>
</block>
</block>
<block wx:elif=\"{{b}}\">
<bb compid=\"{{$compid__1}}\"></bb>
<bb></bb>
</block>
<block wx:else>
<c compid=\"{{$compid__2}}\"></c>
<c></c>
</block>
</block>
</view>
Expand Down Expand Up @@ -373,15 +373,15 @@ describe('if statement', () => {
<view>
<block>
<block wx:if=\"{{a}}\">
<aa compid=\"{{$compid__3}}\"></aa>
<aa></aa>
</block>
<block wx:elif=\"{{b}}\">
<block wx:if=\"{{c}}\">
<bb compid=\"{{$compid__4}}\"></bb>
<bb></bb>
</block>
</block>
<block wx:else>
<c compid=\"{{$compid__5}}\"></c>
<c></c>
</block>
</block>
</view>
Expand Down Expand Up @@ -418,16 +418,16 @@ describe('if statement', () => {
<block>
<block wx:if=\"{{a}}\">
<block wx:if=\"{{d}}\">
<aa compid=\"{{$compid__6}}\"></aa>
<aa></aa>
</block>
</block>
<block wx:elif=\"{{b}}\">
<block wx:if=\"{{c}}\">
<bb compid=\"{{$compid__7}}\"></bb>
<bb></bb>
</block>
</block>
<block wx:else>
<c compid=\"{{$compid__8}}\"></c>
<c></c>
</block>
</block>
</view>
Expand Down Expand Up @@ -466,19 +466,19 @@ describe('if statement', () => {
<block>
<block wx:if=\"{{a}}\">
<block wx:if=\"{{d}}\">
<aa compid=\"{{$compid__9}}\"></aa>
<aa></aa>
</block>
<block wx:elif=\"{{e}}\">
<d compid=\"{{$compid__10}}\"></d>
<d></d>
</block>
</block>
<block wx:elif=\"{{b}}\">
<block wx:if=\"{{c}}\">
<bb compid=\"{{$compid__11}}\"></bb>
<bb></bb>
</block>
</block>
<block wx:else>
<c compid=\"{{$compid__12}}\"></c>
<c></c>
</block>
</block>
</view>
Expand Down Expand Up @@ -507,7 +507,7 @@ describe('if statement', () => {

expect(template).toMatch(prettyPrint(`
<block>
<container compid=\"{{$compid__13}}\">
<container>
<block wx:for=\"{{loopArray0}}\" wx:for-item=\"item\">
<block wx:if=\"{{item.$original === 0}}\">
<image/>
Expand Down Expand Up @@ -541,13 +541,13 @@ describe('if statement', () => {

expect(template).toMatch(prettyPrint(`
<block>
<container compid=\"{{$compid__15}}\">
<container>
<block wx:for=\"{{loopArray0}}\" wx:for-item=\"item\">
<block wx:if=\"{{item.$original === 0}}\">
<image/>
</block>
<block wx:elif=\"{{item.$original === 1}}\">
<test compid=\"{{item.$compid__14}}\"></test>
<test></test>
</block>
<video wx:else></video>
</block>
Expand Down Expand Up @@ -576,13 +576,13 @@ describe('if statement', () => {

expect(template).toMatch(prettyPrint(`
<block>
<container compid=\"{{$compid__17}}\">
<container>
<block wx:for=\"{{loopArray0}}\" wx:for-item=\"item\">
<block wx:if=\"{{item.$original === 0}}\">
<image/>
</block>
<block wx:elif=\"{{item.$original === 1}}\">
<test compid=\"{{item.$compid__16}}\"></test>
<test></test>
</block>
<block wx:else></block>
</block>
Expand Down Expand Up @@ -611,13 +611,13 @@ describe('if statement', () => {

expect(template).toMatch(prettyPrint(`
<block>
<container compid=\"{{$compid__19}}\">
<container>
<block wx:for=\"{{loopArray0}}\" wx:for-item=\"item\">
<block wx:if=\"{{item.$loopState__temp2}}\">
<image/>
</block>
<block wx:elif=\"{{item.$original === 1}}\">
<test compid=\"{{item.$compid__18}}\"></test>
<test></test>
</block>
<block wx:else></block>
</block>
Expand Down Expand Up @@ -646,13 +646,13 @@ describe('if statement', () => {

expect(template).toMatch(prettyPrint(`
<block>
<container compid=\"{{$compid__21}}\">
<container>
<block wx:for=\"{{loopArray0}}\" wx:for-item=\"item\">
<block wx:if=\"{{item.$loopState__temp2}}\">
<image src=\"{{item.$loopState__temp4}}\" />
</block>
<block wx:elif=\"{{item.$original === 1}}\">
<test compid=\"{{item.$compid__20}}\"></test>
<test></test>
</block>
<block wx:else></block>
</block>
Expand Down Expand Up @@ -682,13 +682,13 @@ describe('if statement', () => {

expect(template).toMatch(prettyPrint(`
<block>
<container compid=\"{{$compid__23}}\">
<container>
<block wx:for=\"{{loopArray0}}\" wx:for-item=\"item\">
<block wx:if=\"{{item.$loopState__temp2}}\">
<image src=\"{{item.$loopState__temp4}}\" />
</block>
<block wx:if=\"{{item.$original === 1}}\">
<test compid=\"{{item.$compid__22}}\"></test>
<test></test>
</block>
<block wx:else></block>
</block>
Expand Down Expand Up @@ -722,13 +722,13 @@ describe('if statement', () => {
<view>
<block>
<block wx:if=\"{{current === 0}}\">
<home compid=\"{{$compid__24}}\"></home>
<home></home>
</block>
<block wx:elif=\"{{current === 1}}\">
<goods compid=\"{{$compid__25}}\"></goods>
<goods></goods>
</block>
<block wx:elif=\"{{current === 2}}\">
<order compid=\"{{$compid__26}}\"></order>
<order></order>
</block>
</block>
</view>
Expand Down Expand Up @@ -1218,7 +1218,7 @@ describe('inline 表达式', () => {
<block>
<view>
<block wx:if=\"{{!text}}\">
<btn compid=\"{{$compid__27}}\"></btn>
<btn></btn>
</block>
</view>
</block>
Expand Down
6 changes: 3 additions & 3 deletions packages/taro-transformer-wx/__tests__/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Template', () => {
removeShadowData(inst.state)
expect(Object.keys(inst.state).length).toEqual(1)
expect(template).toMatch(
`<test test=\"{{style}}\" __triggerObserer=\"{{ _triggerObserer }}\"></test>`
`<test compid=\"{{$compid__0}}\"></test>`
)
expect(inst.state.style).toEqual('color:' + 'red')
})
Expand Down Expand Up @@ -531,7 +531,7 @@ describe('Template', () => {
// expect(props.$name).toBe('Custom')
// expect(props.hidden).toBe(true)
expect(template).toMatch(
`<custom hidden=\"{{true}}\" __triggerObserer=\"{{ _triggerObserer }}\"></custom>`
`<custom compid=\"{{$compid__1}}\"></custom>`
)
})

Expand Down Expand Up @@ -814,7 +814,7 @@ describe('字符不转义', () => {
expect(template).toMatch(
prettyPrint(`
<block>
<link __triggerObserer=\"{{ _triggerObserer }}\"></link>
<link></link>
</block>
`)
)
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-transformer-wx/src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ export function parseJSXElement (element: t.JSXElement): string {
return obj
}, {})
} else if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
attributesTrans[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
if (Adapter.type !== Adapters.weapp) {
attributesTrans[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
}
}

return createHTMLElement({
Expand Down
8 changes: 7 additions & 1 deletion packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,9 @@ export class RenderParser {
!DEFAULT_Component_SET.has(openingElement.name.name) &&
/[A-Z]/.test(openingElement.name.name.charAt(0))
) {
if (openingElement.attributes.length === 0) {
return
}
const name = `$compid__${genCompid()}`
const variableName = t.identifier(name)
this.referencedIdentifiers.add(variableName)
Expand Down Expand Up @@ -1573,6 +1576,9 @@ export class RenderParser {
/[A-Z]/.test(element.name.name.charAt(0))
) {
// 如果循环里包含自定义组件
if (element.attributes.length === 0) {
return
}
if (!loops) {
loops = t.arrayExpression([])
findParentLoops(callee, this.loopComponentNames, loops)
Expand Down Expand Up @@ -1780,7 +1786,7 @@ export class RenderParser {
}
})
} else {
throw codeFrameError(object.loc, '多层循环中循环的数组只能是一个变量或成员表达式')
throw codeFrameError(object.loc, '多层循环中循环的数组只能是一个变量或成员表达式,可以尝试把该表达式赋值给循环内部的一个新变量。')
}
}
}
Expand Down

0 comments on commit 699ebe2

Please sign in to comment.