codegen.spec.js 10 KB

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