codegen.spec.js 23 KB

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