codegen.spec.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import { generate } from 'compiler/codegen'
  4. import { isObject, extend } from 'shared/util'
  5. import { isReservedTag } from 'web/util/index'
  6. import { baseOptions } from 'web/compiler/options'
  7. function assertCodegen (template, generatedCode, ...args) {
  8. let staticRenderFnCodes = []
  9. let generateOptions = baseOptions
  10. let proc = null
  11. let len = args.length
  12. while (len--) {
  13. const arg = args[len]
  14. if (Array.isArray(arg)) {
  15. staticRenderFnCodes = arg
  16. } else if (isObject(arg)) {
  17. generateOptions = arg
  18. } else if (typeof arg === 'function') {
  19. proc = arg
  20. }
  21. }
  22. const ast = parse(template, baseOptions)
  23. optimize(ast, baseOptions)
  24. proc && proc(ast)
  25. const res = generate(ast, generateOptions)
  26. expect(res.render).toBe(generatedCode)
  27. expect(res.staticRenderFns).toEqual(staticRenderFnCodes)
  28. }
  29. /* eslint-disable quotes */
  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})}))}`
  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')}))}`
  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')}))}`
  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')}))}`
  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')}))}`
  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})}))}`
  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",[_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 scoped slot with multiline v-if', () => {
  207. assertCodegen(
  208. '<foo><template v-if="\nshow\n" slot-scope="bar">{{ bar }}</template></foo>',
  209. `with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return (\nshow\n)?[_v(_s(bar))]:undefined}}])})}`
  210. )
  211. assertCodegen(
  212. '<foo><div v-if="\nshow\n" slot="foo" slot-scope="bar">{{ bar }}</div></foo>',
  213. `with(this){return _c(\'foo\',{scopedSlots:_u([{key:"foo",fn:function(bar){return (\nshow\n)?_c(\'div\',{},[_v(_s(bar))]):_e()}}])})}`
  214. )
  215. })
  216. it('generate class binding', () => {
  217. // static
  218. assertCodegen(
  219. '<p class="class1">hello world</p>',
  220. `with(this){return _c('p',{staticClass:"class1"},[_v("hello world")])}`,
  221. )
  222. // dynamic
  223. assertCodegen(
  224. '<p :class="class1">hello world</p>',
  225. `with(this){return _c('p',{class:class1},[_v("hello world")])}`
  226. )
  227. })
  228. it('generate style binding', () => {
  229. assertCodegen(
  230. '<p :style="error">hello world</p>',
  231. `with(this){return _c('p',{style:(error)},[_v("hello world")])}`
  232. )
  233. })
  234. it('generate v-show directive', () => {
  235. assertCodegen(
  236. '<p v-show="shown">hello world</p>',
  237. `with(this){return _c('p',{directives:[{name:"show",rawName:"v-show",value:(shown),expression:"shown"}]},[_v("hello world")])}`
  238. )
  239. })
  240. it('generate DOM props with v-bind directive', () => {
  241. // input + value
  242. assertCodegen(
  243. '<input :value="msg">',
  244. `with(this){return _c('input',{domProps:{"value":msg}})}`
  245. )
  246. // non input
  247. assertCodegen(
  248. '<p :value="msg"/>',
  249. `with(this){return _c('p',{attrs:{"value":msg}})}`
  250. )
  251. })
  252. it('generate attrs with v-bind directive', () => {
  253. assertCodegen(
  254. '<input :name="field1">',
  255. `with(this){return _c('input',{attrs:{"name":field1}})}`
  256. )
  257. })
  258. it('generate static attrs', () => {
  259. assertCodegen(
  260. '<input name="field1">',
  261. `with(this){return _c('input',{attrs:{"name":"field1"}})}`
  262. )
  263. })
  264. it('generate events with v-on directive', () => {
  265. assertCodegen(
  266. '<input @input="onInput">',
  267. `with(this){return _c('input',{on:{"input":onInput}})}`
  268. )
  269. })
  270. it('generate events with method call', () => {
  271. assertCodegen(
  272. '<input @input="onInput($event);">',
  273. `with(this){return _c('input',{on:{"input":function($event){onInput($event);}}})}`
  274. )
  275. // empty arguments
  276. assertCodegen(
  277. '<input @input="onInput();">',
  278. `with(this){return _c('input',{on:{"input":function($event){onInput();}}})}`
  279. )
  280. // without semicolon
  281. assertCodegen(
  282. '<input @input="onInput($event)">',
  283. `with(this){return _c('input',{on:{"input":function($event){onInput($event)}}})}`
  284. )
  285. // multiple args
  286. assertCodegen(
  287. '<input @input="onInput($event, \'abc\', 5);">',
  288. `with(this){return _c('input',{on:{"input":function($event){onInput($event, 'abc', 5);}}})}`
  289. )
  290. // expression in args
  291. assertCodegen(
  292. '<input @input="onInput($event, 2+2);">',
  293. `with(this){return _c('input',{on:{"input":function($event){onInput($event, 2+2);}}})}`
  294. )
  295. // tricky symbols in args
  296. assertCodegen(
  297. '<input @input="onInput(\');[\'());\');">',
  298. `with(this){return _c('input',{on:{"input":function($event){onInput(');[\'());');}}})}`
  299. )
  300. })
  301. it('generate events with multiple statements', () => {
  302. // normal function
  303. assertCodegen(
  304. '<input @input="onInput1();onInput2()">',
  305. `with(this){return _c('input',{on:{"input":function($event){onInput1();onInput2()}}})}`
  306. )
  307. // function with multiple args
  308. assertCodegen(
  309. '<input @input="onInput1($event, \'text\');onInput2(\'text2\', $event)">',
  310. `with(this){return _c('input',{on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}`
  311. )
  312. })
  313. it('generate events with keycode', () => {
  314. assertCodegen(
  315. '<input @input.enter="onInput">',
  316. `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)}}})}`
  317. )
  318. // multiple keycodes (delete)
  319. assertCodegen(
  320. '<input @input.delete="onInput">',
  321. `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)}}})}`
  322. )
  323. // multiple keycodes (esc)
  324. assertCodegen(
  325. '<input @input.esc="onInput">',
  326. `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)}}})}`
  327. )
  328. // multiple keycodes (space)
  329. assertCodegen(
  330. '<input @input.space="onInput">',
  331. `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)}}})}`
  332. )
  333. // multiple keycodes (chained)
  334. assertCodegen(
  335. '<input @keydown.enter.delete="onInput">',
  336. `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)}}})}`
  337. )
  338. // number keycode
  339. assertCodegen(
  340. '<input @input.13="onInput">',
  341. `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&$event.keyCode!==13)return null;return onInput($event)}}})}`
  342. )
  343. // custom keycode
  344. assertCodegen(
  345. '<input @input.custom="onInput">',
  346. `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)}}})}`
  347. )
  348. })
  349. it('generate events with generic modifiers', () => {
  350. assertCodegen(
  351. '<input @input.stop="onInput">',
  352. `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();return onInput($event)}}})}`
  353. )
  354. assertCodegen(
  355. '<input @input.prevent="onInput">',
  356. `with(this){return _c('input',{on:{"input":function($event){$event.preventDefault();return onInput($event)}}})}`
  357. )
  358. assertCodegen(
  359. '<input @input.self="onInput">',
  360. `with(this){return _c('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}`
  361. )
  362. })
  363. // GitHub Issues #5146
  364. it('generate events with generic modifiers and keycode correct order', () => {
  365. assertCodegen(
  366. '<input @keydown.enter.prevent="onInput">',
  367. `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}`
  368. )
  369. assertCodegen(
  370. '<input @keydown.enter.stop="onInput">',
  371. `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}`
  372. )
  373. })
  374. it('generate events with mouse event modifiers', () => {
  375. assertCodegen(
  376. '<input @click.ctrl="onClick">',
  377. `with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick($event)}}})}`
  378. )
  379. assertCodegen(
  380. '<input @click.shift="onClick">',
  381. `with(this){return _c('input',{on:{"click":function($event){if(!$event.shiftKey)return null;return onClick($event)}}})}`
  382. )
  383. assertCodegen(
  384. '<input @click.alt="onClick">',
  385. `with(this){return _c('input',{on:{"click":function($event){if(!$event.altKey)return null;return onClick($event)}}})}`
  386. )
  387. assertCodegen(
  388. '<input @click.meta="onClick">',
  389. `with(this){return _c('input',{on:{"click":function($event){if(!$event.metaKey)return null;return onClick($event)}}})}`
  390. )
  391. assertCodegen(
  392. '<input @click.exact="onClick">',
  393. `with(this){return _c('input',{on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}`
  394. )
  395. assertCodegen(
  396. '<input @click.ctrl.exact="onClick">',
  397. `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($event)}}})}`
  398. )
  399. })
  400. it('generate events with multiple modifiers', () => {
  401. assertCodegen(
  402. '<input @input.stop.prevent.self="onInput">',
  403. `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}`
  404. )
  405. })
  406. it('generate events with capture modifier', () => {
  407. assertCodegen(
  408. '<input @input.capture="onInput">',
  409. `with(this){return _c('input',{on:{"!input":function($event){return onInput($event)}}})}`
  410. )
  411. })
  412. it('generate events with once modifier', () => {
  413. assertCodegen(
  414. '<input @input.once="onInput">',
  415. `with(this){return _c('input',{on:{"~input":function($event){return onInput($event)}}})}`
  416. )
  417. })
  418. it('generate events with capture and once modifier', () => {
  419. assertCodegen(
  420. '<input @input.capture.once="onInput">',
  421. `with(this){return _c('input',{on:{"~!input":function($event){return onInput($event)}}})}`
  422. )
  423. })
  424. it('generate events with once and capture modifier', () => {
  425. assertCodegen(
  426. '<input @input.once.capture="onInput">',
  427. `with(this){return _c('input',{on:{"~!input":function($event){return onInput($event)}}})}`
  428. )
  429. })
  430. it('generate events with inline statement', () => {
  431. assertCodegen(
  432. '<input @input="current++">',
  433. `with(this){return _c('input',{on:{"input":function($event){current++}}})}`
  434. )
  435. })
  436. it('generate events with inline function expression', () => {
  437. // normal function
  438. assertCodegen(
  439. '<input @input="function () { current++ }">',
  440. `with(this){return _c('input',{on:{"input":function () { current++ }}})}`
  441. )
  442. // arrow with no args
  443. assertCodegen(
  444. '<input @input="()=>current++">',
  445. `with(this){return _c('input',{on:{"input":()=>current++}})}`
  446. )
  447. // arrow with parens, single arg
  448. assertCodegen(
  449. '<input @input="(e) => current++">',
  450. `with(this){return _c('input',{on:{"input":(e) => current++}})}`
  451. )
  452. // arrow with parens, multi args
  453. assertCodegen(
  454. '<input @input="(a, b, c) => current++">',
  455. `with(this){return _c('input',{on:{"input":(a, b, c) => current++}})}`
  456. )
  457. // arrow with destructuring
  458. assertCodegen(
  459. '<input @input="({ a, b }) => current++">',
  460. `with(this){return _c('input',{on:{"input":({ a, b }) => current++}})}`
  461. )
  462. // arrow single arg no parens
  463. assertCodegen(
  464. '<input @input="e=>current++">',
  465. `with(this){return _c('input',{on:{"input":e=>current++}})}`
  466. )
  467. // with modifiers
  468. assertCodegen(
  469. `<input @keyup.enter="e=>current++">`,
  470. `with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}`
  471. )
  472. })
  473. // #3893
  474. it('should not treat handler with unexpected whitespace as inline statement', () => {
  475. assertCodegen(
  476. '<input @input=" onInput ">',
  477. `with(this){return _c('input',{on:{"input":onInput}})}`
  478. )
  479. })
  480. it('generate unhandled events', () => {
  481. assertCodegen(
  482. '<input @input="current++">',
  483. `with(this){return _c('input',{on:{"input":function(){}}})}`,
  484. ast => {
  485. ast.events.input = undefined
  486. }
  487. )
  488. })
  489. it('generate multiple event handlers', () => {
  490. assertCodegen(
  491. '<input @input="current++" @input.stop="onInput">',
  492. `with(this){return _c('input',{on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}})}`
  493. )
  494. })
  495. it('generate component', () => {
  496. assertCodegen(
  497. '<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
  498. `with(this){return _c('my-component',{attrs:{"name":"mycomponent1","msg":msg},on:{"notify":onNotify}},[_c('div',[_v("hi")])])}`
  499. )
  500. })
  501. it('generate svg component with children', () => {
  502. assertCodegen(
  503. '<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
  504. `with(this){return _c('svg',[_c('my-comp',[_c('circle',{attrs:{"r":10}})])],1)}`
  505. )
  506. })
  507. it('generate is attribute', () => {
  508. assertCodegen(
  509. '<div is="component1"></div>',
  510. `with(this){return _c("component1",{tag:"div"})}`
  511. )
  512. assertCodegen(
  513. '<div :is="component1"></div>',
  514. `with(this){return _c(component1,{tag:"div"})}`
  515. )
  516. // maybe a component and normalize type should be 1
  517. assertCodegen(
  518. '<div><div is="component1"></div></div>',
  519. `with(this){return _c('div',[_c("component1",{tag:"div"})],1)}`
  520. )
  521. })
  522. it('generate component with inline-template', () => {
  523. // have "inline-template'"
  524. assertCodegen(
  525. '<my-component inline-template><p><span>hello world</span></p></my-component>',
  526. `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")])])}}]}})}`
  527. )
  528. // "have inline-template attrs, but not having exactly one child element
  529. assertCodegen(
  530. '<my-component inline-template><hr><hr></my-component>',
  531. `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _c('hr')}},staticRenderFns:[]}})}`
  532. )
  533. try {
  534. assertCodegen(
  535. '<my-component inline-template></my-component>',
  536. ''
  537. )
  538. } catch (e) {}
  539. expect('Inline-template components must have exactly one child element.').toHaveBeenWarned()
  540. expect(console.error.calls.count()).toBe(2)
  541. })
  542. it('generate static trees inside v-for', () => {
  543. assertCodegen(
  544. `<div><div v-for="i in 10"><p><span></span></p></div></div>`,
  545. `with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true)])}))}`,
  546. [`with(this){return _c('p',[_c('span')])}`]
  547. )
  548. })
  549. it('generate component with v-for', () => {
  550. // normalize type: 2
  551. assertCodegen(
  552. '<div><child></child><template v-for="item in list">{{ item }}</template></div>',
  553. `with(this){return _c('div',[_c('child'),_l((list),function(item){return [_v(_s(item))]})],2)}`
  554. )
  555. })
  556. it('generate component with comment', () => {
  557. const options = extend({
  558. comments: true
  559. }, baseOptions)
  560. const template = '<div><!--comment--></div>'
  561. const generatedCode = `with(this){return _c('div',[_e("comment")])}`
  562. const ast = parse(template, options)
  563. optimize(ast, options)
  564. const res = generate(ast, options)
  565. expect(res.render).toBe(generatedCode)
  566. })
  567. // #6150
  568. it('generate comments with special characters', () => {
  569. const options = extend({
  570. comments: true
  571. }, baseOptions)
  572. const template = '<div><!--\n\'comment\'\n--></div>'
  573. const generatedCode = `with(this){return _c('div',[_e("\\n'comment'\\n")])}`
  574. const ast = parse(template, options)
  575. optimize(ast, options)
  576. const res = generate(ast, options)
  577. expect(res.render).toBe(generatedCode)
  578. })
  579. // #8041
  580. it('does not squash templates inside v-pre', () => {
  581. const template = '<div v-pre><template><p>{{msg}}</p></template></div>'
  582. const generatedCode = `with(this){return _m(0)}`
  583. const renderFn = `with(this){return _c('div',{pre:true},[_c('template',[_c('p',[_v("{{msg}}")])])],2)}`
  584. const ast = parse(template, baseOptions)
  585. optimize(ast, baseOptions)
  586. const res = generate(ast, baseOptions)
  587. expect(res.render).toBe(generatedCode)
  588. expect(res.staticRenderFns).toEqual([renderFn])
  589. })
  590. it('not specified ast type', () => {
  591. const res = generate(null, baseOptions)
  592. expect(res.render).toBe(`with(this){return _c("div")}`)
  593. expect(res.staticRenderFns).toEqual([])
  594. })
  595. it('not specified directives option', () => {
  596. assertCodegen(
  597. '<p v-if="show">hello world</p>',
  598. `with(this){return (show)?_c('p',[_v("hello world")]):_e()}`,
  599. { isReservedTag }
  600. )
  601. })
  602. // #9142
  603. it('should compile single v-for component inside template', () => {
  604. assertCodegen(
  605. `<div><template v-if="ok"><foo v-for="i in 1" :key="i"></foo></template></div>`,
  606. `with(this){return _c('div',[(ok)?_l((1),function(i){return _c('foo',{key:i})}):_e()],2)}`
  607. )
  608. })
  609. })
  610. /* eslint-enable quotes */