prefixIdentifiers.spec.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { prefixIdentifiers } from '../src/prefixIdentifiers'
  2. import { compile } from 'web/entry-compiler'
  3. import { format } from 'prettier'
  4. import { BindingTypes } from '../src/types'
  5. const toFn = (source: string) => `function render(){${source}\n}`
  6. it('should work', () => {
  7. const { render } = compile(`<div id="app">
  8. <div :style="{ color }">{{ foo }}</div>
  9. <p v-for="i in list">{{ i }}</p>
  10. <foo inline-template>
  11. <div>{{ bar }}</div>
  12. </foo>
  13. </div>`)
  14. const result = format(prefixIdentifiers(toFn(render)), {
  15. semi: false,
  16. parser: 'babel'
  17. })
  18. expect(result).not.toMatch(`_vm._c`)
  19. expect(result).toMatch(`_vm.foo`)
  20. expect(result).toMatch(`_vm.list`)
  21. expect(result).toMatch(`{ color: _vm.color }`)
  22. expect(result).not.toMatch(`_vm.i`)
  23. expect(result).not.toMatch(`with (this)`)
  24. expect(result).toMatchInlineSnapshot(`
  25. "function render() {
  26. var _vm = this,
  27. _c = _vm._self._c
  28. return _c(
  29. \\"div\\",
  30. { attrs: { id: \\"app\\" } },
  31. [
  32. _c(\\"div\\", { style: { color: _vm.color } }, [_vm._v(_vm._s(_vm.foo))]),
  33. _vm._v(\\" \\"),
  34. _vm._l(_vm.list, function (i) {
  35. return _c(\\"p\\", [_vm._v(_vm._s(i))])
  36. }),
  37. _vm._v(\\" \\"),
  38. _c(\\"foo\\", {
  39. inlineTemplate: {
  40. render: function () {
  41. var _vm = this,
  42. _c = _vm._self._c
  43. return _c(\\"div\\", [_vm._v(_vm._s(_vm.bar))])
  44. },
  45. staticRenderFns: [],
  46. },
  47. }),
  48. ],
  49. 2
  50. )
  51. }
  52. "
  53. `)
  54. })
  55. it('setup bindings', () => {
  56. const { render } = compile(`<div @click="count++">{{ count }}</div>`)
  57. const result = format(
  58. prefixIdentifiers(toFn(render), false, false, undefined, {
  59. count: BindingTypes.SETUP_REF
  60. }),
  61. {
  62. semi: false,
  63. parser: 'babel'
  64. }
  65. )
  66. expect(result).toMatch(`_setup = _vm._self._setupProxy`)
  67. expect(result).toMatch(`_setup.count++`)
  68. expect(result).toMatch(`_vm._s(_setup.count)`)
  69. expect(result).toMatchInlineSnapshot(`
  70. "function render() {
  71. var _vm = this,
  72. _c = _vm._self._c,
  73. _setup = _vm._self._setupProxy
  74. return _c(
  75. \\"div\\",
  76. {
  77. on: {
  78. click: function (\$event) {
  79. _setup.count++
  80. },
  81. },
  82. },
  83. [_vm._v(_vm._s(_setup.count))]
  84. )
  85. }
  86. "
  87. `)
  88. })