Skip to content

Commit

Permalink
test(transformer): 修改 evalClass
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jul 4, 2018
1 parent a32661e commit ef99f65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/taro-transformer-wx/__tests__/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,13 @@ describe('Template', () => {

expect(template).toMatch(prettyPrint(`
<block>
<view class=\"page-body\" wx:if=\"{{tasks !== null}}\"></view>
<view class=\"page-body\" wx:else>
<text>Hello world!</text>
</view>
<block wx:if=\"{{tasks !== null}}\">
<view class=\"page-body\"></view>
</block>
<view class=\"page-body\" wx:else>
<text>Hello world!</text>
</view>
</block>
`))
})

Expand Down Expand Up @@ -307,15 +309,19 @@ describe('Template', () => {
})

expect(template).toMatch(prettyPrint(`
<block>
<view class=\"page-body\" wx:if=\"{{tasks !== null}}\"></view>
<view class=\"page-body\" wx:elif=\"{{tasks.length === 0}}\">
<text>{{tasks.length}}</text>
</view>
<view class=\"page-body\" wx:else>
<text>Hello world!</text>
</view>
</block>
<block>
<block wx:if=\"{{tasks !== null}}\">
<view class=\"page-body\"></view>
</block>
<block wx:elif=\"{{tasks.length === 0}}\">
<view class=\"page-body\">
<text>{{tasks.length}}</text>
</view>
</block>
<view class=\"page-body\" wx:else>
<text>Hello world!</text>
</view>
</block>
`))
})
})
Expand Down
10 changes: 10 additions & 0 deletions packages/taro-transformer-wx/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ export function evalClass (ast: t.File, props = '') {
// constructor 即便没有被定义也会被加上
if (t.isClassMethod(method) && method.kind === 'constructor') {
const index = method.body.body.findIndex(node => t.isSuper(node))
method.body.body.push(
t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(
t.thisExpression(),
t.identifier('state')
),
t.callExpression(t.memberExpression(t.thisExpression(), t.identifier('_createData')), [])
))
)
method.body.body.splice(index, 0, ...statements)
}
}
Expand Down

0 comments on commit ef99f65

Please sign in to comment.