codegen.spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.modifire="value1" v-custom2><p>',
  34. `with(this){return _h('p',{directives:[{name:"custom1",value:(value1),expression:"value1",arg:"arg1",modifiers:{"modifire":true}},{name:"custom2",arg:"arg1"}]})}`
  35. )
  36. })
  37. it('generate v-for directive', () => {
  38. assertCodegen(
  39. '<li v-for="item in items" :key="item.uid"></li>',
  40. `with(this){return (items)&&_l((items),function(item){return _h('li',{key:item.uid})})}`
  41. )
  42. // iterator syntax
  43. assertCodegen(
  44. '<li v-for="(item, i) in items"></li>',
  45. `with(this){return (items)&&_l((items),function(item,i){return _h('li')})}`
  46. )
  47. assertCodegen(
  48. '<li v-for="(item, key, index) in items"></li>',
  49. `with(this){return (items)&&_l((items),function(item,key,index){return _h('li')})}`
  50. )
  51. })
  52. it('generate v-if directive', () => {
  53. assertCodegen(
  54. '<p v-if="show">hello</p>',
  55. `with(this){return (show)?_h('p',["hello"]):_e()}`
  56. )
  57. })
  58. it('generate v-else directive', () => {
  59. assertCodegen(
  60. '<div><p v-if="show">hello</p><p v-else>world</p></div>',
  61. `with(this){return _h('div',[(show)?_h('p',["hello"]):_h('p',["world"])])}`
  62. )
  63. })
  64. it('generate ref', () => {
  65. assertCodegen(
  66. '<p ref="component1"></p>',
  67. `with(this){return _h('p',{ref:"component1"})}`
  68. )
  69. })
  70. it('generate ref on v-for', () => {
  71. assertCodegen(
  72. '<ul><li v-for="item in items" ref="component1"></li></ul>',
  73. `with(this){return _h('ul',[(items)&&_l((items),function(item){return _h('li',{ref:"component1",refInFor:true})})])}`
  74. )
  75. })
  76. it('generate v-bind directive', () => {
  77. assertCodegen(
  78. '<p v-bind="test"></p>',
  79. `with(this){return _h('p',{hook:{"construct":function(n1,n2){_b(n1,test)}}})}`
  80. )
  81. })
  82. it('generate template tag', () => {
  83. assertCodegen(
  84. '<template><p>{{hello}}</p></template>',
  85. `with(this){return [_h('p',[_s(hello)])]}`
  86. )
  87. })
  88. it('generate single slot', () => {
  89. assertCodegen(
  90. '<slot></slot>',
  91. `with(this){return $slots["default"]}`
  92. )
  93. })
  94. it('generate named slot', () => {
  95. assertCodegen(
  96. '<slot name="one"></slot>',
  97. `with(this){return $slots["one"]}`
  98. )
  99. })
  100. it('generate slot fallback content', () => {
  101. assertCodegen(
  102. '<slot><div>hi</div></slot>',
  103. `with(this){return ($slots["default"]||[_m(0)])}`,
  104. [`with(this){return _h('div',["hi"])}`]
  105. )
  106. })
  107. it('generate slot target', () => {
  108. assertCodegen(
  109. '<p slot="one">hello world</p>',
  110. `with(this){return _h('p',{slot:"one"},["hello world"])}`
  111. )
  112. })
  113. it('generate class binding', () => {
  114. // static
  115. assertCodegen(
  116. '<p class="class1">hello world</p>',
  117. 'with(this){return _m(0)}',
  118. [`with(this){return _h('p',{staticClass:"class1"},["hello world"])}`]
  119. )
  120. // dynamic
  121. assertCodegen(
  122. '<p :class="class1">hello world</p>',
  123. `with(this){return _h('p',{class:class1},["hello world"])}`
  124. )
  125. })
  126. it('generate style binding', () => {
  127. assertCodegen(
  128. '<p :style="error">hello world</p>',
  129. `with(this){return _h('p',{style:(error)},["hello world"])}`
  130. )
  131. })
  132. it('generate v-show directive', () => {
  133. assertCodegen(
  134. '<p v-show="shown">hello world</p>',
  135. `with(this){return _h('p',{directives:[{name:"show",value:(shown),expression:"shown"}]},["hello world"])}`
  136. )
  137. })
  138. it('generate DOM props with v-bind directive', () => {
  139. assertCodegen(
  140. '<p :value="msg">',
  141. `with(this){return _h('p',{domProps:{"value":msg}})}`
  142. )
  143. })
  144. it('generate attrs with v-bind directive', () => {
  145. assertCodegen(
  146. '<input :name="field1">',
  147. `with(this){return _h('input',{attrs:{"name":field1}})}`
  148. )
  149. })
  150. it('generate static attrs', () => {
  151. assertCodegen(
  152. '<input name="field1">',
  153. `with(this){return _m(0)}`,
  154. [`with(this){return _h('input',{attrs:{"name":"field1"}})}`]
  155. )
  156. })
  157. it('generate events with v-on directive', () => {
  158. assertCodegen(
  159. '<input @input="onInput">',
  160. `with(this){return _h('input',{on:{"input":onInput}})}`
  161. )
  162. })
  163. it('generate events with keycode', () => {
  164. assertCodegen(
  165. '<input @input.enter="onInput">',
  166. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}})}`
  167. )
  168. // multiple keycodes (delete)
  169. assertCodegen(
  170. '<input @input.delete="onInput">',
  171. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}`
  172. )
  173. // multiple keycodes (chained)
  174. assertCodegen(
  175. '<input @keydown.enter.delete="onInput">',
  176. `with(this){return _h('input',{on:{"keydown":function($event){if($event.keyCode!==13&&$event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}`
  177. )
  178. // number keycode
  179. assertCodegen(
  180. '<input @input.13="onInput">',
  181. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}})}`
  182. )
  183. // custom keycode
  184. assertCodegen(
  185. '<input @input.custom="onInput">',
  186. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==_k("custom"))return;onInput($event)}}})}`
  187. )
  188. })
  189. it('generate events with modifiers', () => {
  190. assertCodegen(
  191. '<input @input.stop="onInput">',
  192. `with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}})}`
  193. )
  194. assertCodegen(
  195. '<input @input.prevent="onInput">',
  196. `with(this){return _h('input',{on:{"input":function($event){$event.preventDefault();onInput($event)}}})}`
  197. )
  198. assertCodegen(
  199. '<input @input.self="onInput">',
  200. `with(this){return _h('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return;onInput($event)}}})}`
  201. )
  202. })
  203. it('generate events with multiple modifers', () => {
  204. assertCodegen(
  205. '<input @input.stop.prevent.self="onInput">',
  206. `with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return;onInput($event)}}})}`
  207. )
  208. })
  209. it('generate events with capture modifier', () => {
  210. assertCodegen(
  211. '<input @input.capture="onInput">',
  212. `with(this){return _h('input',{on:{"!input":function($event){onInput($event)}}})}`
  213. )
  214. })
  215. it('generate events with inline statement', () => {
  216. assertCodegen(
  217. '<input @input="curent++">',
  218. `with(this){return _h('input',{on:{"input":function($event){curent++}}})}`
  219. )
  220. })
  221. it('generate unhandled events', () => {
  222. assertCodegen(
  223. '<input @input="curent++">',
  224. `with(this){return _h('input',{on:{"input":function(){}}})}`,
  225. ast => {
  226. ast.events.input = undefined
  227. }
  228. )
  229. })
  230. it('generate multiple event handlers', () => {
  231. assertCodegen(
  232. '<input @input="curent++" @input="onInput">',
  233. `with(this){return _h('input',{on:{"input":[function($event){curent++},onInput]}})}`
  234. )
  235. })
  236. it('generate component', () => {
  237. assertCodegen(
  238. '<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
  239. `with(this){return _h('my-component',{attrs:{"name":"mycomponent1","msg":msg},on:{"notify":onNotify}},[_m(0)])}`,
  240. [`with(this){return _h('div',["hi"])}`]
  241. )
  242. })
  243. it('generate svg component with children', () => {
  244. assertCodegen(
  245. '<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
  246. `with(this){return _h('svg',[_h('my-comp',[_h('circle',{attrs:{"r":10}})])])}`
  247. )
  248. })
  249. it('generate is attribute', () => {
  250. assertCodegen(
  251. '<div is="component1"></div>',
  252. `with(this){return _h("component1",{tag:"div"})}`
  253. )
  254. assertCodegen(
  255. '<div :is="component1"></div>',
  256. `with(this){return _h(component1,{tag:"div"})}`
  257. )
  258. })
  259. it('generate component with inline-template', () => {
  260. // have "inline-template'"
  261. assertCodegen(
  262. '<my-component inline-template><p>hello world</p></my-component>',
  263. `with(this){return _h('my-component',{inlineTemplate:{render:function(){with(this){return _m(0)}},staticRenderFns:[function(){with(this){return _h('p',["hello world"])}}]}})}`
  264. )
  265. // "have inline-template attrs, but not having extactly one child element
  266. assertCodegen(
  267. '<my-component inline-template><hr><hr></my-component>',
  268. `with(this){return _h('my-component',{inlineTemplate:{render:function(){with(this){return _m(0)}},staticRenderFns:[function(){with(this){return _h('hr')}}]}})}`
  269. )
  270. expect('Inline-template components must have exactly one child element.').toHaveBeenWarned()
  271. })
  272. it('generate static trees inside v-for', () => {
  273. assertCodegen(
  274. `<div><div v-for="i in 10"><span></span></div></div>`,
  275. `with(this){return _h('div',[(10)&&_l((10),function(i){return _h('div',[_m(0,true)])})])}`,
  276. [`with(this){return _h('span')}`]
  277. )
  278. })
  279. it('not specified ast type', () => {
  280. const res = generate(null, baseOptions)
  281. expect(res.render).toBe(`with(this){return _h("div")}`)
  282. expect(res.staticRenderFns).toEqual([])
  283. })
  284. it('not specified directives option', () => {
  285. assertCodegen(
  286. '<p v-if="show">hello world</p>',
  287. `with(this){return (show)?_h('p',["hello world"]):_e()}`,
  288. { isReservedTag }
  289. )
  290. })
  291. })
  292. /* eslint-enable quotes */