codegen.spec.js 11 KB

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