codegen.spec.js 14 KB

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