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

Q
qiang 已提交
3
function assertCodegen (template, templateCode, renderCode = 'with(this){}', options = {}) {
4
  const res = compiler.compile(template, {
fxy060608's avatar
fxy060608 已提交
5
    resourcePath: 'test.wxml',
Q
qiang 已提交
6
    mp: Object.assign({
fxy060608's avatar
fxy060608 已提交
7 8 9
      minified: true,
      isTest: true,
      platform: 'mp-weixin'
Q
qiang 已提交
10
    }, options)
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  })

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

describe('mp:compiler', () => {
  // TODO
  // it('generate directive', () => {
  //   assertCodegen(
  //     '<view v-custom1:arg1.modifier="value1" v-custom2></view>',
  //     `with(this){return _c('view',{directives:[{name:"custom1",rawName:"v-custom1:arg1.modifier",value:(value1),expression:"value1",arg:"arg1",modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}]})}`
  //   )
  // })

  it('generate filters', () => {
    assertCodegen(
      '<view :id="a | b | c">text {{ d | e | f }} text</view>',
fxy060608's avatar
fxy060608 已提交
29 30
      '<view id="{{$root.f0}}">{{"text "+$root.f1+" text"}}</view>',
      'with(this){var f0=_f("c")(_f("b")(a));var f1=_f("f")(_f("e")(d));$mp.data=Object.assign({},{$root:{f0:f0,f1:f1}})}'
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36
    )
  })

  it('generate filters with no arguments', () => {
    assertCodegen(
      '<view>{{ d | e() }}</view>',
fxy060608's avatar
fxy060608 已提交
37 38
      '<view>{{$root.f0}}</view>',
      'with(this){var f0=_f("e")(d);$mp.data=Object.assign({},{$root:{f0:f0}})}'
fxy060608's avatar
fxy060608 已提交
39 40 41 42 43 44
    )
  })

  it('generate v-for directive', () => {
    assertCodegen(
      '<view><view v-for="item in items" :key="item.uid"></view></view>',
fxy060608's avatar
fxy060608 已提交
45
      '<view><block wx:for="{{items}}" wx:for-item="item" wx:for-index="__i0__" wx:key="uid"><view></view></block></view>'
fxy060608's avatar
fxy060608 已提交
46 47 48 49
    )
    // iterator syntax
    assertCodegen(
      '<view><view v-for="(item, i) in items"></view></view>',
fxy060608's avatar
fxy060608 已提交
50
      '<view><block wx:for="{{items}}" wx:for-item="item" wx:for-index="i"><view></view></block></view>'
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    )
    // TODO
    // assertCodegen(
    //   '<view><view v-for="(item, key, index) in items"></view></view>',
    //   `with(this){return _c('view',_l((items),function(item,key,index){return _c('view')}),0)}`
    // )
    // destructuring
    // TODO
    // assertCodegen(
    //   '<view><view v-for="{ a, b } in items"></view></view>',
    //   `<view><view wx:for="{{items}}" wx:for-item="{{a}}" wx:for-index="{{b}}"></view></view>`
    // )
    // TODO
    // assertCodegen(
    //   '<view><view v-for="({ a, b }, key, index) in items"></view></view>',
    //   `with(this){return _c('view',_l((items),function({ a, b },key,index){return _c('view')}),0)}`
    // )
    // v-for with extra element
    assertCodegen(
      '<view><view></view><view v-for="item in items"></view></view>',
fxy060608's avatar
fxy060608 已提交
71
      '<view><view></view><block wx:for="{{items}}" wx:for-item="item" wx:for-index="__i0__"><view></view></block></view>'
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77
    )
  })

  it('generate v-if directive', () => {
    assertCodegen(
      '<view v-if="show">hello</view>',
fxy060608's avatar
fxy060608 已提交
78
      '<block wx:if="{{show}}"><view>hello</view></block>'
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83 84
    )
  })

  it('generate v-else directive', () => {
    assertCodegen(
      '<view><view v-if="show">hello</view><view v-else>world</view></view>',
fxy060608's avatar
fxy060608 已提交
85
      '<view><block wx:if="{{show}}"><view>hello</view></block><block wx:else><view>world</view></block></view>'
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91
    )
  })

  it('generate v-else-if directive', () => {
    assertCodegen(
      '<view><view v-if="show">hello</view><view v-else-if="hide">world</view></view>',
fxy060608's avatar
fxy060608 已提交
92
      '<view><block wx:if="{{show}}"><view>hello</view></block><block wx:else><block wx:if="{{hide}}"><view>world</view></block></block></view>'
fxy060608's avatar
fxy060608 已提交
93 94 95 96 97 98
    )
  })

  it('generate v-else-if with v-else directive', () => {
    assertCodegen(
      '<view><view v-if="show">hello</view><view v-else-if="hide">world</view><view v-else>bye</view></view>',
fxy060608's avatar
fxy060608 已提交
99
      '<view><block wx:if="{{show}}"><view>hello</view></block><block wx:else><block wx:if="{{hide}}"><view>world</view></block><block wx:else><view>bye</view></block></block></view>'
fxy060608's avatar
fxy060608 已提交
100 101 102 103 104 105
    )
  })

  it('generate multi v-else-if with v-else directive', () => {
    assertCodegen(
      '<view><view v-if="show">hello</view><view v-else-if="hide">world</view><view v-else-if="3">elseif</view><view v-else>bye</view></view>',
fxy060608's avatar
fxy060608 已提交
106
      '<view><block wx:if="{{show}}"><view>hello</view></block><block wx:else><block wx:if="{{hide}}"><view>world</view></block><block wx:else><block wx:if="{{3}}"><view>elseif</view></block><block wx:else><view>bye</view></block></block></block></view>'
fxy060608's avatar
fxy060608 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
    )
  })

  it('generate ref', () => {
    assertCodegen(
      '<view ref="component1"></view>',
      '<view data-ref="component1" class="vue-ref"></view>'
    )
  })

  it('generate ref on v-for', () => {
    assertCodegen(
      '<ul><li v-for="item in items" ref="component1"></li></ul>',
fxy060608's avatar
fxy060608 已提交
120
      '<view class="_ul"><block wx:for="{{items}}" wx:for-item="item" wx:for-index="__i0__"><view data-ref="component1" class="_li vue-ref-in-for"></view></block></view>'
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    )
  })

  // it('generate v-bind directive', () => {
  //   assertCodegen(
  //     '<view v-bind="test"></view>',
  //     `<view v-bind="test"></view>`
  //   )
  // })

  // it('generate v-bind with prop directive', () => {
  //   assertCodegen(
  //     '<view v-bind.prop="test"></view>',
  //     `with(this){return _c('view',_b({},'view',test,true))}`
  //   )
  // })

  // it('generate v-bind directive with sync modifier', () => {
  //   assertCodegen(
  //     '<view v-bind.sync="test"></view>',
  //     `with(this){return _c('view',_b({},'view',test,false,true))}`
  //   )
  // })

  it('generate v-model directive', () => {
    assertCodegen(
      '<input v-model="test">',
fxy060608's avatar
fxy060608 已提交
148
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]]]]]}}" value="{{test}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
149 150 151 152 153 154
    )
  })

  it('generate multiline v-model directive', () => {
    assertCodegen(
      '<input v-model="\n test \n">',
fxy060608's avatar
fxy060608 已提交
155
      '<input data-event-opts="{{[[\'input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]]]]]}}" value="{{test}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
156 157 158 159 160 161
    )
  })

  it('generate multiline v-model directive on custom component', () => {
    assertCodegen(
      '<my-component v-model="\n test \n" />',
fxy060608's avatar
fxy060608 已提交
162
      '<my-component bind:input="__e" vue-id="551070e6-1" value="{{test}}" data-event-opts="{{[[\'^input\',[[\'__set_model\',[\'\',\'test\',\'$event\',[]]]]]]}}" bind:__l="__l"></my-component>'
fxy060608's avatar
fxy060608 已提交
163 164 165 166 167 168
    )
  })

  it('generate template tag', () => {
    assertCodegen(
      '<view><template><view>{{hello}}</view></template></view>',
fxy060608's avatar
fxy060608 已提交
169
      '<view><view>{{hello}}</view></view>'
fxy060608's avatar
fxy060608 已提交
170 171 172 173
    )
  })

  it('generate single slot', () => {
fxy060608's avatar
fxy060608 已提交
174
    assertCodegen('<view><slot></slot></view>', '<view><slot></slot></view>')
175 176
    assertCodegen('<view><slot>default</slot></view>', '<view><block wx:if="{{$slots.default}}"><slot></slot></block><block wx:else>default</block></view>')
    assertCodegen('<view><slot>{{hello}}</slot></view>', '<view><block wx:if="{{$slots.default}}"><slot></slot></block><block wx:else>{{hello}}</block></view>')
177
    assertCodegen('<view><slot name="default"></slot></view>', '<view><slot></slot></view>')
fxy060608's avatar
fxy060608 已提交
178 179 180 181 182
  })

  it('generate named slot', () => {
    assertCodegen(
      '<view><slot name="one"></slot></view>',
fxy060608's avatar
fxy060608 已提交
183
      '<view><slot name="one"></slot></view>'
fxy060608's avatar
fxy060608 已提交
184
    )
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    assertCodegen(
      '<view><slot :name="one"></slot></view>',
      '<view><slot name="{{one}}"></slot></view>'
    )
    assertCodegen(
      '<view><slot :name="one+\'test\'"></slot></view>',
      '<view><slot name="{{one+\'test\'}}"></slot></view>'
    )
    assertCodegen(
      '<view><slot :name="one" :test="test"></slot></view>',
      '<view><slot name="{{one}}"></slot></view>',
      'with(this){{$setScopedSlotsParams(one,{"test":test})}}',
      {
        scopedSlotsCompiler: 'augmented'
      }
    )
    assertCodegen(
      '<view><slot :name="one">text</slot></view>',
      '<view><block wx:if="{{$slots[one]}}"><slot name="{{one}}"></slot></block><block wx:else>text</block></view>'
    )
    assertCodegen(
      '<view><slot :name="one+\'test\'">text</slot></view>',
      '<view><block wx:if="{{$slots[one+\'test\']}}"><slot name="{{one+\'test\'}}"></slot></block><block wx:else>text</block></view>'
    )
fxy060608's avatar
fxy060608 已提交
209 210 211 212 213 214 215 216 217 218 219 220
  })

  // it('generate slot fallback content', () => {
  //   assertCodegen(
  //     '<view><slot><view>hi</view></slot></view>',
  //     `with(this){return _c('view',[_t("default",[_c('view',[_v("hi")])])],2)}`
  //   )
  // })

  it('generate slot target', () => {
    assertCodegen(
      '<view slot="one">hello world</view>',
fxy060608's avatar
fxy060608 已提交
221
      '<view slot="one">hello world</view>'
fxy060608's avatar
fxy060608 已提交
222
    )
223 224 225 226
    assertCodegen(
      '<view slot="default">hello world</view>',
      '<view>hello world</view>'
    )
fxy060608's avatar
fxy060608 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  })

  // it('generate scoped slot', () => {
  //   assertCodegen(
  //     '<foo><template slot-scope="bar">{{ bar }}</template></foo>',
  //     `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return [_v(_s(bar))]}}])})}`
  //   )
  //   assertCodegen(
  //     '<foo><view slot-scope="bar">{{ bar }}</view></foo>',
  //     `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return _c('view',{},[_v(_s(bar))])}}])})}`
  //   )
  // })

  // it('generate named scoped slot', () => {
  //   assertCodegen(
  //     '<foo><template slot="foo" slot-scope="bar">{{ bar }}</template></foo>',
  //     `with(this){return _c('foo',{scopedSlots:_u([{key:"foo",fn:function(bar){return [_v(_s(bar))]}}])})}`
  //   )
  //   assertCodegen(
  //     '<foo><view slot="foo" slot-scope="bar">{{ bar }}</view></foo>',
  //     `with(this){return _c('foo',{scopedSlots:_u([{key:"foo",fn:function(bar){return _c('view',{},[_v(_s(bar))])}}])})}`
  //   )
  // })

  // it('generate scoped slot with multiline v-if', () => {
  //   assertCodegen(
  //     '<foo><template v-if="\nshow\n" slot-scope="bar">{{ bar }}</template></foo>',
  //     `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return (\nshow\n)?[_v(_s(bar))]:undefined}}])})}`
  //   )
  //   assertCodegen(
  //     '<foo><view v-if="\nshow\n" slot="foo" slot-scope="bar">{{ bar }}</view></foo>',
  //     `with(this){return _c(\'foo\',{scopedSlots:_u([{key:"foo",fn:function(bar){return (\nshow\n)?_c(\'view\',{},[_v(_s(bar))]):_e()}}])})}`
  //   )
  // })

  it('generate class binding', () => {
    // static
    assertCodegen(
      '<view class="class1">hello world</view>',
fxy060608's avatar
fxy060608 已提交
266
      '<view class="class1">hello world</view>'
fxy060608's avatar
fxy060608 已提交
267 268 269 270
    )
    // dynamic
    assertCodegen(
      '<view :class="class1">hello world</view>',
fxy060608's avatar
fxy060608 已提交
271
      '<view class="{{[class1]}}">hello world</view>'
fxy060608's avatar
fxy060608 已提交
272 273 274 275 276 277 278 279
    )
    //     assertCodegen(
    //       '<view :class="class1">hello world</view>',
    //       `<view class="{{$root.c0}}">hello world</view>`,
    //       `with(this){var c0=__get_class(class1);$mp.data=Object.assign({},{$root:{c0:c0}})}`
    //     )
  })

Q
qiang 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
  it('generate attrs with mergeVirtualHostAttributes', () => {
    assertCodegen(
      '<view>hello world</view>',
      '<view class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view><view>hello world</view></view>',
      '<view class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}"><view>hello world</view></view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<custom-view>hello world</custom-view>',
      '<custom-view vue-id="551070e6-1" class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}" bind:__l="__l" virtualHostStyle="{{virtualHostStyle}}" virtualHostClass="{{[virtualHostClass]}}" vue-slots="{{[\'default\']}}">hello world</custom-view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<custom-view :class="class1" :style="style">hello world</custom-view>',
      '<custom-view class="{{[class1,virtualHostClass]}}" style="{{(style)+virtualHostStyle}}" vue-id="551070e6-1" bind:__l="__l" virtualHostStyle="{{(style)+virtualHostStyle}}" virtualHostClass="{{[class1,virtualHostClass]}}" vue-slots="{{[\'default\']}}">hello world</custom-view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view><custom-view>hello world</custom-view></view>',
      '<view class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}"><custom-view vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}">hello world</custom-view></view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view><custom-view :class="class1" :style="style">hello world</custom-view></view>',
      '<view class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}"><custom-view class="{{[class1]}}" style="{{(style)}}" vue-id="551070e6-1" bind:__l="__l" virtualHostStyle="{{(style)}}" virtualHostClass="{{[class1]}}" vue-slots="{{[\'default\']}}">hello world</custom-view></view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
  })

  it('generate class with mergeVirtualHostAttributes', () => {
    assertCodegen(
      '<view class="a">hello world</view>',
      '<view class="{{[\'a\',virtualHostClass]}}" style="{{virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view :class="class1">hello world</view>',
      '<view class="{{[class1,virtualHostClass]}}" style="{{virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view :class="[class1,class2]">hello world</view>',
      '<view class="{{[class1,class2,virtualHostClass]}}" style="{{virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view :class="{class1,class2}">hello world</view>',
      '<view class="{{[(class1)?\'class1\':\'\',(class2)?\'class2\':\'\',virtualHostClass]}}" style="{{virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view class="a external-class c" :class="class1">hello world</view>',
      '<view class="{{[\'a\',\'external-class\',\'c\',class1,virtualHostClass]}}" style="{{virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view class="a external-class c" :class="class1"><view class="a external-class c" :class="class1">hello world</view></view>',
      '<view class="{{[\'a\',\'external-class\',\'c\',class1,virtualHostClass]}}" style="{{virtualHostStyle}}"><view class="{{[\'a\',\'external-class\',\'c\',class1]}}">hello world</view></view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
  })

fxy060608's avatar
fxy060608 已提交
382 383 384
  it('generate staticStyle', () => {
    assertCodegen(
      '<view style="height:400upx">hello world</view>',
fxy060608's avatar
fxy060608 已提交
385
      '<view style="height:400rpx;">hello world</view>'
fxy060608's avatar
fxy060608 已提交
386 387 388 389 390 391
    )
  })

  it('generate style binding', () => {
    assertCodegen(
      '<view :style="error">hello world</view>',
fxy060608's avatar
fxy060608 已提交
392
      '<view style="{{(error)}}">hello world</view>'
fxy060608's avatar
fxy060608 已提交
393 394 395 396 397 398 399 400
    )
    //     assertCodegen(
    //       '<view :style="error">hello world</view>',
    //       `<view style="{{$root.s0}}">hello world</view>`,
    //       `with(this){var s0=__get_style(error);$mp.data=Object.assign({},{$root:{s0:s0}})}`
    //     )
  })

Q
qiang 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  it('generate style with mergeVirtualHostAttributes', () => {
    assertCodegen(
      '<view style="color:red">hello world</view>',
      '<view class="{{[virtualHostClass]}}" style="{{\'color:red;\'+virtualHostStyle}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view :style="style">hello world</view>',
      '<view style="{{(style)+virtualHostStyle}}" class="{{[virtualHostClass]}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
    assertCodegen(
      '<view :style="{color}">hello world</view>',
      '<view style="{{\'color:\'+(color)+\';\'+virtualHostStyle}}" class="{{[virtualHostClass]}}">hello world</view>',
      'with(this){}',
      {
        mergeVirtualHostAttributes: true
      }
    )
  })

fxy060608's avatar
fxy060608 已提交
428 429 430
  it('generate v-show directive', () => {
    assertCodegen(
      '<view v-show="shown">hello world</view>',
fxy060608's avatar
fxy060608 已提交
431
      '<view hidden="{{!(shown)}}">hello world</view>'
fxy060608's avatar
fxy060608 已提交
432 433 434 435 436
    )
  })

  it('generate DOM props with v-bind directive', () => {
    // input + value
fxy060608's avatar
fxy060608 已提交
437
    assertCodegen('<input :value="msg">', '<input value="{{msg}}"/>')
fxy060608's avatar
fxy060608 已提交
438
    // non input
fxy060608's avatar
fxy060608 已提交
439
    assertCodegen('<view :value="msg"/>', '<view value="{{msg}}"></view>')
fxy060608's avatar
fxy060608 已提交
440 441 442
  })

  it('generate attrs with v-bind directive', () => {
fxy060608's avatar
fxy060608 已提交
443
    assertCodegen('<input :name="field1">', '<input name="{{field1}}"/>')
fxy060608's avatar
fxy060608 已提交
444 445 446
  })

  it('generate static attrs', () => {
fxy060608's avatar
fxy060608 已提交
447
    assertCodegen('<input name="field1">', '<input name="field1"/>')
fxy060608's avatar
fxy060608 已提交
448 449 450 451 452
  })

  it('generate events with v-on directive', () => {
    assertCodegen(
      '<input @input="onInput">',
fxy060608's avatar
fxy060608 已提交
453
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
454 455 456 457 458 459
    )
  })

  it('generate events with method call', () => {
    assertCodegen(
      '<input @input="onInput($event);">',
fxy060608's avatar
fxy060608 已提交
460
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
461 462 463 464
    )
    // empty arguments
    assertCodegen(
      '<input @input="onInput();">',
fxy060608's avatar
fxy060608 已提交
465
      '<input data-event-opts="{{[[\'input\',[[\'onInput\']]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
466 467 468 469
    )
    // without semicolon
    assertCodegen(
      '<input @input="onInput($event)">',
fxy060608's avatar
fxy060608 已提交
470
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
471 472 473 474
    )
    // multiple args
    assertCodegen(
      '<input @input="onInput($event, \'abc\', 5);">',
fxy060608's avatar
fxy060608 已提交
475
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\',\'abc\',5]]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
476 477 478 479
    )
    // expression in args
    assertCodegen(
      '<input @input="onInput($event, 2+2);">',
fxy060608's avatar
fxy060608 已提交
480
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\',2+2]]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
481
    )
482 483 484
    // v-for
    assertCodegen(
      '<view v-for="(item,index) in list" :key="index"><view @click="$test.test(item)">test</view></view>',
Q
qiang 已提交
485 486
      '<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">test</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)}}}'
487
    )
fxy060608's avatar
fxy060608 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    // tricky symbols in args
    //         assertCodegen(
    //             `<input @input="onInput(');[\\'());');">`,
    //             `<input data-event-id="e0" bindinput="__e"/>`,
    //             `with(this){if(!$mp.events){$mp.events=__get_event({"e0":{on:{"input":function($event){onInput(");['());")}}}})}}`
    //         )
  })

  //     it('generate events with multiple statements', () => {
  //         // normal function
  //         assertCodegen(
  //             '<input @input="onInput1();onInput2()">',
  //             `<input data-event-id="e0" bindinput="__e"/>`,
  //             `with(this){if(!$mp.events){$mp.events=__get_event({"e0":{on:{"input":function($event){onInput1();onInput2()}}}})}}`
  //         )
  //         // function with multiple args
  //         assertCodegen(
  //             '<input @input="onInput1($event, \'text\');onInput2(\'text2\', $event)">',
  //             `<input data-event-id="e0" bindinput="__e"/>`,
  //             `with(this){if(!$mp.events){$mp.events=__get_event({"e0":{on:{"input":function($event){onInput1($event,"text");onInput2("text2",$event)}}}})}}`
  //         )
  //     })

  //     it('generate events with keycode', () => {
  //         assertCodegen(
  //             '<input @input.enter="onInput">',
  //             `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}`
  //         )
  //         // multiple keycodes (delete)
  //         assertCodegen(
  //             '<input @input.delete="onInput">',
  //             `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
  //         )
  //         // multiple keycodes (esc)
  //         assertCodegen(
  //             '<input @input.esc="onInput">',
  //             `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}`
  //         )
  //         // multiple keycodes (space)
  //         assertCodegen(
  //             '<input @input.space="onInput">',
  //             `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}`
  //         )
  //         // multiple keycodes (chained)
  //         assertCodegen(
  //             '<input @keydown.enter.delete="onInput">',
  //             `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
  //         )
  //         // number keycode
  //         assertCodegen(
  //             '<input @input.13="onInput">',
  //             `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&$event.keyCode!==13)return null;return onInput($event)}}})}`
  //         )
  //         // custom keycode
  //         assertCodegen(
  //             '<input @input.custom="onInput">',
  //             `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}`
  //         )
  //     })

  it('generate events with generic modifiers', () => {
    assertCodegen(
      '<input @input.stop="onInput">',
fxy060608's avatar
fxy060608 已提交
551
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" catchinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
552 553 554
    )
    assertCodegen(
      '<input @input.prevent="onInput">',
fxy060608's avatar
fxy060608 已提交
555
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
556 557 558
    )
    assertCodegen(
      '<input @input.self="onInput">',
fxy060608's avatar
fxy060608 已提交
559
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
560 561 562 563 564 565 566
    )
  })

  // GitHub Issues #5146
  it('generate events with generic modifiers and keycode correct order', () => {
    assertCodegen(
      '<input @keydown.enter.prevent="onInput">',
fxy060608's avatar
fxy060608 已提交
567
      '<input data-event-opts="{{[[\'keydown\',[[\'onInput\',[\'$event\']]]]]}}" bindkeydown="__e"/>'
fxy060608's avatar
fxy060608 已提交
568 569 570 571
    )

    assertCodegen(
      '<input @keydown.enter.stop="onInput">',
fxy060608's avatar
fxy060608 已提交
572
      '<input data-event-opts="{{[[\'keydown\',[[\'onInput\',[\'$event\']]]]]}}" catchkeydown="__e"/>'
fxy060608's avatar
fxy060608 已提交
573 574 575 576 577 578
    )
  })

  it('generate events with mouse event modifiers', () => {
    assertCodegen(
      '<input @click.ctrl="onClick">',
fxy060608's avatar
fxy060608 已提交
579
      '<input data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]]]}}" bindtap="__e"/>'
fxy060608's avatar
fxy060608 已提交
580 581 582
    )
    assertCodegen(
      '<input @click.shift="onClick">',
fxy060608's avatar
fxy060608 已提交
583
      '<input data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]]]}}" bindtap="__e"/>'
fxy060608's avatar
fxy060608 已提交
584 585 586
    )
    assertCodegen(
      '<input @click.alt="onClick">',
fxy060608's avatar
fxy060608 已提交
587
      '<input data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]]]}}" bindtap="__e"/>'
fxy060608's avatar
fxy060608 已提交
588 589 590
    )
    assertCodegen(
      '<input @click.meta="onClick">',
fxy060608's avatar
fxy060608 已提交
591
      '<input data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]]]}}" bindtap="__e"/>'
fxy060608's avatar
fxy060608 已提交
592 593 594
    )
    assertCodegen(
      '<input @click.exact="onClick">',
fxy060608's avatar
fxy060608 已提交
595
      '<input data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]]]}}" bindtap="__e"/>'
fxy060608's avatar
fxy060608 已提交
596 597 598
    )
    assertCodegen(
      '<input @click.ctrl.exact="onClick">',
fxy060608's avatar
fxy060608 已提交
599
      '<input data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]]]}}" bindtap="__e"/>'
fxy060608's avatar
fxy060608 已提交
600 601 602 603 604 605
    )
  })

  it('generate events with multiple modifiers', () => {
    assertCodegen(
      '<input @input.stop.prevent.self="onInput">',
fxy060608's avatar
fxy060608 已提交
606
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" catchinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
607 608 609 610 611 612
    )
  })

  it('generate events with capture modifier', () => {
    assertCodegen(
      '<input @input.capture="onInput">',
fxy060608's avatar
fxy060608 已提交
613
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" capture-bind:input="__e"/>'
fxy060608's avatar
fxy060608 已提交
614 615 616 617 618 619
    )
  })

  it('generate events with once modifier', () => {
    assertCodegen(
      '<input @input.once="onInput">',
fxy060608's avatar
fxy060608 已提交
620
      '<input data-event-opts="{{[[\'~input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
621 622 623 624 625 626
    )
  })

  it('generate events with capture and once modifier', () => {
    assertCodegen(
      '<input @input.capture.once="onInput">',
fxy060608's avatar
fxy060608 已提交
627
      '<input data-event-opts="{{[[\'~input\',[[\'onInput\',[\'$event\']]]]]}}" capture-bind:input="__e"/>'
fxy060608's avatar
fxy060608 已提交
628 629 630 631 632 633
    )
  })

  it('generate events with once and capture modifier', () => {
    assertCodegen(
      '<input @input.once.capture="onInput">',
fxy060608's avatar
fxy060608 已提交
634
      '<input data-event-opts="{{[[\'~input\',[[\'onInput\',[\'$event\']]]]]}}" capture-bind:input="__e"/>'
fxy060608's avatar
fxy060608 已提交
635 636 637 638 639 640
    )
  })

  it('generate events with inline statement', () => {
    assertCodegen(
      '<input @input="current++">',
fxy060608's avatar
fxy060608 已提交
641 642
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=function($event){current++}}}'
fxy060608's avatar
fxy060608 已提交
643 644 645 646 647 648 649
    )
  })

  it('generate events with inline function expression', () => {
    // normal function
    assertCodegen(
      '<input @input="function () { current++ }">',
fxy060608's avatar
fxy060608 已提交
650 651
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=function(){current++}}}'
fxy060608's avatar
fxy060608 已提交
652 653 654
    )
    // normal named function
    assertCodegen(
655
      '<input @input="function fn () { current++ }">',
fxy060608's avatar
fxy060608 已提交
656 657
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=function fn(){current++}}}'
fxy060608's avatar
fxy060608 已提交
658 659 660 661 662
    )

    // arrow with no args
    assertCodegen(
      '<input @input="()=>current++">',
fxy060608's avatar
fxy060608 已提交
663 664
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=()=>current++}}'
fxy060608's avatar
fxy060608 已提交
665 666 667 668
    )
    // arrow with parens, single arg
    assertCodegen(
      '<input @input="(e) => current++">',
fxy060608's avatar
fxy060608 已提交
669 670
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=e=>current++}}'
fxy060608's avatar
fxy060608 已提交
671 672 673 674
    )
    // arrow with parens, multi args
    assertCodegen(
      '<input @input="(a, b, c) => current++">',
fxy060608's avatar
fxy060608 已提交
675 676
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=(a,b,c)=>current++}}'
fxy060608's avatar
fxy060608 已提交
677 678 679 680
    )
    // arrow with destructuring
    assertCodegen(
      '<input @input="({ a, b }) => current++">',
fxy060608's avatar
fxy060608 已提交
681 682
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=({a,b})=>current++}}'
fxy060608's avatar
fxy060608 已提交
683 684 685 686
    )
    // arrow single arg no parens
    assertCodegen(
      '<input @input="e=>current++">',
fxy060608's avatar
fxy060608 已提交
687 688
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" bindinput="__e"/>',
      'with(this){if(!_isMounted){e0=e=>current++}}'
fxy060608's avatar
fxy060608 已提交
689 690 691
    )
    // with modifiers
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
692 693 694
      '<input @input.stop="e=>current++">',
      '<input data-event-opts="{{[[\'input\',[[\'e0\',[\'$event\']]]]]}}" catchinput="__e"/>',
      'with(this){if(!_isMounted){e0=function($event){$event.stopPropagation();return(e=>current++)($event)}}}'
fxy060608's avatar
fxy060608 已提交
695 696 697 698 699 700 701
    )
  })

  // #3893
  it('should not treat handler with unexpected whitespace as inline statement', () => {
    assertCodegen(
      '<input @input=" onInput ">',
fxy060608's avatar
fxy060608 已提交
702
      '<input data-event-opts="{{[[\'input\',[[\'onInput\',[\'$event\']]]]]}}" bindinput="__e"/>'
fxy060608's avatar
fxy060608 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    )
  })

  //     it('generate unhandled events', () => {
  //         assertCodegen(
  //             '<input @input="current++">',
  //             `with(this){return _c('input',{on:{"input":function(){}}})}`,
  //             ast => {
  //                 ast.events.input = undefined
  //             }
  //         )
  //     })

  //     it('generate multiple event handlers', () => {
  //         assertCodegen(
  //             '<input @input="current++" @input.stop="onInput">',
  //             `<input data-event-id="e0" catchinput="__e"/>`,
  //             `with(this){if(!$mp.events){$mp.events=__get_event({"e0":{on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}}})}}`
  //         )
  //     })

  it('generate component', () => {
    assertCodegen(
      '<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
fxy060608's avatar
fxy060608 已提交
727
      '<my-component vue-id="551070e6-1" name="mycomponent1" msg="{{msg}}" data-event-opts="{{[[\'^notify\',[[\'onNotify\']]]]}}" bind:notify="__e" bind:__l="__l" vue-slots="{{[\'default\']}}"><view class="_div">hi</view></my-component>'
fxy060608's avatar
fxy060608 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741
      // `with(this){if(!$mp.events){$mp.events=__get_event({"e0":{on:{"notify":onNotify},component:true}})}}`
    )
  })

  //     it('generate svg component with children', () => {
  //         assertCodegen(
  //             '<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
  //             `<svg><my-comp><circle r="{{10}}"></circle></my-comp></svg>`
  //         )
  //     })

  it('generate is attribute', () => {
    assertCodegen(
      '<div is="component1"></div>',
fxy060608's avatar
fxy060608 已提交
742
      '<component1 vue-id="551070e6-1" bind:__l="__l"></component1>'
fxy060608's avatar
fxy060608 已提交
743 744 745 746 747 748 749 750
    )
    //         assertCodegen(
    //             '<div :is="component1"></div>',
    //             '<view is="{{component1}}"></view>'
    //         )
    // maybe a component and normalize type should be 1
    assertCodegen(
      '<div><div is="component1"></div></div>',
fxy060608's avatar
fxy060608 已提交
751
      '<view class="_div"><component1 vue-id="551070e6-1" bind:__l="__l"></component1></view>'
fxy060608's avatar
fxy060608 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    )
  })

  //     it('generate component with inline-template', () => {
  //       // have "inline-template'"
  //       assertCodegen(
  //         '<my-component inline-template><p><span>hello world</span></p></my-component>',
  //         `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _m(0)}},staticRenderFns:[function(){with(this){return _c('p',[_c('span',[_v("hello world")])])}}]}})}`
  //       )
  //       // "have inline-template attrs, but not having exactly one child element
  //       assertCodegen(
  //         '<my-component inline-template><hr><hr></my-component>',
  //         `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _c('hr')}},staticRenderFns:[]}})}`
  //       )
  //       try {
  //         assertCodegen(
  //           '<my-component inline-template></my-component>',
  //           ''
  //         )
  //       } catch (e) {}
  //       expect('Inline-template components must have exactly one child element.').toHaveBeenWarned()
  //       expect(console.error.calls.count()).toBe(2)
  //     })

  it('generate static trees inside v-for', () => {
    // TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
779 780
      '<div><div v-for="i in 10"><p><span></span></p></div></div>',
      '<view class="_div"><block wx:for="{{10}}" wx:for-item="i" wx:for-index="__i0__"><view class="_div"><view class="_p"><label class="_span"></label></view></view></block></view>'
fxy060608's avatar
fxy060608 已提交
781 782 783 784 785 786 787
    )
  })

  it('generate component with v-for', () => {
    // normalize type: 2
    assertCodegen(
      '<div><child></child><template v-for="item in list">{{ item }}</template></div>',
fxy060608's avatar
fxy060608 已提交
788
      '<view class="_div"><child vue-id="551070e6-1" bind:__l="__l"></child><block wx:for="{{list}}" wx:for-item="item" wx:for-index="__i0__">{{item}}</block></view>'
fxy060608's avatar
fxy060608 已提交
789 790 791 792 793 794
    )
  })

  it('generate component with comment', () => {
    assertCodegen(
      '<div><!--comment--></div>',
fxy060608's avatar
fxy060608 已提交
795
      '<view class="_div"></view>'
fxy060608's avatar
fxy060608 已提交
796 797 798 799 800 801
    )
  })
  // #6150
  it('generate comments with special characters', () => {
    assertCodegen(
      '<div><!--\n\'comment\'\n--></div>',
fxy060608's avatar
fxy060608 已提交
802
      '<view class="_div"></view>'
fxy060608's avatar
fxy060608 已提交
803 804 805 806 807 808
    )
  })
  // #8041
  it('does not squash templates inside v-pre', () => {
    assertCodegen(
      '<div v-pre><template><p>{{msg}}</p></template></div>',
fxy060608's avatar
fxy060608 已提交
809
      '<view class="_div"><view class="_p">{{msg}}</view></view>'
fxy060608's avatar
fxy060608 已提交
810 811 812 813 814 815
    )
  })

  it('not specified ast type', () => {
    assertCodegen(
      '',
fxy060608's avatar
fxy060608 已提交
816
      '<view class="_div"></view>'
fxy060608's avatar
fxy060608 已提交
817 818 819 820 821 822
    )
  })

  it('not specified directives option', () => {
    assertCodegen(
      '<p v-if="show">hello world</p>',
fxy060608's avatar
fxy060608 已提交
823
      '<block wx:if="{{show}}"><view class="_p">hello world</view></block>'
fxy060608's avatar
fxy060608 已提交
824 825 826 827 828 829
    )
  })

  // #9142
  it('should compile single v-for component inside template', () => {
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
830 831
      '<div><template v-if="ok"><foo v-for="i in 1" :key="i"></foo></template></div>',
      '<view class="_div"><block wx:if="{{ok}}"><block wx:for="{{1}}" wx:for-item="i" wx:for-index="__i0__" wx:key="*this"><foo vue-id="{{\'551070e6-1-\'+__i0__}}" bind:__l="__l"></foo></block></block></view>'
fxy060608's avatar
fxy060608 已提交
832 833
    )
  })
834
})