codegen.spec.js 13 KB

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