codegen.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 filters', () => {
  38. assertCodegen(
  39. '<div :id="a | b | c">{{ d | e | f }}</div>',
  40. `with(this){return _h('div',{attrs:{"id":_f("c")(_f("b")(a))}},[_s(_f("f")(_f("e")(d)))])}`
  41. )
  42. })
  43. it('generate v-for directive', () => {
  44. assertCodegen(
  45. '<li v-for="item in items" :key="item.uid"></li>',
  46. `with(this){return _l((items),function(item){return _h('li',{key:item.uid})})}`
  47. )
  48. // iterator syntax
  49. assertCodegen(
  50. '<li v-for="(item, i) in items"></li>',
  51. `with(this){return _l((items),function(item,i){return _h('li')})}`
  52. )
  53. assertCodegen(
  54. '<li v-for="(item, key, index) in items"></li>',
  55. `with(this){return _l((items),function(item,key,index){return _h('li')})}`
  56. )
  57. })
  58. it('generate v-if directive', () => {
  59. assertCodegen(
  60. '<p v-if="show">hello</p>',
  61. `with(this){return (show)?_h('p',["hello"]):_e()}`
  62. )
  63. })
  64. it('generate v-else directive', () => {
  65. assertCodegen(
  66. '<div><p v-if="show">hello</p><p v-else>world</p></div>',
  67. `with(this){return _h('div',[(show)?_h('p',["hello"]):_h('p',["world"])])}`
  68. )
  69. })
  70. it('generate ref', () => {
  71. assertCodegen(
  72. '<p ref="component1"></p>',
  73. `with(this){return _h('p',{ref:"component1"})}`
  74. )
  75. })
  76. it('generate ref on v-for', () => {
  77. assertCodegen(
  78. '<ul><li v-for="item in items" ref="component1"></li></ul>',
  79. `with(this){return _h('ul',[_l((items),function(item){return _h('li',{ref:"component1",refInFor:true})})])}`
  80. )
  81. })
  82. it('generate v-bind directive', () => {
  83. assertCodegen(
  84. '<p v-bind="test"></p>',
  85. `with(this){return _h('p',_b({},'p',test))}`
  86. )
  87. })
  88. it('generate template tag', () => {
  89. assertCodegen(
  90. '<template><p>{{hello}}</p></template>',
  91. `with(this){return [_h('p',[_s(hello)])]}`
  92. )
  93. })
  94. it('generate single slot', () => {
  95. assertCodegen(
  96. '<slot></slot>',
  97. `with(this){return _t("default")}`
  98. )
  99. })
  100. it('generate named slot', () => {
  101. assertCodegen(
  102. '<slot name="one"></slot>',
  103. `with(this){return _t("one")}`
  104. )
  105. })
  106. it('generate slot fallback content', () => {
  107. assertCodegen(
  108. '<slot><div>hi</div></slot>',
  109. `with(this){return _t("default",[_h('div',["hi"])])}`
  110. )
  111. })
  112. it('generate slot target', () => {
  113. assertCodegen(
  114. '<p slot="one">hello world</p>',
  115. `with(this){return _h('p',{slot:"one"},["hello world"])}`
  116. )
  117. })
  118. it('generate class binding', () => {
  119. // static
  120. assertCodegen(
  121. '<p class="class1">hello world</p>',
  122. `with(this){return _h('p',{staticClass:"class1"},["hello world"])}`,
  123. )
  124. // dynamic
  125. assertCodegen(
  126. '<p :class="class1">hello world</p>',
  127. `with(this){return _h('p',{class:class1},["hello world"])}`
  128. )
  129. })
  130. it('generate style binding', () => {
  131. assertCodegen(
  132. '<p :style="error">hello world</p>',
  133. `with(this){return _h('p',{style:(error)},["hello world"])}`
  134. )
  135. })
  136. it('generate v-show directive', () => {
  137. assertCodegen(
  138. '<p v-show="shown">hello world</p>',
  139. `with(this){return _h('p',{directives:[{name:"show",rawName:"v-show",value:(shown),expression:"shown"}]},["hello world"])}`
  140. )
  141. })
  142. it('generate DOM props with v-bind directive', () => {
  143. // input + value
  144. assertCodegen(
  145. '<input :value="msg">',
  146. `with(this){return _h('input',{domProps:{"value":msg}})}`
  147. )
  148. // non input
  149. assertCodegen(
  150. '<p :value="msg">',
  151. `with(this){return _h('p',{attrs:{"value":msg}})}`
  152. )
  153. })
  154. it('generate attrs with v-bind directive', () => {
  155. assertCodegen(
  156. '<input :name="field1">',
  157. `with(this){return _h('input',{attrs:{"name":field1}})}`
  158. )
  159. })
  160. it('generate static attrs', () => {
  161. assertCodegen(
  162. '<input name="field1">',
  163. `with(this){return _h('input',{attrs:{"name":"field1"}})}`
  164. )
  165. })
  166. it('generate events with v-on directive', () => {
  167. assertCodegen(
  168. '<input @input="onInput">',
  169. `with(this){return _h('input',{on:{"input":onInput}})}`
  170. )
  171. })
  172. it('generate events with keycode', () => {
  173. assertCodegen(
  174. '<input @input.enter="onInput">',
  175. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}})}`
  176. )
  177. // multiple keycodes (delete)
  178. assertCodegen(
  179. '<input @input.delete="onInput">',
  180. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}`
  181. )
  182. // multiple keycodes (chained)
  183. assertCodegen(
  184. '<input @keydown.enter.delete="onInput">',
  185. `with(this){return _h('input',{on:{"keydown":function($event){if($event.keyCode!==13&&$event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}`
  186. )
  187. // number keycode
  188. assertCodegen(
  189. '<input @input.13="onInput">',
  190. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}})}`
  191. )
  192. // custom keycode
  193. assertCodegen(
  194. '<input @input.custom="onInput">',
  195. `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==_k("custom"))return;onInput($event)}}})}`
  196. )
  197. })
  198. it('generate events with modifiers', () => {
  199. assertCodegen(
  200. '<input @input.stop="onInput">',
  201. `with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}})}`
  202. )
  203. assertCodegen(
  204. '<input @input.prevent="onInput">',
  205. `with(this){return _h('input',{on:{"input":function($event){$event.preventDefault();onInput($event)}}})}`
  206. )
  207. assertCodegen(
  208. '<input @input.self="onInput">',
  209. `with(this){return _h('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return;onInput($event)}}})}`
  210. )
  211. assertCodegen(
  212. '<input @input.ctrl="onInput">',
  213. `with(this){return _h('input',{on:{"input":function($event){if(!$event.ctrlKey)return;onInput($event)}}})}`
  214. )
  215. assertCodegen(
  216. '<input @input.shift="onInput">',
  217. `with(this){return _h('input',{on:{"input":function($event){if(!$event.shiftKey)return;onInput($event)}}})}`
  218. )
  219. assertCodegen(
  220. '<input @input.alt="onInput">',
  221. `with(this){return _h('input',{on:{"input":function($event){if(!$event.altKey)return;onInput($event)}}})}`
  222. )
  223. assertCodegen(
  224. '<input @input.meta="onInput">',
  225. `with(this){return _h('input',{on:{"input":function($event){if(!$event.metaKey)return;onInput($event)}}})}`
  226. )
  227. })
  228. it('generate events with multiple modifers', () => {
  229. assertCodegen(
  230. '<input @input.stop.prevent.self="onInput">',
  231. `with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return;onInput($event)}}})}`
  232. )
  233. })
  234. it('generate events with capture modifier', () => {
  235. assertCodegen(
  236. '<input @input.capture="onInput">',
  237. `with(this){return _h('input',{on:{"!input":function($event){onInput($event)}}})}`
  238. )
  239. })
  240. it('generate events with inline statement', () => {
  241. assertCodegen(
  242. '<input @input="curent++">',
  243. `with(this){return _h('input',{on:{"input":function($event){curent++}}})}`
  244. )
  245. })
  246. it('generate events with inline function expression', () => {
  247. // normal function
  248. assertCodegen(
  249. '<input @input="function () { current++ }">',
  250. `with(this){return _h('input',{on:{"input":function () { current++ }}})}`
  251. )
  252. // arrow with no args
  253. assertCodegen(
  254. '<input @input="()=>current++">',
  255. `with(this){return _h('input',{on:{"input":()=>current++}})}`
  256. )
  257. // arrow with parens, single arg
  258. assertCodegen(
  259. '<input @input="(e) => current++">',
  260. `with(this){return _h('input',{on:{"input":(e) => current++}})}`
  261. )
  262. // arrow with parens, multi args
  263. assertCodegen(
  264. '<input @input="(a, b, c) => current++">',
  265. `with(this){return _h('input',{on:{"input":(a, b, c) => current++}})}`
  266. )
  267. // arrow with destructuring
  268. assertCodegen(
  269. '<input @input="({ a, b }) => current++">',
  270. `with(this){return _h('input',{on:{"input":({ a, b }) => current++}})}`
  271. )
  272. // arrow single arg no parens
  273. assertCodegen(
  274. '<input @input="e=>current++">',
  275. `with(this){return _h('input',{on:{"input":e=>current++}})}`
  276. )
  277. })
  278. // #3893
  279. it('should not treat handler with unexpected whitespace as inline statement', () => {
  280. assertCodegen(
  281. '<input @input=" onInput ">',
  282. `with(this){return _h('input',{on:{"input": onInput }})}`
  283. )
  284. })
  285. it('generate unhandled events', () => {
  286. assertCodegen(
  287. '<input @input="curent++">',
  288. `with(this){return _h('input',{on:{"input":function(){}}})}`,
  289. ast => {
  290. ast.events.input = undefined
  291. }
  292. )
  293. })
  294. it('generate multiple event handlers', () => {
  295. assertCodegen(
  296. '<input @input="curent++" @input="onInput">',
  297. `with(this){return _h('input',{on:{"input":[function($event){curent++},onInput]}})}`
  298. )
  299. })
  300. it('generate component', () => {
  301. assertCodegen(
  302. '<my-component name="mycomponent1" :msg="msg" @notify="onNotify"><div>hi</div></my-component>',
  303. `with(this){return _h('my-component',{attrs:{"name":"mycomponent1","msg":msg},on:{"notify":onNotify}},[_h('div',["hi"])])}`
  304. )
  305. })
  306. it('generate svg component with children', () => {
  307. assertCodegen(
  308. '<svg><my-comp><circle :r="10"></circle></my-comp></svg>',
  309. `with(this){return _h('svg',[_h('my-comp',[_h('circle',{attrs:{"r":10}})])])}`
  310. )
  311. })
  312. it('generate is attribute', () => {
  313. assertCodegen(
  314. '<div is="component1"></div>',
  315. `with(this){return _h("component1",{tag:"div"})}`
  316. )
  317. assertCodegen(
  318. '<div :is="component1"></div>',
  319. `with(this){return _h(component1,{tag:"div"})}`
  320. )
  321. })
  322. it('generate component with inline-template', () => {
  323. // have "inline-template'"
  324. assertCodegen(
  325. '<my-component inline-template><p><span>hello world</span></p></my-component>',
  326. `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"])])}}]}})}`
  327. )
  328. // "have inline-template attrs, but not having extactly one child element
  329. assertCodegen(
  330. '<my-component inline-template><hr><hr></my-component>',
  331. `with(this){return _h('my-component',{inlineTemplate:{render:function(){with(this){return _h('hr')}},staticRenderFns:[]}})}`
  332. )
  333. expect('Inline-template components must have exactly one child element.').toHaveBeenWarned()
  334. })
  335. it('generate static trees inside v-for', () => {
  336. assertCodegen(
  337. `<div><div v-for="i in 10"><p><span></span></p></div></div>`,
  338. `with(this){return _h('div',[_l((10),function(i){return _h('div',[_m(0,true)])})])}`,
  339. [`with(this){return _h('p',[_h('span')])}`]
  340. )
  341. })
  342. it('not specified ast type', () => {
  343. const res = generate(null, baseOptions)
  344. expect(res.render).toBe(`with(this){return _h("div")}`)
  345. expect(res.staticRenderFns).toEqual([])
  346. })
  347. it('not specified directives option', () => {
  348. assertCodegen(
  349. '<p v-if="show">hello world</p>',
  350. `with(this){return (show)?_h('p',["hello world"]):_e()}`,
  351. { isReservedTag }
  352. )
  353. })
  354. })
  355. /* eslint-enable quotes */