codegen.spec.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import { generate } from 'compiler/codegen'
  4. import { isObject, isFunction, extend } from 'shared/util'
  5. import { isReservedTag } from 'web/util/index'
  6. import { baseOptions } from 'web/compiler/options'
  7. import { BindingTypes } from '../../../../packages/compiler-sfc/src/types'
  8. function assertCodegen(template, generatedCode, ...args) {
  9. let staticRenderFnCodes: string[] = []
  10. let generateOptions = baseOptions
  11. let proc: Function | null = null
  12. let len = args.length
  13. while (len--) {
  14. const arg = args[len]
  15. if (Array.isArray(arg)) {
  16. staticRenderFnCodes = arg
  17. } else if (isObject(arg)) {
  18. generateOptions = arg
  19. } else if (isFunction(arg)) {
  20. proc = arg
  21. }
  22. }
  23. const ast = parse(template, baseOptions)
  24. optimize(ast, baseOptions)
  25. proc && proc(ast)
  26. const res = generate(ast, generateOptions)
  27. expect(res.render).toBe(generatedCode)
  28. expect(res.staticRenderFns).toEqual(staticRenderFnCodes)
  29. }
  30. describe('codegen', () => {
  31. it('generate directive', () => {
  32. assertCodegen(
  33. '<p v-custom1:arg1.modifier="value1" v-custom2></p>',
  34. `with(this){return _c('p',{directives:[{name:"custom1",rawName:"v-custom1:arg1.modifier",value:(value1),expression:"value1",arg:"arg1",modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}]})}`
  35. )
  36. })
  37. it('generate filters', () => {
  38. assertCodegen(
  39. '<div :id="a | b | c">{{ d | e | f }}</div>',
  40. `with(this){return _c('div',{attrs:{"id":_f("c")(_f("b")(a))}},[_v(_s(_f("f")(_f("e")(d))))])}`
  41. )
  42. })
  43. it('generate filters with no arguments', () => {
  44. assertCodegen(
  45. '<div>{{ d | e() }}</div>',
  46. `with(this){return _c('div',[_v(_s(_f("e")(d)))])}`
  47. )
  48. })
  49. it('generate v-for directive', () => {
  50. assertCodegen(
  51. '<div><li v-for="item in items" :key="item.uid"></li></div>',
  52. `with(this){return _c('div',_l((items),function(item){return _c('li',{key:item.uid})}),0)}`
  53. )
  54. // iterator syntax
  55. assertCodegen(
  56. '<div><li v-for="(item, i) in items"></li></div>',
  57. `with(this){return _c('div',_l((items),function(item,i){return _c('li')}),0)}`
  58. )
  59. assertCodegen(
  60. '<div><li v-for="(item, key, index) in items"></li></div>',
  61. `with(this){return _c('div',_l((items),function(item,key,index){return _c('li')}),0)}`
  62. )
  63. // destructuring
  64. assertCodegen(
  65. '<div><li v-for="{ a, b } in items"></li></div>',
  66. `with(this){return _c('div',_l((items),function({ a, b }){return _c('li')}),0)}`
  67. )
  68. assertCodegen(
  69. '<div><li v-for="({ a, b }, key, index) in items"></li></div>',
  70. `with(this){return _c('div',_l((items),function({ a, b },key,index){return _c('li')}),0)}`
  71. )
  72. // v-for with extra element
  73. assertCodegen(
  74. '<div><p></p><li v-for="item in items"></li></div>',
  75. `with(this){return _c('div',[_c('p'),_l((items),function(item){return _c('li')})],2)}`
  76. )
  77. })
  78. it('generate v-if directive', () => {
  79. assertCodegen(
  80. '<p v-if="show">hello</p>',
  81. `with(this){return (show)?_c('p',[_v("hello")]):_e()}`
  82. )
  83. })
  84. it('generate v-else directive', () => {
  85. assertCodegen(
  86. '<div><p v-if="show">hello</p><p v-else>world</p></div>',
  87. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):_c('p',[_v("world")])])}`
  88. )
  89. })
  90. it('generate v-else-if directive', () => {
  91. assertCodegen(
  92. '<div><p v-if="show">hello</p><p v-else-if="hide">world</p></div>',
  93. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):(hide)?_c('p',[_v("world")]):_e()])}`
  94. )
  95. })
  96. it('generate v-else-if with v-else directive', () => {
  97. assertCodegen(
  98. '<div><p v-if="show">hello</p><p v-else-if="hide">world</p><p v-else>bye</p></div>',
  99. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):(hide)?_c('p',[_v("world")]):_c('p',[_v("bye")])])}`
  100. )
  101. })
  102. it('generate multi v-else-if with v-else directive', () => {
  103. assertCodegen(
  104. '<div><p v-if="show">hello</p><p v-else-if="hide">world</p><p v-else-if="3">elseif</p><p v-else>bye</p></div>',
  105. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):(hide)?_c('p',[_v("world")]):(3)?_c('p',[_v("elseif")]):_c('p',[_v("bye")])])}`
  106. )
  107. })
  108. it('generate ref', () => {
  109. assertCodegen(
  110. '<p ref="component1"></p>',
  111. `with(this){return _c('p',{ref:"component1"})}`
  112. )
  113. })
  114. it('generate ref on v-for', () => {
  115. assertCodegen(
  116. '<ul><li v-for="item in items" ref="component1"></li></ul>',
  117. `with(this){return _c('ul',_l((items),function(item){return _c('li',{ref:"component1",refInFor:true})}),0)}`
  118. )
  119. })
  120. it('generate v-bind directive', () => {
  121. assertCodegen(
  122. '<p v-bind="test"></p>',
  123. `with(this){return _c('p',_b({},'p',test,false))}`
  124. )
  125. })
  126. it('generate v-bind with prop directive', () => {
  127. assertCodegen(
  128. '<p v-bind.prop="test"></p>',
  129. `with(this){return _c('p',_b({},'p',test,true))}`
  130. )
  131. })
  132. it('generate v-bind directive with sync modifier', () => {
  133. assertCodegen(
  134. '<p v-bind.sync="test"></p>',
  135. `with(this){return _c('p',_b({},'p',test,false,true))}`
  136. )
  137. })
  138. it('generate v-model directive', () => {
  139. assertCodegen(
  140. '<input v-model="test">',
  141. `with(this){return _c('input',{directives:[{name:"model",rawName:"v-model",value:(test),expression:"test"}],domProps:{"value":(test)},on:{"input":function($event){if($event.target.composing)return;test=$event.target.value}}})}`
  142. )
  143. })
  144. it('generate multiline v-model directive', () => {
  145. assertCodegen(
  146. '<input v-model="\n test \n">',
  147. `with(this){return _c('input',{directives:[{name:"model",rawName:"v-model",value:(\n test \n),expression:"\\n test \\n"}],domProps:{"value":(\n test \n)},on:{"input":function($event){if($event.target.composing)return;\n test \n=$event.target.value}}})}`
  148. )
  149. })
  150. it('generate multiline v-model directive on custom component', () => {
  151. assertCodegen(
  152. '<my-component v-model="\n test \n" />',
  153. `with(this){return _c('my-component',{model:{value:(\n test \n),callback:function ($$v) {\n test \n=$$v},expression:"\\n test \\n"}})}`
  154. )
  155. })
  156. it('generate template tag', () => {
  157. assertCodegen(
  158. '<div><template><p>{{hello}}</p></template></div>',
  159. `with(this){return _c('div',[[_c('p',[_v(_s(hello))])]],2)}`
  160. )
  161. })
  162. it('generate single slot', () => {
  163. assertCodegen(
  164. '<div><slot></slot></div>',
  165. `with(this){return _c('div',[_t("default")],2)}`
  166. )
  167. })
  168. it('generate named slot', () => {
  169. assertCodegen(
  170. '<div><slot name="one"></slot></div>',
  171. `with(this){return _c('div',[_t("one")],2)}`
  172. )
  173. })
  174. it('generate slot fallback content', () => {
  175. assertCodegen(
  176. '<div><slot><div>hi</div></slot></div>',
  177. `with(this){return _c('div',[_t("default",function(){return [_c('div',[_v("hi")])]})],2)}`
  178. )
  179. })
  180. it('generate slot target', () => {
  181. assertCodegen(
  182. '<p slot="one">hello world</p>',
  183. `with(this){return _c('p',{attrs:{"slot":"one"},slot:"one"},[_v("hello world")])}`
  184. )
  185. })
  186. it('generate scoped slot', () => {
  187. assertCodegen(
  188. '<foo><template slot-scope="bar">{{ bar }}</template></foo>',
  189. `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return [_v(_s(bar))]}}])})}`
  190. )
  191. assertCodegen(
  192. '<foo><div slot-scope="bar">{{ bar }}</div></foo>',
  193. `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return _c('div',{},[_v(_s(bar))])}}])})}`
  194. )
  195. })
  196. it('generate named scoped slot', () => {
  197. assertCodegen(
  198. '<foo><template slot="foo" slot-scope="bar">{{ bar }}</template></foo>',
  199. `with(this){return _c('foo',{scopedSlots:_u([{key:"foo",fn:function(bar){return [_v(_s(bar))]}}])})}`
  200. )
  201. assertCodegen(
  202. '<foo><div slot="foo" slot-scope="bar">{{ bar }}</div></foo>',
  203. `with(this){return _c('foo',{scopedSlots:_u([{key:"foo",fn:function(bar){return _c('div',{},[_v(_s(bar))])}}])})}`
  204. )
  205. })
  206. it('generate dynamic scoped slot', () => {
  207. assertCodegen(
  208. '<foo><template :slot="foo" slot-scope="bar">{{ bar }}</template></foo>',
  209. `with(this){return _c('foo',{scopedSlots:_u([{key:foo,fn:function(bar){return [_v(_s(bar))]}}],null,true)})}`
  210. )
  211. })
  212. it('generate scoped slot with multiline v-if', () => {
  213. assertCodegen(
  214. '<foo><template v-if="\nshow\n" slot-scope="bar">{{ bar }}</template></foo>',
  215. `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return (\nshow\n)?[_v(_s(bar))]:undefined}}],null,true)})}`
  216. )
  217. assertCodegen(
  218. '<foo><div v-if="\nshow\n" slot="foo" slot-scope="bar">{{ bar }}</div></foo>',
  219. `with(this){return _c(\'foo\',{scopedSlots:_u([{key:"foo",fn:function(bar){return (\nshow\n)?_c(\'div\',{},[_v(_s(bar))]):_e()}}],null,true)})}`
  220. )
  221. })
  222. it('generate scoped slot with new slot syntax', () => {
  223. assertCodegen(
  224. '<foo><template v-if="show" #default="bar">{{ bar }}</template></foo>',
  225. `with(this){return _c('foo',{scopedSlots:_u([(show)?{key:"default",fn:function(bar){return [_v(_s(bar))]}}:null],null,true)})}`
  226. )
  227. })
  228. it('generate class binding', () => {
  229. // static
  230. assertCodegen(
  231. '<p class="class1">hello world</p>',
  232. `with(this){return _c('p',{staticClass:"class1"},[_v("hello world")])}`
  233. )
  234. // dynamic
  235. assertCodegen(
  236. '<p :class="class1">hello world</p>',
  237. `with(this){return _c('p',{class:class1},[_v("hello world")])}`
  238. )
  239. })
  240. it('generate style binding', () => {
  241. assertCodegen(
  242. '<p :style="error">hello world</p>',
  243. `with(this){return _c('p',{style:(error)},[_v("hello world")])}`
  244. )
  245. })
  246. it('generate v-show directive', () => {
  247. assertCodegen(
  248. '<p v-show="shown">hello world</p>',
  249. `with(this){return _c('p',{directives:[{name:"show",rawName:"v-show",value:(shown),expression:"shown"}]},[_v("hello world")])}`
  250. )
  251. })
  252. it('generate DOM props with v-bind directive', () => {
  253. // input + value
  254. assertCodegen(
  255. '<input :value="msg">',
  256. `with(this){return _c('input',{domProps:{"value":msg}})}`
  257. )
  258. // non input
  259. assertCodegen(
  260. '<p :value="msg"/>',
  261. `with(this){return _c('p',{attrs:{"value":msg}})}`
  262. )
  263. })
  264. it('generate attrs with v-bind directive', () => {
  265. assertCodegen(
  266. '<input :name="field1">',
  267. `with(this){return _c('input',{attrs:{"name":field1}})}`
  268. )
  269. })
  270. it('generate static attrs', () => {
  271. assertCodegen(
  272. '<input name="field1">',
  273. `with(this){return _c('input',{attrs:{"name":"field1"}})}`
  274. )
  275. })
  276. it('generate events with v-on directive', () => {
  277. assertCodegen(
  278. '<input @input="onInput">',
  279. `with(this){return _c('input',{on:{"input":onInput}})}`
  280. )
  281. })
  282. it('generate events with method call', () => {
  283. assertCodegen(
  284. '<input @input="onInput($event);">',
  285. `with(this){return _c('input',{on:{"input":function($event){return onInput($event);}}})}`
  286. )
  287. // empty arguments
  288. assertCodegen(
  289. '<input @input="onInput();">',
  290. `with(this){return _c('input',{on:{"input":function($event){return onInput();}}})}`
  291. )
  292. // without semicolon
  293. assertCodegen(
  294. '<input @input="onInput($event)">',
  295. `with(this){return _c('input',{on:{"input":function($event){return onInput($event)}}})}`
  296. )
  297. // multiple args
  298. assertCodegen(
  299. '<input @input="onInput($event, \'abc\', 5);">',
  300. `with(this){return _c('input',{on:{"input":function($event){return onInput($event, 'abc', 5);}}})}`
  301. )
  302. // expression in args
  303. assertCodegen(
  304. '<input @input="onInput($event, 2+2);">',
  305. `with(this){return _c('input',{on:{"input":function($event){return onInput($event, 2+2);}}})}`
  306. )
  307. // tricky symbols in args
  308. assertCodegen(
  309. `<input @input="onInput(');[\\'());');">`,
  310. `with(this){return _c('input',{on:{"input":function($event){onInput(');[\\'());');}}})}`
  311. )
  312. // function name including a `function` part (#9920)
  313. assertCodegen(
  314. '<input @input="functionName()">',
  315. `with(this){return _c('input',{on:{"input":function($event){return functionName()}}})}`
  316. )
  317. })
  318. it('generate events with multiple statements', () => {
  319. // normal function
  320. assertCodegen(
  321. '<input @input="onInput1();onInput2()">',
  322. `with(this){return _c('input',{on:{"input":function($event){onInput1();onInput2()}}})}`
  323. )
  324. // function with multiple args
  325. assertCodegen(
  326. "<input @input=\"onInput1($event, 'text');onInput2('text2', $event)\">",
  327. `with(this){return _c('input',{on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}`
  328. )
  329. })
  330. it('generate events with keycode', () => {
  331. assertCodegen(
  332. '<input @input.enter="onInput">',
  333. `with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput.apply(null, arguments)}}})}`
  334. )
  335. // multiple keycodes (delete)
  336. assertCodegen(
  337. '<input @input.delete="onInput">',
  338. `with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput.apply(null, arguments)}}})}`
  339. )
  340. // multiple keycodes (esc)
  341. assertCodegen(
  342. '<input @input.esc="onInput">',
  343. `with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput.apply(null, arguments)}}})}`
  344. )
  345. // multiple keycodes (space)
  346. assertCodegen(
  347. '<input @input.space="onInput">',
  348. `with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput.apply(null, arguments)}}})}`
  349. )
  350. // multiple keycodes (chained)
  351. assertCodegen(
  352. '<input @keydown.enter.delete="onInput">',
  353. `with(this){return _c('input',{on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput.apply(null, arguments)}}})}`
  354. )
  355. // number keycode
  356. assertCodegen(
  357. '<input @input.13="onInput">',
  358. `with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&$event.keyCode!==13)return null;return onInput.apply(null, arguments)}}})}`
  359. )
  360. // custom keycode
  361. assertCodegen(
  362. '<input @input.custom="onInput">',
  363. `with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput.apply(null, arguments)}}})}`
  364. )
  365. })
  366. it('generate events with generic modifiers', () => {
  367. assertCodegen(
  368. '<input @input.stop="onInput">',
  369. `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();return onInput.apply(null, arguments)}}})}`
  370. )
  371. assertCodegen(
  372. '<input @input.prevent="onInput">',
  373. `with(this){return _c('input',{on:{"input":function($event){$event.preventDefault();return onInput.apply(null, arguments)}}})}`
  374. )
  375. assertCodegen(
  376. '<input @input.self="onInput">',
  377. `with(this){return _c('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput.apply(null, arguments)}}})}`
  378. )
  379. })
  380. // GitHub Issues #5146
  381. it('generate events with generic modifiers and keycode correct order', () => {
  382. assertCodegen(
  383. '<input @keydown.enter.prevent="onInput">',
  384. `with(this){return _c('input',{on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput.apply(null, arguments)}}})}`
  385. )
  386. assertCodegen(
  387. '<input @keydown.enter.stop="onInput">',
  388. `with(this){return _c('input',{on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput.apply(null, arguments)}}})}`
  389. )
  390. })
  391. it('generate events with mouse event modifiers', () => {
  392. assertCodegen(
  393. '<input @click.ctrl="onClick">',
  394. `with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick.apply(null, arguments)}}})}`
  395. )
  396. assertCodegen(
  397. '<input @click.shift="onClick">',
  398. `with(this){return _c('input',{on:{"click":function($event){if(!$event.shiftKey)return null;return onClick.apply(null, arguments)}}})}`
  399. )
  400. assertCodegen(
  401. '<input @click.alt="onClick">',
  402. `with(this){return _c('input',{on:{"click":function($event){if(!$event.altKey)return null;return onClick.apply(null, arguments)}}})}`
  403. )
  404. assertCodegen(
  405. '<input @click.meta="onClick">',
  406. `with(this){return _c('input',{on:{"click":function($event){if(!$event.metaKey)return null;return onClick.apply(null, arguments)}}})}`
  407. )
  408. assertCodegen(
  409. '<input @click.exact="onClick">',
  410. `with(this){return _c('input',{on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick.apply(null, arguments)}}})}`
  411. )
  412. assertCodegen(
  413. '<input @click.ctrl.exact="onClick">',
  414. `with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick.apply(null, arguments)}}})}`
  415. )
  416. })
  417. it('generate events with multiple modifiers', () => {
  418. assertCodegen(
  419. '<input @input.stop.prevent.self="onInput">',
  420. `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput.apply(null, arguments)}}})}`
  421. )
  422. })
  423. it('generate events with capture modifier', () => {
  424. assertCodegen(
  425. '<input @input.capture="onInput">',
  426. `with(this){return _c('input',{on:{"!input":function($event){return onInput.apply(null, arguments)}}})}`
  427. )
  428. })
  429. it('generate events with once modifier', () => {
  430. assertCodegen(
  431. '<input @input.once="onInput">',
  432. `with(this){return _c('input',{on:{"~input":function($event){return onInput.apply(null, arguments)}}})}`
  433. )
  434. })
  435. it('generate events with capture and once modifier', () => {
  436. assertCodegen(
  437. '<input @input.capture.once="onInput">',
  438. `with(this){return _c('input',{on:{"~!input":function($event){return onInput.apply(null, arguments)}}})}`
  439. )
  440. })
  441. it('generate events with once and capture modifier', () => {
  442. assertCodegen(
  443. '<input @input.once.capture="onInput">',
  444. `with(this){return _c('input',{on:{"~!input":function($event){return onInput.apply(null, arguments)}}})}`
  445. )
  446. })
  447. it('generate events with inline statement', () => {
  448. assertCodegen(
  449. '<input @input="current++">',
  450. `with(this){return _c('input',{on:{"input":function($event){current++}}})}`
  451. )
  452. })
  453. it('generate events with inline function expression', () => {
  454. // normal function
  455. assertCodegen(
  456. '<input @input="function () { current++ }">',
  457. `with(this){return _c('input',{on:{"input":function () { current++ }}})}`
  458. )
  459. // normal named function
  460. assertCodegen(
  461. '<input @input="function fn () { current++ }">',
  462. `with(this){return _c('input',{on:{"input":function fn () { current++ }}})}`
  463. )
  464. // arrow with no args
  465. assertCodegen(
  466. '<input @input="()=>current++">',
  467. `with(this){return _c('input',{on:{"input":()=>current++}})}`
  468. )
  469. // arrow with parens, single arg
  470. assertCodegen(
  471. '<input @input="(e) => current++">',
  472. `with(this){return _c('input',{on:{"input":(e) => current++}})}`
  473. )
  474. // arrow with parens, multi args
  475. assertCodegen(
  476. '<input @input="(a, b, c) => current++">',
  477. `with(this){return _c('input',{on:{"input":(a, b, c) => current++}})}`
  478. )
  479. // arrow with destructuring
  480. assertCodegen(
  481. '<input @input="({ a, b }) => current++">',
  482. `with(this){return _c('input',{on:{"input":({ a, b }) => current++}})}`
  483. )
  484. // arrow single arg no parens
  485. assertCodegen(
  486. '<input @input="e=>current++">',
  487. `with(this){return _c('input',{on:{"input":e=>current++}})}`
  488. )
  489. // with modifiers
  490. assertCodegen(
  491. `<input @keyup.enter="e=>current++">`,
  492. `with(this){return _c('input',{on:{"keyup":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++).apply(null, arguments)}}})}`
  493. )
  494. })
  495. // #3893
  496. it('should not treat handler with unexpected whitespace as inline statement', () => {
  497. assertCodegen(
  498. '<input @input=" onInput ">',
  499. `with(this){return _c('input',{on:{"input":onInput}})}`
  500. )
  501. })
  502. it('generate unhandled events', () => {
  503. assertCodegen(
  504. '<input @input="current++">',
  505. `with(this){return _c('input',{on:{"input":function(){}}})}`,
  506. ast => {
  507. ast.events.input = undefined
  508. }
  509. )
  510. })
  511. it('generate multiple event handlers', () => {
  512. assertCodegen(
  513. '<input @input="current++" @input.stop="onInput">',
  514. `with(this){return _c('input',{on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput.apply(null, arguments)}]}})}`
  515. )
  516. })
  517. it('generate component', () => {
  518. assertCodegen(
  519. '<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
  520. `with(this){return _c('my-component',{attrs:{"name":"mycomponent1","msg":msg},on:{"notify":onNotify}},[_c('div',[_v("hi")])])}`
  521. )
  522. })
  523. it('generate svg component with children', () => {
  524. assertCodegen(
  525. '<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
  526. `with(this){return _c('svg',[_c('my-comp',[_c('circle',{attrs:{"r":10}})])],1)}`
  527. )
  528. })
  529. it('generate is attribute', () => {
  530. assertCodegen(
  531. '<div is="component1"></div>',
  532. `with(this){return _c("component1",{tag:"div"})}`
  533. )
  534. assertCodegen(
  535. '<div :is="component1"></div>',
  536. `with(this){return _c(component1,{tag:"div"})}`
  537. )
  538. // maybe a component and normalize type should be 1
  539. assertCodegen(
  540. '<div><div is="component1"></div></div>',
  541. `with(this){return _c('div',[_c("component1",{tag:"div"})],1)}`
  542. )
  543. })
  544. it('generate component with inline-template', () => {
  545. // have "inline-template'"
  546. assertCodegen(
  547. '<my-component inline-template><p><span>hello world</span></p></my-component>',
  548. `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")])])}}]}})}`
  549. )
  550. // "have inline-template attrs, but not having exactly one child element
  551. assertCodegen(
  552. '<my-component inline-template><hr><hr></my-component>',
  553. `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _c('hr')}},staticRenderFns:[]}})}`
  554. )
  555. assertCodegen(
  556. '<my-component inline-template></my-component>',
  557. `with(this){return _c('my-component',{})}`
  558. )
  559. // have "is" attribute
  560. assertCodegen(
  561. '<div is="myComponent" inline-template><div></div></div>',
  562. `with(this){return _c("myComponent",{tag:"div",inlineTemplate:{render:function(){with(this){return _c('div')}},staticRenderFns:[]}})}`
  563. )
  564. assertCodegen(
  565. '<div is="myComponent" inline-template></div>',
  566. `with(this){return _c("myComponent",{tag:"div"})}`
  567. )
  568. expect(
  569. 'Inline-template components must have exactly one child element.'
  570. ).toHaveBeenWarned()
  571. expect((console.error as any).mock.calls.length).toBe(3)
  572. })
  573. it('generate static trees inside v-for', () => {
  574. assertCodegen(
  575. `<div><div v-for="i in 10"><p><span></span></p></div></div>`,
  576. `with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true)])}),0)}`,
  577. [`with(this){return _c('p',[_c('span')])}`]
  578. )
  579. })
  580. it('generate component with v-for', () => {
  581. // normalize type: 2
  582. assertCodegen(
  583. '<div><child></child><template v-for="item in list">{{ item }}</template></div>',
  584. `with(this){return _c('div',[_c('child'),_l((list),function(item){return [_v(_s(item))]})],2)}`
  585. )
  586. })
  587. it('generate component with comment', () => {
  588. const options = extend(
  589. {
  590. comments: true
  591. },
  592. baseOptions
  593. )
  594. const template = '<div><!--comment--></div>'
  595. const generatedCode = `with(this){return _c('div',[_e("comment")])}`
  596. const ast = parse(template, options)
  597. optimize(ast, options)
  598. const res = generate(ast, options)
  599. expect(res.render).toBe(generatedCode)
  600. })
  601. // #6150
  602. it('generate comments with special characters', () => {
  603. const options = extend(
  604. {
  605. comments: true
  606. },
  607. baseOptions
  608. )
  609. const template = "<div><!--\n'comment'\n--></div>"
  610. const generatedCode = `with(this){return _c('div',[_e("\\n'comment'\\n")])}`
  611. const ast = parse(template, options)
  612. optimize(ast, options)
  613. const res = generate(ast, options)
  614. expect(res.render).toBe(generatedCode)
  615. })
  616. // #8041
  617. it('does not squash templates inside v-pre', () => {
  618. const template = '<div v-pre><template><p>{{msg}}</p></template></div>'
  619. const generatedCode = `with(this){return _m(0)}`
  620. const renderFn = `with(this){return _c('div',{pre:true},[_c('template',[_c('p',[_v("{{msg}}")])])],2)}`
  621. const ast = parse(template, baseOptions)
  622. optimize(ast, baseOptions)
  623. const res = generate(ast, baseOptions)
  624. expect(res.render).toBe(generatedCode)
  625. expect(res.staticRenderFns).toEqual([renderFn])
  626. })
  627. it('not specified ast type', () => {
  628. const res = generate(undefined, baseOptions)
  629. expect(res.render).toBe(`with(this){return _c("div")}`)
  630. expect(res.staticRenderFns).toEqual([])
  631. })
  632. it('not specified directives option', () => {
  633. assertCodegen(
  634. '<p v-if="show">hello world</p>',
  635. `with(this){return (show)?_c('p',[_v("hello world")]):_e()}`,
  636. { isReservedTag }
  637. )
  638. })
  639. // #9142
  640. it('should compile single v-for component inside template', () => {
  641. assertCodegen(
  642. `<div><template v-if="ok"><foo v-for="i in 1" :key="i"></foo></template></div>`,
  643. `with(this){return _c('div',[(ok)?_l((1),function(i){return _c('foo',{key:i})}):_e()],2)}`
  644. )
  645. })
  646. it('component with bindings ', () => {
  647. const ast = parse(`<div><Foo/><foo-bar></foo-bar></div>`, baseOptions)
  648. optimize(ast, baseOptions)
  649. const res = generate(ast, {
  650. ...baseOptions,
  651. bindings: {
  652. Foo: BindingTypes.SETUP_CONST,
  653. FooBar: BindingTypes.SETUP_CONST
  654. }
  655. })
  656. expect(res.render).toMatchInlineSnapshot(
  657. '"with(this){return _c(\'div\',[_c(Foo),_c(FooBar)],1)}"'
  658. )
  659. })
  660. // #12674
  661. it('component with bindings: should not resolve native elements', () => {
  662. const ast = parse(`<div><form>{{ n }}</form></div>`, baseOptions)
  663. optimize(ast, baseOptions)
  664. const res = generate(ast, {
  665. ...baseOptions,
  666. bindings: {
  667. form: BindingTypes.SETUP_CONST
  668. }
  669. })
  670. expect(res.render).toMatch(`_c('form'`)
  671. expect(res.render).toMatchInlineSnapshot(
  672. "\"with(this){return _c('div',[_c('form',[_v(_s(n))])])}\""
  673. )
  674. })
  675. })