codegen.spec.js 14 KB

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