compiler-extra.spec.js 42.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const compiler = require('../lib')

fxy060608's avatar
fxy060608 已提交
3
function assertCodegen (template, templateCode, renderCode = 'with(this){}', options = {}, mpOptions = {}) {
4
  const res = compiler.compile(template, Object.assign({
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12 13 14 15 16
    resourcePath: 'test.wxml',
    mp: Object.assign({
      minified: true,
      isTest: true,
      platform: 'mp-weixin'
    }, mpOptions)
  }, options))

  expect(res.template).toBe(templateCode)
  expect(res.render).toBe(renderCode)
}

17
describe('mp:compiler-extra', () => {
18 19 20
  it('generate mp filter ', () => {
    assertCodegen(
      /* eslint-disable no-template-curly-in-string */
fxy060608's avatar
fxy060608 已提交
21 22
      '<view @touchmove="a.touchmove">{{t.a}}{{t[\'a\']}}{{t.a(b)}}{{t[\'a\'](b)}}{{u.t.a(b)}}{{u.t.a}}</view>',
      '<view bindtouchmove="{{a.touchmove}}">{{t.a+t[\'a\']+t.a(b)+t[\'a\'](b)+$root.g0+u.t.a}}</view>',
23
      'with(this){var g0=u.t.a(b);$mp.data=Object.assign({},{$root:{g0:g0}})}', {
24 25 26 27
        filterModules: {
          t: {},
          a: {}
        }
28 29 30 31
      }
    )
  })

fxy060608's avatar
fxy060608 已提交
32 33 34
  it('generate scopeId', () => {
    assertCodegen(
      '<view></view>',
fxy060608's avatar
fxy060608 已提交
35
      '<view class="data-v-1"></view>',
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41
      undefined, {
        scopeId: 'data-v-1'
      }
    )
    assertCodegen(
      '<view class="view"></view>',
fxy060608's avatar
fxy060608 已提交
42
      '<view class="view data-v-2"></view>',
fxy060608's avatar
fxy060608 已提交
43 44 45 46 47 48
      undefined, {
        scopeId: 'data-v-2'
      }
    )
    assertCodegen(
      '<view :class="[{ active: isActive }, errorClass]"></view>',
fxy060608's avatar
fxy060608 已提交
49
      '<view class="{{[\'data-v-3\',[(isActive)?\'active\':\'\'],errorClass]}}"></view>',
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55
      undefined, {
        scopeId: 'data-v-3'
      }
    )
    assertCodegen(
      '<view class="static" :class="[{ active: isActive }, errorClass]"></view>',
fxy060608's avatar
fxy060608 已提交
56
      '<view class="{{[\'static data-v-4\',[(isActive)?\'active\':\'\'],errorClass]}}"></view>',
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61 62
      undefined, {
        scopeId: 'data-v-4'
      }
    )
    assertCodegen(
      '<view ref="ref" :class="[{ active: isActive }, errorClass]"></view>',
fxy060608's avatar
fxy060608 已提交
63
      '<view data-ref="ref" class="{{[\'data-v-5 vue-ref\',[(isActive)?\'active\':\'\'],errorClass]}}"></view>',
fxy060608's avatar
fxy060608 已提交
64 65 66 67 68 69
      undefined, {
        scopeId: 'data-v-5'
      }
    )
    assertCodegen(
      '<view :class="view"></view>',
fxy060608's avatar
fxy060608 已提交
70
      '<view class="{{[\'data-v-6\',view]}}"></view>',
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
      undefined, {
        scopeId: 'data-v-6'
      }
    )
    //     assertCodegen(
    //       '<view :class="view"></view>',
    //       `<view class="{{$root.c0}}"></view>`,
    //       `with(this){var c0=__get_class(view,"data-v-6");$mp.data=Object.assign({},{$root:{c0:c0}})}`, {
    //         scopeId: 'data-v-6'
    //       }
    //     )
    assertCodegen(
      '<view :class="view" class="view"></view>',
fxy060608's avatar
fxy060608 已提交
84
      '<view class="{{[\'view data-v-7\',view]}}"></view>',
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      undefined, {
        scopeId: 'data-v-7'
      }
    )
    //         assertCodegen(
    //             '<view :class="view" class="view"></view>',
    //             `<view class="{{$root.c0}}"></view>`,
    //             `with(this){var c0=__get_class(view,"view data-v-7");$mp.data=Object.assign({},{$root:{c0:c0}})}`, {
    //                 scopeId: 'data-v-7'
    //             }
    //         )
  })

  it('generate staticStyle upx and px', () => {
    assertCodegen(
      '<view style="height:100upx;width:100upx;">text</view>',
fxy060608's avatar
fxy060608 已提交
101
      '<view style="height:100rpx;width:100rpx;">text</view>'
fxy060608's avatar
fxy060608 已提交
102 103 104
    )
    assertCodegen(
      '<view style="height:100upx;width:100rpx;">text</view>',
fxy060608's avatar
fxy060608 已提交
105
      '<view style="height:100rpx;width:100rpx;">text</view>'
fxy060608's avatar
fxy060608 已提交
106 107 108
    )
    assertCodegen(
      '<view style="height:100px;width:100px;">text</view>',
fxy060608's avatar
fxy060608 已提交
109
      '<view style="height:100px;width:100px;">text</view>'
fxy060608's avatar
fxy060608 已提交
110 111 112
    )
    assertCodegen(
      '<view style="height:100px;width:100px;">text</view>',
fxy060608's avatar
fxy060608 已提交
113
      '<view style="height:100rpx;width:100rpx;">text</view>',
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118 119 120 121 122 123
      undefined,
      undefined, {
        transformPx: true
      }
    )
  })

  it('generate text trim', () => {
    assertCodegen(
      '<view>text</view>',
fxy060608's avatar
fxy060608 已提交
124
      '<view>text</view>'
fxy060608's avatar
fxy060608 已提交
125 126 127 128
    )

    assertCodegen(
      '<view> text </view>',
fxy060608's avatar
fxy060608 已提交
129
      '<view>text</view>'
fxy060608's avatar
fxy060608 已提交
130 131 132
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
133 134
      '<text>{{line_one_cn+\' \'}}</text>',
      '<text>{{line_one_cn+\' \'}}</text>'
fxy060608's avatar
fxy060608 已提交
135 136 137
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
138 139
      '<text>{{" "+line_one_cn}}</text>',
      '<text>{{" "+line_one_cn}}</text>'
fxy060608's avatar
fxy060608 已提交
140 141 142 143
    )

    assertCodegen(
      '<text>\nN: {{title}}\n′</text>',
fxy060608's avatar
fxy060608 已提交
144
      '<text>{{\'N: \'+title+"\\n′"}}</text>'
fxy060608's avatar
fxy060608 已提交
145 146 147
    )
    assertCodegen(
      '<text>我是第一行\n我的第二行</text>',
fxy060608's avatar
fxy060608 已提交
148
      '<text>我是第一行\n我的第二行</text>'
fxy060608's avatar
fxy060608 已提交
149 150 151
    )
    assertCodegen(
      '<text>我是第一行\n我的第二行1{{title}}</text>',
fxy060608's avatar
fxy060608 已提交
152
      '<text>{{"我是第一行\\n我的第二行1"+title}}</text>'
fxy060608's avatar
fxy060608 已提交
153 154 155 156
    )
    assertCodegen(
      `<text>我是第一行
我的第二行2{{title}}</text>`,
fxy060608's avatar
fxy060608 已提交
157
      '<text>{{"我是第一行\\n我的第二行2"+title}}</text>'
fxy060608's avatar
fxy060608 已提交
158 159 160 161
    )

    assertCodegen(
      '<view> text text </view>',
fxy060608's avatar
fxy060608 已提交
162
      '<view>text text</view>'
fxy060608's avatar
fxy060608 已提交
163 164 165
    )
    assertCodegen(
      '<view>text {{text}} text</view>',
fxy060608's avatar
fxy060608 已提交
166
      '<view>{{"text "+text+" text"}}</view>'
fxy060608's avatar
fxy060608 已提交
167 168 169 170 171 172 173
    )
    //         assertCodegen(
    //             '<view>text {{text}} \ntext</view>',
    //             `<view>{{"text " + text + " \ntext"}}</view>`
    //         )
    assertCodegen(
      '<view> text {{text}} 文本 </view>',
fxy060608's avatar
fxy060608 已提交
174
      '<view>{{\'text \'+text+\' 文本\'}}</view>'
fxy060608's avatar
fxy060608 已提交
175 176 177
    )
    assertCodegen(
      '<view>{{text}} text  text </view>',
fxy060608's avatar
fxy060608 已提交
178
      '<view>{{text+\' text  text\'}}</view>'
fxy060608's avatar
fxy060608 已提交
179 180 181
    )
    assertCodegen(
      '<view>  {{text}} text  text </view>',
fxy060608's avatar
fxy060608 已提交
182
      '<view>{{\'\'+text+\' text  text\'}}</view>'
fxy060608's avatar
fxy060608 已提交
183 184 185
    )
    assertCodegen(
      '<view>{{text}} text  text {{text}}</view>',
fxy060608's avatar
fxy060608 已提交
186
      '<view>{{text+" text  text "+text}}</view>'
fxy060608's avatar
fxy060608 已提交
187 188 189
    )
    assertCodegen(
      '<view>  {{text}} text  text {{text}}  </view>',
fxy060608's avatar
fxy060608 已提交
190
      '<view>{{\'\'+text+" text  text "+text+\'\'}}</view>'
fxy060608's avatar
fxy060608 已提交
191 192 193 194 195 196
    )
  })

  it('generate default slot', () => {
    assertCodegen(
      '<component1>text</component1>',
fxy060608's avatar
fxy060608 已提交
197
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}">text</component1>'
fxy060608's avatar
fxy060608 已提交
198 199 200
    )
    assertCodegen(
      '<component1>text<text>123213</text></component1>',
fxy060608's avatar
fxy060608 已提交
201
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}">text<text>123213</text></component1>'
fxy060608's avatar
fxy060608 已提交
202 203 204
    )
    assertCodegen(
      '<component1>text<block slot="right"></block></component1>',
fxy060608's avatar
fxy060608 已提交
205
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\',\'right\']}}">text<view slot="right"></view></component1>'
fxy060608's avatar
fxy060608 已提交
206 207 208 209 210 211
    )
  })

  it('generate input value', () => {
    assertCodegen(
      '<input type="text" value="" />',
fxy060608's avatar
fxy060608 已提交
212
      '<input type="text" value=""/>'
fxy060608's avatar
fxy060608 已提交
213 214 215
    )
  })

216 217 218
  it('generate v-slot', () => {
    assertCodegen(
      '<view><template></template></view>',
fxy060608's avatar
fxy060608 已提交
219
      '<view></view>'
220
    )
fxy060608's avatar
fxy060608 已提交
221 222 223

    assertCodegen(
      '<component1><template slot="f">f</template><template slot="c">c</template>默认</component1>',
fxy060608's avatar
fxy060608 已提交
224
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\',\'f\',\'c\']}}"><view slot="f">f</view><view slot="c">c</view>默认</component1>'
fxy060608's avatar
fxy060608 已提交
225 226 227 228
    )

    assertCodegen(
      '<component1 v-slot>text</component1>',
229
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}"><view>text</view></component1>'
fxy060608's avatar
fxy060608 已提交
230 231 232 233
    )

    assertCodegen(
      '<component1 v-slot:default>text<text>123213</text></component1>',
234
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}"><view>text<text>123213</text></view></component1>'
fxy060608's avatar
fxy060608 已提交
235 236 237
    )
    assertCodegen(
      '<component1><template v-slot:left><text></text></template><template v-slot:right><text></text></template></component1>',
fxy060608's avatar
fxy060608 已提交
238
      '<component1 vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'left\',\'right\']}}"><view slot="left"><text></text></view><view slot="right"><text></text></view></component1>'
fxy060608's avatar
fxy060608 已提交
239 240 241 242 243 244 245 246 247 248 249
    )
    assertCodegen(
      `<my-component>
       <template v-slot:header>
        <h1>Here might be a page title</h1>
      </template>
      <p>A paragraph for the main content.</p>
      <template v-slot:footer>
        <p>Here's some contact info</p>
      </template>
    </my-component>`,
fxy060608's avatar
fxy060608 已提交
250
      '<my-component vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\',\'header\',\'footer\']}}"><view slot="header"><view class="_h1">Here might be a page title</view></view><view slot="footer"><view class="_p">Here\'s some contact info</view></view><view class="_p">A paragraph for the main content.</view></my-component>'
fxy060608's avatar
fxy060608 已提交
251 252 253
    )
  })

fxy060608's avatar
fxy060608 已提交
254 255 256 257
  it('generate events inside v-for', () => {
    assertCodegen(
      '<view v-for="item in dataList" :key="item.id" @click="click1(item, 1);click2(item, 2);"/>',
      '<block wx:for="{{dataList}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><view data-event-opts="{{[[\'tap\',[[\'click1\',[\'$0\',1],[[[\'dataList\',\'id\',item.id]]]],[\'click2\',[\'$0\',2],[[[\'dataList\',\'id\',item.id]]]]]]]}}" bindtap="__e"></view></block>'
258
    )
fxy060608's avatar
fxy060608 已提交
259 260
    // TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
261 262 263
      '<view>1<view  v-for="item in items" :key="item"><input v-for="item1 in item" :key="item1" @input="handle" @click="e=>count++"></view></view>',
      '<view>1<block wx:for="{{items}}" wx:for-item="item" wx:for-index="__i0__" wx:key="*this"><view><block wx:for="{{item}}" wx:for-item="item1" wx:for-index="__i1__" wx:key="*this"><input data-event-opts="{{[[\'input\',[[\'handle\',[\'$event\']]]],[\'tap\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e" bindtap="__e"/></block></view></block></view>',
      'with(this){if(!_isMounted){e0=e=>count++}}'
fxy060608's avatar
fxy060608 已提交
264 265
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
266 267 268
      '<view>2<view  v-for="(item,index) in items" :key="item"><input v-for="(item1,index1) in item" :key="item1" @input="handle" @click="e=>count++"></view></view>',
      '<view>2<block wx:for="{{items}}" wx:for-item="item" wx:for-index="index" wx:key="*this"><view><block wx:for="{{item}}" wx:for-item="item1" wx:for-index="index1" wx:key="*this"><input data-event-opts="{{[[\'input\',[[\'handle\',[\'$event\']]]],[\'tap\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e" bindtap="__e"/></block></view></block></view>',
      'with(this){if(!_isMounted){e0=e=>count++}}'
fxy060608's avatar
fxy060608 已提交
269 270
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
271 272
      '<view v-for="item in data.items" @tap="change(item,item.b,true)">3</view>',
      '<block wx:for="{{data.items}}" wx:for-item="item" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',true],[[[\'data.items\',\'\',__i0__]],[[\'data.items\',\'\',__i0__,\'b\']]]]]]]}}" bindtap="__e">3</view></block>'
fxy060608's avatar
fxy060608 已提交
273 274
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
275 276
      '<view v-for="(item,index) in data.items" @tap="change(item,item.b,true,index)">33</view>',
      '<block wx:for="{{data.items}}" wx:for-item="item" wx:for-index="index"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',true,index],[[[\'data.items\',\'\',index]],[[\'data.items\',\'\',index,\'b\']]]]]]]}}" bindtap="__e">33</view></block>'
fxy060608's avatar
fxy060608 已提交
277 278
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
279 280
      '<view v-for="(item,index) in data.items" @tap="change(item,true,index,item.b)">4</view>',
      '<block wx:for="{{data.items}}" wx:for-item="item" wx:for-index="index"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',true,index,\'$1\'],[[[\'data.items\',\'\',index]],[[\'data.items\',\'\',index,\'b\']]]]]]]}}" bindtap="__e">4</view></block>'
fxy060608's avatar
fxy060608 已提交
281 282
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
283 284
      '<view v-for="item in data.items" :key="item.id" @tap="change(item,item.b,true)">5</view>',
      '<block wx:for="{{data.items}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',true],[[[\'data.items\',\'id\',item.id]],[[\'data.items\',\'id\',item.id,\'b\']]]]]]]}}" bindtap="__e">5</view></block>'
fxy060608's avatar
fxy060608 已提交
285 286
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
287 288
      '<view v-for="item in data.items" :key="item.id" @tap="change(item,item.b,true)">6</view>',
      '<block wx:for="{{data.items}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',true],[[[\'data.items\',\'id\',item.id]],[[\'data.items\',\'id\',item.id,\'b\']]]]]]]}}" bindtap="__e">6</view></block>'
fxy060608's avatar
fxy060608 已提交
289 290
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
291 292
      '<view v-for="(item,index) in data.items" :key="item.id" @tap="change(item,item.b,test,true)">7<view v-for="(meta,index) in item.metas" :key="meta.id" @tap="change(meta,meta.b,true)"></view></view>',
      '<block wx:for="{{data.items}}" wx:for-item="item" wx:for-index="index" wx:key="id"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',\'$2\',true],[[[\'data.items\',\'id\',item.id]],[[\'data.items\',\'id\',item.id,\'b\']],\'test\']]]]]}}" bindtap="__e">7<block wx:for="{{item.metas}}" wx:for-item="meta" wx:for-index="index" wx:key="id"><view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',true],[[[\'data.items\',\'id\',item.id],[\'metas\',\'id\',meta.id]],[[\'data.items\',\'id\',item.id],[\'metas\',\'id\',meta.id,\'b\']]]]]]]}}" bindtap="__e"></view></block></view></block>'
fxy060608's avatar
fxy060608 已提交
293 294 295 296 297 298 299
    )
    assertCodegen(
      `<block v-for="(weeks, week) in canlender.weeks" :key="week">
        <block v-for="(day, index) in weeks" :key="index">,
             <view @tap="selectDays(week,index,canlender.month === day.month,day.disable,canlender.lunar)"></view>
        </block>
    </block>`,
fxy060608's avatar
fxy060608 已提交
300
      '<block wx:for="{{canlender.weeks}}" wx:for-item="weeks" wx:for-index="week" wx:key="week"><block><block wx:for="{{weeks}}" wx:for-item="day" wx:for-index="index" wx:key="index"><block>,<view data-event-opts="{{[[\'tap\',[[\'selectDays\',[week,index,canlender.month===day.month,\'$0\',\'$1\'],[[[\'canlender.weeks\',\'\',week],[\'\',\'\',index,\'disable\']],\'canlender.lunar\']]]]]}}" bindtap="__e"></view></block></block></block></block>'
301 302
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
303 304
      '<view v-for="item in list">9<input type="text" v-for="meta in item.meta" :key="meta.id" v-model="meta.value"></view>',
      '<block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__"><view>9<block wx:for="{{item.meta}}" wx:for-item="meta" wx:for-index="__i1__" wx:key="id"><input type="text" data-event-opts="{{[[\'input\',[[\'__set_model\',[\'$0\',\'value\',\'$event\',[]],[[[\'list\',\'\',__i0__],[\'meta\',\'id\',meta.id]]]]]]]}}" value="{{meta.value}}" bindinput="__e"/></block></view></block>'
305 306
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
307 308
      '<view @click="onSetYear(+1,-1)"></view>',
      '<view data-event-opts="{{[[\'tap\',[[\'onSetYear\',[1,-1]]]]]}}" bindtap="__e"></view>'
309 310
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
311 312
      '<view class="input-list" v-for="(item,index) in dataList" :key="item.id"><input v-model.trim="dataList2[index].val" /></view>',
      '<block wx:for="{{dataList}}" wx:for-item="item" wx:for-index="index" wx:key="id"><view class="input-list"><input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'$0\',\'val\',\'$event\',[\'trim\']],[\'dataList2.\'+index+\'\']]]],[\'blur\',[[\'$forceUpdate\']]]]}}" value="{{dataList2[index].val}}" bindinput="__e" bindblur="__e"/></view></block>'
fxy060608's avatar
fxy060608 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    )
    assertCodegen(
      ` <view>
        <view v-for="item in list[idx]" :key="item.id" class="mid-item-title" @click="m1(item)">
          <view class="mid-item-icon" @click.stop="m2(item)"></view>
        </view>
        </view>`,
      '<view><block wx:for="{{list[idx]}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><view data-event-opts="{{[[\'tap\',[[\'m1\',[\'$0\'],[[[\'list.\'+idx+\'\',\'id\',item.id]]]]]]]}}" class="mid-item-title" bindtap="__e"><view data-event-opts="{{[[\'tap\',[[\'m2\',[\'$0\'],[[[\'list.\'+idx+\'\',\'id\',item.id]]]]]]]}}" class="mid-item-icon" catchtap="__e"></view></view></block></view>'
    )
    assertCodegen(
      '<view><view class="item" v-for="i in \'abc\'" :key="i" @click="func(i)"></view></view>',
      '<view><block wx:for="abc" wx:for-item="i" wx:for-index="__i0__" wx:key="*this"><view data-event-opts="{{[[\'tap\',[[\'func\',[\'$0\'],[[[\'#s#abc\',\'\',__i0__]]]]]]]}}" class="item" bindtap="__e"></view></block></view>'
    )
    assertCodegen(
      '<view><view class="item" v-for="i in 5" :key="i" @click="func(i)"></view></view>',
      '<view><block wx:for="{{5}}" wx:for-item="i" wx:for-index="__i0__" wx:key="*this"><view data-event-opts="{{[[\'tap\',[[\'func\',[\'$0\'],[[[5,\'\',__i0__]]]]]]]}}" class="item" bindtap="__e"></view></block></view>'
    )
330 331 332 333 334
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view @click="$test.test(item,item.length)"></view></view>',
      '<block wx:for="{{list}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" data-event-params="{{({item})}}" bindtap="__e"></view></view></block>',
      'with(this){if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return $test.test(item,item.length)}}}'
    )
fxy060608's avatar
fxy060608 已提交
335 336 337 338
  })

  it('generate class binding', () => {
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
339 340
      '<p :class=\'"icon-"+item.icon\'>1</p>',
      '<view class="{{[\'_p\',\'icon-\'+item.icon]}}">1</view>'
fxy060608's avatar
fxy060608 已提交
341 342 343
    )
    assertCodegen(
      '<div :class="{ active: isActive }">1</div>',
fxy060608's avatar
fxy060608 已提交
344
      '<view class="{{[\'_div\',(isActive)?\'active\':\'\']}}">1</view>'
fxy060608's avatar
fxy060608 已提交
345 346
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
347 348
      '<p class="static" :class="{ active: isActive, \'text-danger\': hasError }">2</p>',
      '<view class="{{[\'static _p\',(isActive)?\'active\':\'\',(hasError)?\'text-danger\':\'\']}}">2</view>'
fxy060608's avatar
fxy060608 已提交
349 350 351
    )
    assertCodegen(
      '<p class="static" :class="[activeClass, errorClass]">3</p>',
fxy060608's avatar
fxy060608 已提交
352
      '<view class="{{[\'static _p\',activeClass,errorClass]}}">3</view>'
fxy060608's avatar
fxy060608 已提交
353 354
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
355 356
      '<p class="static" :class="[isActive ? activeClass : \'\', errorClass]">4</p>',
      '<view class="{{[\'static _p\',isActive?activeClass:\'\',errorClass]}}">4</view>'
fxy060608's avatar
fxy060608 已提交
357 358
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
359 360
      '<p class="static" :class="[{ active: isActive }, errorClass]">5</p>',
      '<view class="{{[\'static _p\',[(isActive)?\'active\':\'\'],errorClass]}}">5</view>'
fxy060608's avatar
fxy060608 已提交
361 362
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
363 364
      '<div class="container" :class="computedClassObject">6</div>',
      '<view class="{{[\'container _div\',computedClassObject]}}">6</view>'
fxy060608's avatar
fxy060608 已提交
365 366 367 368 369 370 371
    )
    //         assertCodegen(
    //             `<div class="container" :class="computedClassObject">6</div>`,
    //             `<view class="{{$root.c0}}">6</view>`,
    //             `with(this){var c0=__get_class(computedClassObject,"container");$mp.data=Object.assign({},{$root:{c0:c0}})}`
    //         )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
372 373
      '<p :class="index === currentIndex ? activeClass : itemClass">7</p>',
      '<view class="{{[\'_p\',index===currentIndex?activeClass:itemClass]}}">7</view>'
fxy060608's avatar
fxy060608 已提交
374 375
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
376 377
      '<p :class="\'m-content-head-\'+message.user">8</p>',
      '<view class="{{[\'_p\',\'m-content-head-\'+message.user]}}">8</view>'
fxy060608's avatar
fxy060608 已提交
378 379
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
380 381
      '<p :class="classStr1 || classStr2" class="bg">9</p>',
      '<view class="{{[\'bg _p\',classStr1||classStr2]}}">9</view>'
fxy060608's avatar
fxy060608 已提交
382 383 384 385 386
    )
  })

  it('generate style binding', () => {
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
387 388
      '<p :style="\'color:red\'">1</p>',
      '<view style="{{(\'color:red\')}}" class="_p">1</view>'
fxy060608's avatar
fxy060608 已提交
389 390
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
391 392
      '<p style="background-color:black" :style="\'color:red\'">1</p>',
      '<view style="{{\'background-color:black;\'+(\'color:red\')}}" class="_p">1</view>'
fxy060608's avatar
fxy060608 已提交
393 394
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
395 396
      '<p :style="{ color: activeColor }">1</p>',
      '<view style="{{\'color:\'+(activeColor)+\';\'}}" class="_p">1</view>'
fxy060608's avatar
fxy060608 已提交
397 398
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
399 400
      '<p :style="{ color: activeColor, fontSize: fontSize + \'px\' }">2</p>',
      '<view style="{{\'color:\'+(activeColor)+\';\'+(\'font-size:\'+(fontSize+\'px\')+\';\')}}" class="_p">2</view>'
fxy060608's avatar
fxy060608 已提交
401 402
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
403 404
      '<p :style="{ color: activeColor }" style="background-color:red;height:100upx">3</p>',
      '<view style="{{\'background-color:red;height:100rpx;\'+(\'color:\'+(activeColor)+\';\')}}" class="_p">3</view>'
fxy060608's avatar
fxy060608 已提交
405 406
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
407 408
      '<p :style="[{ color: activeColor, fontSize: fontSize + \'px\' }]">4</p>',
      '<view style="{{\'color:\'+(activeColor)+\';\'+(\'font-size:\'+(fontSize+\'px\')+\';\')}}" class="_p">4</view>'
fxy060608's avatar
fxy060608 已提交
409 410
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
411 412
      '<p :style="[{ color: activeColor, fontSize: fontSize + \'px\' },{ \'background-color\': activeColor}]">5</p>',
      '<view style="{{\'color:\'+(activeColor)+\';\'+(\'font-size:\'+(fontSize+\'px\')+\';\')+(\'background-color:\'+(activeColor)+\';\')}}" class="_p">5</view>'
fxy060608's avatar
fxy060608 已提交
413 414
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
415 416 417
      '<div :style="[baseStyles, overridingStyles]">6</div>',
      '<view style="{{$root.s0}}" class="_div">6</view>',
      'with(this){var s0=__get_style([baseStyles,overridingStyles]);$mp.data=Object.assign({},{$root:{s0:s0}})}'
fxy060608's avatar
fxy060608 已提交
418 419
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
420 421
      '<div :style="styleObject">7</div>',
      '<view style="{{(styleObject)}}" class="_div">7</view>'
fxy060608's avatar
fxy060608 已提交
422 423 424 425 426 427 428
    )
    //         assertCodegen(
    //             `<div :style="styleObject">7</div>`,
    //             `<view style="{{$root.s0}}">7</view>`,
    //             `with(this){var s0=__get_style(styleObject);$mp.data=Object.assign({},{$root:{s0:s0}})}`
    //         )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
429 430
      '<p :style="index === currentIndex ? activeStyle : itemStyle">8</p>',
      '<view style="{{(index===currentIndex?activeStyle:itemStyle)}}" class="_p">8</view>'
fxy060608's avatar
fxy060608 已提交
431 432
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
433 434
      '<p :style="\'background:\'+background">9</p>',
      '<view style="{{(\'background:\'+background)}}" class="_p">9</view>'
fxy060608's avatar
fxy060608 已提交
435 436
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
437 438
      '<p :style="styleStr1 || styleStr2" style="background:red">10</p>',
      '<view style="{{\'background:red;\'+(styleStr1||styleStr2)}}" class="_p">10</view>'
fxy060608's avatar
fxy060608 已提交
439 440 441 442 443 444
    )
  })

  it('generate events with v-on directive on custom component', () => {
    assertCodegen(
      '<my-component @click="handleClick"/>',
fxy060608's avatar
fxy060608 已提交
445
      '<my-component bind:click="__e" vue-id="551070e6-1" data-event-opts="{{[[\'^click\',[[\'handleClick\']]]]}}" bind:__l="__l"></my-component>'
fxy060608's avatar
fxy060608 已提交
446 447 448
    )
    assertCodegen(
      '<my-component @click-left="handleClick"/>',
fxy060608's avatar
fxy060608 已提交
449
      '<my-component bind:clickLeft="__e" vue-id="551070e6-1" data-event-opts="{{[[\'^clickLeft\',[[\'handleClick\']]]]}}" bind:__l="__l"></my-component>'
fxy060608's avatar
fxy060608 已提交
450 451 452 453 454 455
    )
  })

  it('generate v-model directive on custom component', () => {
    assertCodegen(
      '<my-component v-model="test" @input="handle">1</my-component>',
fxy060608's avatar
fxy060608 已提交
456
      '<my-component bind:input="__e" vue-id="551070e6-1" value="{{test}}" data-event-opts="{{[[\'^input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]],[\'handle\']]]]}}" bind:__l="__l" vue-slots="{{[\'default\']}}">1</my-component>'
fxy060608's avatar
fxy060608 已提交
457 458 459
    )
    assertCodegen(
      '<my-component v-model="test" @click="handle">2</my-component>',
fxy060608's avatar
fxy060608 已提交
460
      '<my-component bind:click="__e" bind:input="__e" vue-id="551070e6-1" value="{{test}}" data-event-opts="{{[[\'^click\',[[\'handle\']]],[\'^input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]]]]]}}" bind:__l="__l" vue-slots="{{[\'default\']}}">2</my-component>'
461 462 463
    )
    assertCodegen(
      '<my-component v-model="test.a">3</my-component>',
fxy060608's avatar
fxy060608 已提交
464
      '<my-component bind:input="__e" vue-id="551070e6-1" value="{{test.a}}" data-event-opts="{{[[\'^input\',[[\'__set_model\',[\'$0\',\'a\',\'$event\',[]],[\'test\']]]]]}}" bind:__l="__l" vue-slots="{{[\'default\']}}">3</my-component>'
465 466 467
    )
    assertCodegen(
      '<my-component v-model="test.a.b">4</my-component>',
fxy060608's avatar
fxy060608 已提交
468
      '<my-component bind:input="__e" vue-id="551070e6-1" value="{{test.a.b}}" data-event-opts="{{[[\'^input\',[[\'__set_model\',[\'$0\',\'b\',\'$event\',[]],[\'test.a\']]]]]}}" bind:__l="__l" vue-slots="{{[\'default\']}}">4</my-component>'
469 470 471
    )
    assertCodegen(
      '<my-component v-model="test[a.b][a.b]">4</my-component>',
fxy060608's avatar
fxy060608 已提交
472
      '<my-component bind:input="__e" vue-id="551070e6-1" value="{{test[a.b][a.b]}}" data-event-opts="{{[[\'^input\',[[\'__set_model\',[\'$0\',\'$1\',\'$event\',[]],[\'test.\'+a.b+\'\',\'a.b\']]]]]}}" bind:__l="__l" vue-slots="{{[\'default\']}}">4</my-component>'
fxy060608's avatar
fxy060608 已提交
473 474 475 476 477 478
    )
  })

  it('generate object property on custom component', () => {
    assertCodegen(
      '<my-component v-model="test" @input="handle" />',
fxy060608's avatar
fxy060608 已提交
479
      '<my-component bind:input="__e" vue-id="551070e6-1" value="{{test}}" data-event-opts="{{[[\'^input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]],[\'handle\']]]]}}" bind:__l="__l"></my-component>'
fxy060608's avatar
fxy060608 已提交
480 481 482 483 484
    )
  })
  it('generate v-text directive', () => {
    assertCodegen(
      '<view v-text="aaa1"></view>',
fxy060608's avatar
fxy060608 已提交
485
      '<view>{{aaa1}}</view>'
fxy060608's avatar
fxy060608 已提交
486 487 488
    )
    assertCodegen(
      '<view v-text="aaa1+1"></view>',
fxy060608's avatar
fxy060608 已提交
489
      '<view>{{aaa1+1}}</view>'
fxy060608's avatar
fxy060608 已提交
490 491
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
492 493
      '<view v-text="\'aaa2\'"></view>',
      '<view>aaa2</view>'
fxy060608's avatar
fxy060608 已提交
494 495 496 497 498
    )
  })
  it('generate v-html directive', () => {
    assertCodegen(
      '<view v-html="aaa"></view>',
fxy060608's avatar
fxy060608 已提交
499
      '<view><rich-text nodes="{{aaa}}"></rich-text></view>'
fxy060608's avatar
fxy060608 已提交
500 501 502 503 504 505
    )
  })

  it('generate v-bind directive with sync modifier', () => {
    assertCodegen(
      '<text-document :title.sync="aaa"></text-document>',
fxy060608's avatar
fxy060608 已提交
506
      '<text-document vue-id="551070e6-1" title="{{aaa}}" data-event-opts="{{[[\'^updateTitle\',[[\'__set_sync\',[\'$0\',\'aaa\',\'$event\'],[\'\']]]]]}}" bind:updateTitle="__e" bind:__l="__l"></text-document>'
fxy060608's avatar
fxy060608 已提交
507 508 509
    )
    assertCodegen(
      '<text-document :title.sync="doc.title"></text-document>',
fxy060608's avatar
fxy060608 已提交
510
      '<text-document vue-id="551070e6-1" title="{{doc.title}}" data-event-opts="{{[[\'^updateTitle\',[[\'__set_sync\',[\'$0\',\'title\',\'$event\'],[\'doc\']]]]]}}" bind:updateTitle="__e" bind:__l="__l"></text-document>'
fxy060608's avatar
fxy060608 已提交
511 512 513
    )
    assertCodegen(
      '<text-document :title.sync="doc.a.title"></text-document>',
fxy060608's avatar
fxy060608 已提交
514
      '<text-document vue-id="551070e6-1" title="{{doc.a.title}}" data-event-opts="{{[[\'^updateTitle\',[[\'__set_sync\',[\'$0\',\'title\',\'$event\'],[\'doc.a\']]]]]}}" bind:updateTitle="__e" bind:__l="__l"></text-document>'
fxy060608's avatar
fxy060608 已提交
515 516 517
    )
    assertCodegen(
      '<text-document v-for="item in items" :title.sync="item.title"></text-document>',
fxy060608's avatar
fxy060608 已提交
518
      '<block wx:for="{{items}}" wx:for-item="item" wx:for-index="__i0__"><text-document vue-id="{{\'551070e6-1-\'+__i0__}}" title="{{item.title}}" data-event-opts="{{[[\'^updateTitle\',[[\'__set_sync\',[\'$0\',\'title\',\'$event\'],[[[\'items\',\'\',__i0__,\'\']]]]]]]}}" bind:updateTitle="__e" bind:__l="__l"></text-document></block>'
fxy060608's avatar
fxy060608 已提交
519 520 521
    )
    assertCodegen(
      '<text-document v-for="item in items" :title.sync="item.meta.title"></text-document>',
fxy060608's avatar
fxy060608 已提交
522
      '<block wx:for="{{items}}" wx:for-item="item" wx:for-index="__i0__"><text-document vue-id="{{\'551070e6-1-\'+__i0__}}" title="{{item.meta.title}}" data-event-opts="{{[[\'^updateTitle\',[[\'__set_sync\',[\'$0\',\'title\',\'$event\'],[[[\'items\',\'\',__i0__,\'meta\']]]]]]]}}" bind:updateTitle="__e" bind:__l="__l"></text-document></block>'
fxy060608's avatar
fxy060608 已提交
523 524 525 526 527 528
    )
  })

  it('generate v-model directive with generic modifiers', () => {
    assertCodegen(
      '<input v-model.lazy="test">',
fxy060608's avatar
fxy060608 已提交
529
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]]]]]}}" value="{{test}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
530 531 532
    )
    assertCodegen(
      '<input v-model.number="test">',
fxy060608's avatar
fxy060608 已提交
533
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[\'number\']]]]],[\'blur\',[[\'$forceUpdate\']]]]}}" value="{{test}}" bindinput="__e" bindblur="__e"/>'
fxy060608's avatar
fxy060608 已提交
534 535 536
    )
    assertCodegen(
      '<input v-model.number.trim="test">',
fxy060608's avatar
fxy060608 已提交
537
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[\'number\',\'trim\']]]]],[\'blur\',[[\'$forceUpdate\']]]]}}" value="{{test}}" bindinput="__e" bindblur="__e"/>'
fxy060608's avatar
fxy060608 已提交
538 539 540
    )
    assertCodegen(
      '<input class="uni-input" @input="replaceInput" v-model="changeValue" placeholder="连续的两个1会变成2" />',
fxy060608's avatar
fxy060608 已提交
541
      '<input class="uni-input" placeholder="连续的两个1会变成2" data-event-opts="{{[[\'input\',[[\'__set_model\',[\'\',\'changeValue\',\'$event\',[]]],[\'replaceInput\',[\'$event\']]]]]}}" value="{{changeValue}}" bindinput="__e"/>'
542 543 544
    )
    assertCodegen(
      '<input v-model="model2.m" />',
fxy060608's avatar
fxy060608 已提交
545
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'$0\',\'m\',\'$event\',[]],[\'model2\']]]]]}}" value="{{model2.m}}" bindinput="__e"/>'
546 547 548
    )
    assertCodegen(
      '<input v-model="model3.m.m" />',
fxy060608's avatar
fxy060608 已提交
549
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'$0\',\'m\',\'$event\',[]],[\'model3.m\']]]]]}}" value="{{model3.m.m}}" bindinput="__e"/>'
550 551 552
    )
    assertCodegen(
      '<input v-model="model3[a.b][a.b]" />',
fxy060608's avatar
fxy060608 已提交
553
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'$0\',\'$1\',\'$event\',[]],[\'model3.\'+a.b+\'\',\'a.b\']]]]]}}" value="{{model3[a.b][a.b]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
554 555 556 557 558
    )
  })

  it('generate v-for', () => {
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
559 560
      '<view><view v-for="(item, index) in list"><view>{{handle(item)}}</view></view></view>',
      '<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view>{{item.m0}}</view></view></block></view>',
561
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
fxy060608's avatar
fxy060608 已提交
562 563
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
564 565
      '<view><view v-for="(item, index) in list"><view>{{handle(item)}}{{item.title}}</view></view></view>',
      '<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view>{{item.m0+item.$orig.title}}</view></view></block></view>',
566
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
fxy060608's avatar
fxy060608 已提交
567 568
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
569 570
      '<view><view v-for="(item, index) in list"><view v-for="(item1,index1) in list1">{{handle(item1)}}{{item1.title}}</view></view></view>',
      '<view><block wx:for="{{$root.l1}}" wx:for-item="item" wx:for-index="index"><view><block wx:for="{{item.l0}}" wx:for-item="item1" wx:for-index="index1"><view>{{item1.m0+item1.$orig.title}}</view></block></view></block></view>',
571
      'with(this){var l1=__map(list,function(item,index){var $orig=__get_orig(item);var l0=__map(list1,function(item1,index1){var $orig=__get_orig(item1);var m0=handle(item1);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}'
572 573
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
574 575
      '<view v-for="(section, index) in sections" :key="index">title: {{ section.title|prefix }}<view v-for="(sub_titles, _index) in section.sub_titles" :key="_index">{{ sub_titles|prefix }}</view></view>',
      '<block wx:for="{{$root.l1}}" wx:for-item="section" wx:for-index="index" wx:key="index"><view>{{"title: "+section.f0}}<block wx:for="{{section.l0}}" wx:for-item="sub_titles" wx:for-index="_index" wx:key="_index"><view>{{sub_titles.f1}}</view></block></view></block>',
576
      'with(this){var l1=__map(sections,function(section,index){var $orig=__get_orig(section);var f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var $orig=__get_orig(sub_titles);var f1=_f("prefix")(sub_titles);return{$orig:$orig,f1:f1}});return{$orig:$orig,f0:f0,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}'
577 578 579
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
580 581 582
      '<view v-for="(item, index) in list" :key="item">{{aaa.item.id | test | test1}}</view>',
      '<block wx:for="{{list}}" wx:for-item="item" wx:for-index="index" wx:key="*this"><view>{{$root.f0}}</view></block>',
      'with(this){var f0=_f("test1")(_f("test")(aaa.item.id));$mp.data=Object.assign({},{$root:{f0:f0}})}'
583 584
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
585 586
      '<view v-for="(item, index) in list" :key="item">{{item.item.id | test | test1}}</view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="$orig"><view>{{item.f0}}</view></block>',
587
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var f0=_f("test1")(_f("test")(item.item.id));return{$orig:$orig,f0:f0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
588 589
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
590 591
      '<text v-for="(item, i) in list" v-bind:key="i" >{{ item.split(\'\').join(\' \') }}</text>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="i" wx:key="i"><text>{{item.g0}}</text></block>',
592
      'with(this){var l0=__map(list,function(item,i){var $orig=__get_orig(item);var g0=item.split("").join(" ");return{$orig:$orig,g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
593
    )
fxy060608's avatar
fxy060608 已提交
594 595 596 597 598 599 600 601 602
    assertCodegen(
      `<view class="content">
        <view v-for="(item, index) in tabList" :key="index">
            <view v-for="(item2,index2) in list[item.key]" :key="index2" @click="show(item2.id, item2.id)">
                {{formatIt(item2.id)}}
            </view>
        </view>
    </view>`,
      '<view class="content"><block wx:for="{{$root.l1}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:for="{{item.l0}}" wx:for-item="item2" wx:for-index="index2" wx:key="index2"><view data-event-opts="{{[[\'tap\',[[\'show\',[\'$0\',\'$1\'],[[[\'list.\'+item.$orig.key+\'\',\'\',index2,\'id\']],[[\'list.\'+item.$orig.key+\'\',\'\',index2,\'id\']]]]]]]}}" bindtap="__e">{{\'\'+item2.m0+\'\'}}</view></block></view></block></view>',
603
      'with(this){var l1=__map(tabList,function(item,index){var $orig=__get_orig(item);var l0=__map(list[item.key],function(item2,index2){var $orig=__get_orig(item2);var m0=formatIt(item2.id);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}'
fxy060608's avatar
fxy060608 已提交
604
    )
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    assertCodegen(
      '<view><view v-for="(item, key) in {x:\'x\'}"><view>{{item}}</view></view></view>',
      '<view><block wx:for="{{({x:\'x\'})}}" wx:for-item="item" wx:for-index="key"><view><view>{{item}}</view></view></block></view>'
    )
    assertCodegen(
      '<view><view v-for="(item, key) in {\'x-x\':\'x\'}"><view>{{item}}</view></view></view>',
      '<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="key"><view><view>{{item}}</view></view></block></view>',
      'with(this){var l0={"x-x":"x"};$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
    assertCodegen(
      '<view><view v-for="(item, index) in getList(test)"><view>{{item}}</view></view></view>',
      '<view><block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view>{{item}}</view></view></block></view>',
      'with(this){var l0=getList(test);$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
    assertCodegen(
      '<view><view v-for="(item, index) in list[get(test)]"><view>{{item}}</view></view></view>',
      '<view><block wx:for="{{list[$root.m0]}}" wx:for-item="item" wx:for-index="index"><view><view>{{item}}</view></view></block></view>',
      'with(this){var m0=get(test);$mp.data=Object.assign({},{$root:{m0:m0}})}'
    )
fxy060608's avatar
fxy060608 已提交
624 625 626 627 628 629
  })

  it('generate TemplateLiteral ', () => {
    assertCodegen(
      /* eslint-disable no-template-curly-in-string */
      '<view @tap="go(`/pages/page/page?id=${aaa}`)"></view>',
fxy060608's avatar
fxy060608 已提交
630
      '<view data-event-opts="{{[[\'tap\',[[\'go\',[\'/pages/page/page?id=\'+aaa]]]]]}}" bindtap="__e"></view>'
fxy060608's avatar
fxy060608 已提交
631 632 633 634
    )
    assertCodegen(
      /* eslint-disable no-template-curly-in-string */
      '<view :data-test="`hello`+aaa"></view>',
fxy060608's avatar
fxy060608 已提交
635
      '<view data-test="{{\'hello\'+aaa}}"></view>'
fxy060608's avatar
fxy060608 已提交
636 637
    )
  })
fxy060608's avatar
fxy060608 已提交
638 639 640 641 642
  it('generate event ', () => {
    assertCodegen(
      '<view @/>',
      '<view></view>'
    )
643

644
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
645 646
      '<text v-for="item in items[\'metas\']" :key="item[\'id\']" class="title" @tap="handle(item[\'id\'],item[\'title\'])">{{item.title}}</text>',
      '<block wx:for="{{items[\'metas\']}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><text data-event-opts="{{[[\'tap\',[[\'handle\',[\'$0\',\'$1\'],[[[\'items.metas\',\'id\',item[\'id\'],\'id\']],[[\'items.metas\',\'id\',item[\'id\'],\'title\']]]]]]]}}" class="title" bindtap="__e">{{item.title}}</text></block>'
647
    )
fxy060608's avatar
fxy060608 已提交
648
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
649 650
      '<view @touchmove>1</view>',
      '<view data-event-opts="{{[[\'touchmove\',[[\'\',[\'$event\']]]]]}}" bindtouchmove="__e">1</view>'
fxy060608's avatar
fxy060608 已提交
651 652
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
653 654
      '<view @touchmove="">2</view>',
      '<view data-event-opts="{{[[\'touchmove\',[[\'\',[\'$event\']]]]]}}" bindtouchmove="__e">2</view>'
fxy060608's avatar
fxy060608 已提交
655 656
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
657 658
      '<view @touchmove.stop.prevent>3</view>',
      '<view data-event-opts="{{[[\'touchmove\',[[\'\',[\'$event\']]]]]}}" catchtouchmove="__e">3</view>'
fxy060608's avatar
fxy060608 已提交
659 660
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
661 662
      '<view @tap="change(item,item.b,\'a\',true)"></view>',
      '<view data-event-opts="{{[[\'tap\',[[\'change\',[\'$0\',\'$1\',\'a\',true],[\'item\',\'item.b\']]]]]}}" bindtap="__e"></view>'
663 664
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
665 666
      '<view @click="handle({name:\'a\',b:123},[1,2,3])"></view>',
      '<view data-event-opts="{{[[\'tap\',[[\'handle\',[[\'o\',[\'name\',\'a\'],[\'b\',123]],[1,2,3]]]]]]}}" bindtap="__e"></view>'
667 668
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
669 670
      '<text v-for="(item,index) in pickerList[pickerIndex[0][2][\'asdf\']].children" :key="index">{{item.title}}</text>',
      '<block wx:for="{{pickerList[pickerIndex[0][2][\'asdf\']].children}}" wx:for-item="item" wx:for-index="index" wx:key="index"><text>{{item.title}}</text></block>'
671 672
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
673 674
      '<text v-for="(item,index) in pickerList[0][1].children" :key="index" @tap="handle(item,index)">{{item.title}}</text>',
      '<block wx:for="{{pickerList[0][1].children}}" wx:for-item="item" wx:for-index="index" wx:key="index"><text data-event-opts="{{[[\'tap\',[[\'handle\',[\'$0\',index],[[[\'pickerList.__$n0.__$n1.children\',\'\',index]]]]]]]}}" bindtap="__e">{{item.title}}</text></block>'
675 676
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
677 678
      '<view v-for="(v2,i2) in ddd.item[0]" :key="i2" @tap="toInfo(v2)"></view>',
      '<block wx:for="{{ddd.item[0]}}" wx:for-item="v2" wx:for-index="i2" wx:key="i2"><view data-event-opts="{{[[\'tap\',[[\'toInfo\',[\'$0\'],[[[\'ddd.item.__$n0\',\'\',i2]]]]]]]}}" bindtap="__e"></view></block>'
679 680
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
681 682
      '<button type="primary" @click="test(arr, arr[index+1])">click me</button>',
      '<button type="primary" data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\',\'$1\'],[\'arr\',\'arr.\'+index+1+\'\']]]]]}}" bindtap="__e">click me</button>'
683 684
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
685 686
      '<button type="primary" @click="test(arr, arr[0])">click me</button>',
      '<button type="primary" data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\',\'$1\'],[\'arr\',\'arr.__$n0\']]]]]}}" bindtap="__e">click me</button>'
687 688
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
689 690
      '<view v-for="item in list" @click="test(item)">{{ item }}</view>',
      '<block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__"><view data-event-opts="{{[[\'tap\',[[\'test\',[\'$0\'],[[[\'list\',\'\',__i0__]]]]]]]}}" bindtap="__e">{{item}}</view></block>'
691
    )
fxy060608's avatar
fxy060608 已提交
692 693 694 695 696 697 698 699 700 701
  })
  it('generate bool attr', () => {
    assertCodegen(
      '<video id class style inline-template controls/>',
      '<video id controls="{{true}}"></video>'
    )
    assertCodegen(
      '<video controls=""/>',
      '<video controls></video>'
    )
fxy060608's avatar
fxy060608 已提交
702
  })
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  it('generate v-if', () => {
    assertCodegen(
      '<view v-if="show">{{getValue(key)}}</view>',
      '<block wx:if="{{show}}"><view>{{$root.m0}}</view></block>',
      'with(this){var m0=show?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0}})}'
    )
    assertCodegen(
      '<view v-if="getValue(key)">{{getValue(key)}}</view>',
      '<block wx:if="{{$root.m0}}"><view>{{$root.m1}}</view></block>',
      'with(this){var m0=getValue(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}'
    )
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view v-if="item">{{getValue(item)}}</view></view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig}}"><view>{{item.m0}}</view></block></view></block>',
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
719 720 721 722 723
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view v-if="item.length">{{getValue(item)}}</view></view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig.length}}"><view>{{item.m0}}</view></block></view></block>',
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
724 725 726 727 728
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view v-if="item.length>0">{{getValue(item)}}</view></view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig.length>0}}"><view>{{item.m0}}</view></block></view></block>',
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length>0?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view v-if="item.length>0">{{test(\'item\')}}</view></view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.$orig.length>0}}"><view>{{item.m0}}</view></block></view></block>',
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length>0?test("item"):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view v-if="test(item)">{{test(\'item\')}}</view></view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.m0}}"><view>{{item.m1}}</view></block></view></block>',
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=test(item);var m1=m0?test("item"):null;return{$orig:$orig,m0:m0,m1:m1}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view v-if="test(item.id)"><view v-if="test(item.type)">{{test(\'item\')}}</view></view></view>',
      '<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view><block wx:if="{{item.m0}}"><view><block wx:if="{{item.m1}}"><view>{{item.m2}}</view></block></view></block></view></block>',
      'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=test(item.id);var m1=m0?test(item.type):null;var m2=m0&&m1?test("item"):null;return{$orig:$orig,m0:m0,m1:m1,m2:m2}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
    )
744
  })
745
})