codegen.spec.js 10 KB

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