codegen.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import { generate } from 'compiler/codegen'
  4. import { isObject } 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 v-for directive', () => {
  44. assertCodegen(
  45. '<div><li v-for="item in items" :key="item.uid"></li></div>',
  46. `with(this){return _c('div',_l((items),function(item){return _c('li',{key:item.uid})}))}`
  47. )
  48. // iterator syntax
  49. assertCodegen(
  50. '<div><li v-for="(item, i) in items"></li></div>',
  51. `with(this){return _c('div',_l((items),function(item,i){return _c('li')}))}`
  52. )
  53. assertCodegen(
  54. '<div><li v-for="(item, key, index) in items"></li></div>',
  55. `with(this){return _c('div',_l((items),function(item,key,index){return _c('li')}))}`
  56. )
  57. // destructuring
  58. assertCodegen(
  59. '<div><li v-for="{ a, b } in items"></li></div>',
  60. `with(this){return _c('div',_l((items),function({ a, b }){return _c('li')}))}`
  61. )
  62. assertCodegen(
  63. '<div><li v-for="({ a, b }, key, index) in items"></li></div>',
  64. `with(this){return _c('div',_l((items),function({ a, b },key,index){return _c('li')}))}`
  65. )
  66. // v-for with extra element
  67. assertCodegen(
  68. '<div><p></p><li v-for="item in items"></li></div>',
  69. `with(this){return _c('div',[_c('p'),_l((items),function(item){return _c('li')})],2)}`
  70. )
  71. })
  72. it('generate v-if directive', () => {
  73. assertCodegen(
  74. '<p v-if="show">hello</p>',
  75. `with(this){return (show)?_c('p',[_v("hello")]):_e()}`
  76. )
  77. })
  78. it('generate v-else directive', () => {
  79. assertCodegen(
  80. '<div><p v-if="show">hello</p><p v-else>world</p></div>',
  81. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):_c('p',[_v("world")])])}`
  82. )
  83. })
  84. it('generate v-else-if directive', () => {
  85. assertCodegen(
  86. '<div><p v-if="show">hello</p><p v-else-if="hide">world</p></div>',
  87. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):(hide)?_c('p',[_v("world")]):_e()])}`
  88. )
  89. })
  90. it('generate v-else-if with v-else directive', () => {
  91. assertCodegen(
  92. '<div><p v-if="show">hello</p><p v-else-if="hide">world</p><p v-else>bye</p></div>',
  93. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):(hide)?_c('p',[_v("world")]):_c('p',[_v("bye")])])}`
  94. )
  95. })
  96. it('generate multi 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-if="3">elseif</p><p v-else>bye</p></div>',
  99. `with(this){return _c('div',[(show)?_c('p',[_v("hello")]):(hide)?_c('p',[_v("world")]):(3)?_c('p',[_v("elseif")]):_c('p',[_v("bye")])])}`
  100. )
  101. })
  102. it('generate ref', () => {
  103. assertCodegen(
  104. '<p ref="component1"></p>',
  105. `with(this){return _c('p',{ref:"component1"})}`
  106. )
  107. })
  108. it('generate ref on v-for', () => {
  109. assertCodegen(
  110. '<ul><li v-for="item in items" ref="component1"></li></ul>',
  111. `with(this){return _c('ul',_l((items),function(item){return _c('li',{ref:"component1",refInFor:true})}))}`
  112. )
  113. })
  114. it('generate v-bind directive', () => {
  115. assertCodegen(
  116. '<p v-bind="test"></p>',
  117. `with(this){return _c('p',_b({},'p',test))}`
  118. )
  119. })
  120. it('generate template tag', () => {
  121. assertCodegen(
  122. '<div><template><p>{{hello}}</p></template></div>',
  123. `with(this){return _c('div',[[_c('p',[_v(_s(hello))])]],2)}`
  124. )
  125. })
  126. it('generate single slot', () => {
  127. assertCodegen(
  128. '<div><slot></slot></div>',
  129. `with(this){return _c('div',[_t("default")],2)}`
  130. )
  131. })
  132. it('generate named slot', () => {
  133. assertCodegen(
  134. '<div><slot name="one"></slot></div>',
  135. `with(this){return _c('div',[_t("one")],2)}`
  136. )
  137. })
  138. it('generate slot fallback content', () => {
  139. assertCodegen(
  140. '<div><slot><div>hi</div></slot></div>',
  141. `with(this){return _c('div',[_t("default",[_c('div',[_v("hi")])])],2)}`
  142. )
  143. })
  144. it('generate slot target', () => {
  145. assertCodegen(
  146. '<p slot="one">hello world</p>',
  147. `with(this){return _c('p',{slot:"one"},[_v("hello world")])}`
  148. )
  149. })
  150. it('generate class binding', () => {
  151. // static
  152. assertCodegen(
  153. '<p class="class1">hello world</p>',
  154. `with(this){return _c('p',{staticClass:"class1"},[_v("hello world")])}`,
  155. )
  156. // dynamic
  157. assertCodegen(
  158. '<p :class="class1">hello world</p>',
  159. `with(this){return _c('p',{class:class1},[_v("hello world")])}`
  160. )
  161. })
  162. it('generate style binding', () => {
  163. assertCodegen(
  164. '<p :style="error">hello world</p>',
  165. `with(this){return _c('p',{style:(error)},[_v("hello world")])}`
  166. )
  167. })
  168. it('generate v-show directive', () => {
  169. assertCodegen(
  170. '<p v-show="shown">hello world</p>',
  171. `with(this){return _c('p',{directives:[{name:"show",rawName:"v-show",value:(shown),expression:"shown"}]},[_v("hello world")])}`
  172. )
  173. })
  174. it('generate DOM props with v-bind directive', () => {
  175. // input + value
  176. assertCodegen(
  177. '<input :value="msg">',
  178. `with(this){return _c('input',{domProps:{"value":msg}})}`
  179. )
  180. // non input
  181. assertCodegen(
  182. '<p :value="msg"/>',
  183. `with(this){return _c('p',{attrs:{"value":msg}})}`
  184. )
  185. })
  186. it('generate attrs with v-bind directive', () => {
  187. assertCodegen(
  188. '<input :name="field1">',
  189. `with(this){return _c('input',{attrs:{"name":field1}})}`
  190. )
  191. })
  192. it('generate static attrs', () => {
  193. assertCodegen(
  194. '<input name="field1">',
  195. `with(this){return _c('input',{attrs:{"name":"field1"}})}`
  196. )
  197. })
  198. it('generate events with v-on directive', () => {
  199. assertCodegen(
  200. '<input @input="onInput">',
  201. `with(this){return _c('input',{on:{"input":onInput}})}`
  202. )
  203. })
  204. it('generate events with keycode', () => {
  205. assertCodegen(
  206. '<input @input.enter="onInput">',
  207. `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;onInput($event)}}})}`
  208. )
  209. // multiple keycodes (delete)
  210. assertCodegen(
  211. '<input @input.delete="onInput">',
  212. `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"delete",[8,46]))return null;onInput($event)}}})}`
  213. )
  214. // multiple keycodes (chained)
  215. assertCodegen(
  216. '<input @keydown.enter.delete="onInput">',
  217. `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13)&&_k($event.keyCode,"delete",[8,46]))return null;onInput($event)}}})}`
  218. )
  219. // number keycode
  220. assertCodegen(
  221. '<input @input.13="onInput">',
  222. `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&$event.keyCode!==13)return null;onInput($event)}}})}`
  223. )
  224. // custom keycode
  225. assertCodegen(
  226. '<input @input.custom="onInput">',
  227. `with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"custom"))return null;onInput($event)}}})}`
  228. )
  229. })
  230. it('generate events with generic modifiers', () => {
  231. assertCodegen(
  232. '<input @input.stop="onInput">',
  233. `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}})}`
  234. )
  235. assertCodegen(
  236. '<input @input.prevent="onInput">',
  237. `with(this){return _c('input',{on:{"input":function($event){$event.preventDefault();onInput($event)}}})}`
  238. )
  239. assertCodegen(
  240. '<input @input.self="onInput">',
  241. `with(this){return _c('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return null;onInput($event)}}})}`
  242. )
  243. })
  244. // Github Issues #5146
  245. it('generate events with generic modifiers and keycode correct order', () => {
  246. assertCodegen(
  247. '<input @keydown.enter.prevent="onInput">',
  248. `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;$event.preventDefault();onInput($event)}}})}`
  249. )
  250. assertCodegen(
  251. '<input @keydown.enter.stop="onInput">',
  252. `with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;$event.stopPropagation();onInput($event)}}})}`
  253. )
  254. })
  255. it('generate events with mouse event modifiers', () => {
  256. assertCodegen(
  257. '<input @click.ctrl="onClick">',
  258. `with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;onClick($event)}}})}`
  259. )
  260. assertCodegen(
  261. '<input @click.shift="onClick">',
  262. `with(this){return _c('input',{on:{"click":function($event){if(!$event.shiftKey)return null;onClick($event)}}})}`
  263. )
  264. assertCodegen(
  265. '<input @click.alt="onClick">',
  266. `with(this){return _c('input',{on:{"click":function($event){if(!$event.altKey)return null;onClick($event)}}})}`
  267. )
  268. assertCodegen(
  269. '<input @click.meta="onClick">',
  270. `with(this){return _c('input',{on:{"click":function($event){if(!$event.metaKey)return null;onClick($event)}}})}`
  271. )
  272. })
  273. it('generate events with multiple modifiers', () => {
  274. assertCodegen(
  275. '<input @input.stop.prevent.self="onInput">',
  276. `with(this){return _c('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;onInput($event)}}})}`
  277. )
  278. })
  279. it('generate events with capture modifier', () => {
  280. assertCodegen(
  281. '<input @input.capture="onInput">',
  282. `with(this){return _c('input',{on:{"!input":function($event){onInput($event)}}})}`
  283. )
  284. })
  285. it('generate events with once modifier', () => {
  286. assertCodegen(
  287. '<input @input.once="onInput">',
  288. `with(this){return _c('input',{on:{"~input":function($event){onInput($event)}}})}`
  289. )
  290. })
  291. it('generate events with capture and once modifier', () => {
  292. assertCodegen(
  293. '<input @input.capture.once="onInput">',
  294. `with(this){return _c('input',{on:{"~!input":function($event){onInput($event)}}})}`
  295. )
  296. })
  297. it('generate events with once and capture modifier', () => {
  298. assertCodegen(
  299. '<input @input.once.capture="onInput">',
  300. `with(this){return _c('input',{on:{"~!input":function($event){onInput($event)}}})}`
  301. )
  302. })
  303. it('generate events with inline statement', () => {
  304. assertCodegen(
  305. '<input @input="current++">',
  306. `with(this){return _c('input',{on:{"input":function($event){current++}}})}`
  307. )
  308. })
  309. it('generate events with inline function expression', () => {
  310. // normal function
  311. assertCodegen(
  312. '<input @input="function () { current++ }">',
  313. `with(this){return _c('input',{on:{"input":function () { current++ }}})}`
  314. )
  315. // arrow with no args
  316. assertCodegen(
  317. '<input @input="()=>current++">',
  318. `with(this){return _c('input',{on:{"input":()=>current++}})}`
  319. )
  320. // arrow with parens, single arg
  321. assertCodegen(
  322. '<input @input="(e) => current++">',
  323. `with(this){return _c('input',{on:{"input":(e) => current++}})}`
  324. )
  325. // arrow with parens, multi args
  326. assertCodegen(
  327. '<input @input="(a, b, c) => current++">',
  328. `with(this){return _c('input',{on:{"input":(a, b, c) => current++}})}`
  329. )
  330. // arrow with destructuring
  331. assertCodegen(
  332. '<input @input="({ a, b }) => current++">',
  333. `with(this){return _c('input',{on:{"input":({ a, b }) => current++}})}`
  334. )
  335. // arrow single arg no parens
  336. assertCodegen(
  337. '<input @input="e=>current++">',
  338. `with(this){return _c('input',{on:{"input":e=>current++}})}`
  339. )
  340. // with modifiers
  341. assertCodegen(
  342. `<input @keyup.enter="e=>current++">`,
  343. `with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;(e=>current++)($event)}}})}`
  344. )
  345. })
  346. // #3893
  347. it('should not treat handler with unexpected whitespace as inline statement', () => {
  348. assertCodegen(
  349. '<input @input=" onInput ">',
  350. `with(this){return _c('input',{on:{"input": onInput }})}`
  351. )
  352. })
  353. it('generate unhandled events', () => {
  354. assertCodegen(
  355. '<input @input="current++">',
  356. `with(this){return _c('input',{on:{"input":function(){}}})}`,
  357. ast => {
  358. ast.events.input = undefined
  359. }
  360. )
  361. })
  362. it('generate multiple event handlers', () => {
  363. assertCodegen(
  364. '<input @input="current++" @input.stop="onInput">',
  365. `with(this){return _c('input',{on:{"input":[function($event){current++},function($event){$event.stopPropagation();onInput($event)}]}})}`
  366. )
  367. })
  368. it('generate component', () => {
  369. assertCodegen(
  370. '<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
  371. `with(this){return _c('my-component',{attrs:{"name":"mycomponent1","msg":msg},on:{"notify":onNotify}},[_c('div',[_v("hi")])])}`
  372. )
  373. })
  374. it('generate svg component with children', () => {
  375. assertCodegen(
  376. '<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
  377. `with(this){return _c('svg',[_c('my-comp',[_c('circle',{attrs:{"r":10}})])],1)}`
  378. )
  379. })
  380. it('generate is attribute', () => {
  381. assertCodegen(
  382. '<div is="component1"></div>',
  383. `with(this){return _c("component1",{tag:"div"})}`
  384. )
  385. assertCodegen(
  386. '<div :is="component1"></div>',
  387. `with(this){return _c(component1,{tag:"div"})}`
  388. )
  389. })
  390. it('generate component with inline-template', () => {
  391. // have "inline-template'"
  392. assertCodegen(
  393. '<my-component inline-template><p><span>hello world</span></p></my-component>',
  394. `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")])])}}]}})}`
  395. )
  396. // "have inline-template attrs, but not having exactly one child element
  397. assertCodegen(
  398. '<my-component inline-template><hr><hr></my-component>',
  399. `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _c('hr')}},staticRenderFns:[]}})}`
  400. )
  401. expect('Inline-template components must have exactly one child element.').toHaveBeenWarned()
  402. })
  403. it('generate static trees inside v-for', () => {
  404. assertCodegen(
  405. `<div><div v-for="i in 10"><p><span></span></p></div></div>`,
  406. `with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true)])}))}`,
  407. [`with(this){return _c('p',[_c('span')])}`]
  408. )
  409. })
  410. it('generate component with v-for', () => {
  411. // normalize type: 2
  412. assertCodegen(
  413. '<div><child></child><template v-for="item in list">{{ item }}</template></div>',
  414. `with(this){return _c('div',[_c('child'),_l((list),function(item){return [_v(_s(item))]})],2)}`
  415. )
  416. })
  417. it('not specified ast type', () => {
  418. const res = generate(null, baseOptions)
  419. expect(res.render).toBe(`with(this){return _c("div")}`)
  420. expect(res.staticRenderFns).toEqual([])
  421. })
  422. it('not specified directives option', () => {
  423. assertCodegen(
  424. '<p v-if="show">hello world</p>',
  425. `with(this){return (show)?_c('p',[_v("hello world")]):_e()}`,
  426. { isReservedTag }
  427. )
  428. })
  429. })
  430. /* eslint-enable quotes */