codegen.spec.js 11 KB

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