codegen.spec.js 11 KB

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