compileScript.spec.ts 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. import { BindingTypes } from '../src/types'
  2. import { compile, assertCode } from './util'
  3. describe('SFC compile <script setup>', () => {
  4. test('should expose top level declarations', () => {
  5. const { content, bindings } = compile(`
  6. <script setup>
  7. import { x } from './x'
  8. let a = 1
  9. const b = 2
  10. function c() {}
  11. class d {}
  12. </script>
  13. <script>
  14. import { xx } from './x'
  15. let aa = 1
  16. const bb = 2
  17. function cc() {}
  18. class dd {}
  19. </script>
  20. `)
  21. expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
  22. expect(bindings).toStrictEqual({
  23. x: BindingTypes.SETUP_MAYBE_REF,
  24. a: BindingTypes.SETUP_LET,
  25. b: BindingTypes.SETUP_CONST,
  26. c: BindingTypes.SETUP_CONST,
  27. d: BindingTypes.SETUP_CONST,
  28. xx: BindingTypes.SETUP_MAYBE_REF,
  29. aa: BindingTypes.SETUP_LET,
  30. bb: BindingTypes.SETUP_CONST,
  31. cc: BindingTypes.SETUP_CONST,
  32. dd: BindingTypes.SETUP_CONST
  33. })
  34. assertCode(content)
  35. })
  36. test('binding analysis for destructure', () => {
  37. const { content, bindings } = compile(`
  38. <script setup>
  39. const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
  40. </script>
  41. `)
  42. expect(content).toMatch('return { foo, bar, baz, y, z }')
  43. expect(bindings).toStrictEqual({
  44. foo: BindingTypes.SETUP_MAYBE_REF,
  45. bar: BindingTypes.SETUP_MAYBE_REF,
  46. baz: BindingTypes.SETUP_MAYBE_REF,
  47. y: BindingTypes.SETUP_MAYBE_REF,
  48. z: BindingTypes.SETUP_MAYBE_REF
  49. })
  50. assertCode(content)
  51. })
  52. test('defineProps()', () => {
  53. const { content, bindings } = compile(`
  54. <script setup>
  55. const props = defineProps({
  56. foo: String
  57. })
  58. const bar = 1
  59. </script>
  60. `)
  61. // should generate working code
  62. assertCode(content)
  63. // should analyze bindings
  64. expect(bindings).toStrictEqual({
  65. foo: BindingTypes.PROPS,
  66. bar: BindingTypes.SETUP_CONST,
  67. props: BindingTypes.SETUP_REACTIVE_CONST
  68. })
  69. // should remove defineOptions import and call
  70. expect(content).not.toMatch('defineProps')
  71. // should generate correct setup signature
  72. expect(content).toMatch(`setup(__props) {`)
  73. // should assign user identifier to it
  74. expect(content).toMatch(`const props = __props`)
  75. // should include context options in default export
  76. expect(content).toMatch(`export default {
  77. props: {
  78. foo: String
  79. },`)
  80. })
  81. test('defineProps w/ external definition', () => {
  82. const { content } = compile(`
  83. <script setup>
  84. import { propsModel } from './props'
  85. const props = defineProps(propsModel)
  86. </script>
  87. `)
  88. assertCode(content)
  89. expect(content).toMatch(`export default {
  90. props: propsModel,`)
  91. })
  92. // #4764
  93. test('defineProps w/ leading code', () => {
  94. const { content } = compile(`
  95. <script setup>import { x } from './x'
  96. const props = defineProps({})
  97. </script>
  98. `)
  99. // props declaration should be inside setup, not moved along with the import
  100. expect(content).not.toMatch(`const props = __props\nimport`)
  101. assertCode(content)
  102. })
  103. test('defineEmits()', () => {
  104. const { content, bindings } = compile(`
  105. <script setup>
  106. const myEmit = defineEmits(['foo', 'bar'])
  107. </script>
  108. `)
  109. assertCode(content)
  110. expect(bindings).toStrictEqual({
  111. myEmit: BindingTypes.SETUP_CONST
  112. })
  113. // should remove defineOptions import and call
  114. expect(content).not.toMatch('defineEmits')
  115. // should generate correct setup signature
  116. expect(content).toMatch(`setup(__props, { emit: myEmit }) {`)
  117. // should include context options in default export
  118. expect(content).toMatch(`export default {
  119. emits: ['foo', 'bar'],`)
  120. })
  121. test('defineProps/defineEmits in multi-variable declaration', () => {
  122. const { content } = compile(`
  123. <script setup>
  124. const props = defineProps(['item']),
  125. a = 1,
  126. emit = defineEmits(['a']);
  127. </script>
  128. `)
  129. assertCode(content)
  130. expect(content).toMatch(`const a = 1;`) // test correct removal
  131. expect(content).toMatch(`props: ['item'],`)
  132. expect(content).toMatch(`emits: ['a'],`)
  133. })
  134. // vuejs/core #6757
  135. test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
  136. const { content } = compile(`
  137. <script setup>
  138. const a = 1,
  139. props = defineProps(['item']),
  140. emit = defineEmits(['a']);
  141. </script>
  142. `)
  143. assertCode(content)
  144. expect(content).toMatch(`const a = 1;`) // test correct removal
  145. expect(content).toMatch(`props: ['item'],`)
  146. expect(content).toMatch(`emits: ['a'],`)
  147. })
  148. test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
  149. const { content } = compile(`
  150. <script setup>
  151. const props = defineProps(['item']),
  152. emit = defineEmits(['a']);
  153. </script>
  154. `)
  155. assertCode(content)
  156. expect(content).toMatch(`props: ['item'],`)
  157. expect(content).toMatch(`emits: ['a'],`)
  158. })
  159. test('defineExpose()', () => {
  160. const { content } = compile(`
  161. <script setup>
  162. defineExpose({ foo: 123 })
  163. </script>
  164. `)
  165. assertCode(content)
  166. // should remove defineOptions import and call
  167. expect(content).not.toMatch('defineExpose')
  168. // should generate correct setup signature
  169. expect(content).toMatch(`setup(__props, { expose }) {`)
  170. // should replace callee
  171. expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
  172. })
  173. test('<script> after <script setup> the script content not end with `\\n`', () => {
  174. const { content } = compile(`
  175. <script setup>
  176. import { x } from './x'
  177. </script>
  178. <script>const n = 1</script>
  179. `)
  180. assertCode(content)
  181. })
  182. describe('<script> and <script setup> co-usage', () => {
  183. test('script first', () => {
  184. const { content } = compile(`
  185. <script>
  186. export const n = 1
  187. export default {}
  188. </script>
  189. <script setup>
  190. import { x } from './x'
  191. x()
  192. </script>
  193. `)
  194. assertCode(content)
  195. })
  196. test('script setup first', () => {
  197. const { content } = compile(`
  198. <script setup>
  199. import { x } from './x'
  200. x()
  201. </script>
  202. <script>
  203. export const n = 1
  204. export default {}
  205. </script>
  206. `)
  207. assertCode(content)
  208. })
  209. test('script setup first, named default export', () => {
  210. const { content } = compile(`
  211. <script setup>
  212. import { x } from './x'
  213. x()
  214. </script>
  215. <script>
  216. export const n = 1
  217. const def = {}
  218. export { def as default }
  219. </script>
  220. `)
  221. assertCode(content)
  222. })
  223. // #4395
  224. test('script setup first, lang="ts", script block content export default', () => {
  225. const { content } = compile(`
  226. <script setup lang="ts">
  227. import { x } from './x'
  228. x()
  229. </script>
  230. <script lang="ts">
  231. export default {
  232. name: "test"
  233. }
  234. </script>
  235. `)
  236. // ensure __default__ is declared before used
  237. expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
  238. assertCode(content)
  239. })
  240. describe('spaces in ExportDefaultDeclaration node', () => {
  241. // #4371
  242. test('with many spaces and newline', () => {
  243. // #4371
  244. const { content } = compile(`
  245. <script>
  246. export const n = 1
  247. export default
  248. {
  249. some:'option'
  250. }
  251. </script>
  252. <script setup>
  253. import { x } from './x'
  254. x()
  255. </script>
  256. `)
  257. assertCode(content)
  258. })
  259. test('with minimal spaces', () => {
  260. const { content } = compile(`
  261. <script>
  262. export const n = 1
  263. export default{
  264. some:'option'
  265. }
  266. </script>
  267. <script setup>
  268. import { x } from './x'
  269. x()
  270. </script>
  271. `)
  272. assertCode(content)
  273. })
  274. })
  275. })
  276. describe('imports', () => {
  277. test('should hoist and expose imports', () => {
  278. assertCode(
  279. compile(`<script setup>
  280. import { ref } from 'vue'
  281. import 'foo/css'
  282. </script>`).content
  283. )
  284. })
  285. test('should extract comment for import or type declarations', () => {
  286. assertCode(
  287. compile(`
  288. <script setup>
  289. import a from 'a' // comment
  290. import b from 'b'
  291. </script>
  292. `).content
  293. )
  294. })
  295. // #2740
  296. test('should allow defineProps/Emit at the start of imports', () => {
  297. assertCode(
  298. compile(`<script setup>
  299. import { ref } from 'vue'
  300. defineProps(['foo'])
  301. defineEmits(['bar'])
  302. const r = ref(0)
  303. </script>`).content
  304. )
  305. })
  306. test('import dedupe between <script> and <script setup>', () => {
  307. const { content } = compile(`
  308. <script>
  309. import { x } from './x'
  310. </script>
  311. <script setup>
  312. import { x } from './x'
  313. x()
  314. </script>
  315. `)
  316. assertCode(content)
  317. expect(content.indexOf(`import { x }`)).toEqual(
  318. content.lastIndexOf(`import { x }`)
  319. )
  320. })
  321. })
  322. // in dev mode, declared bindings are returned as an object from setup()
  323. // when using TS, users may import types which should not be returned as
  324. // values, so we need to check import usage in the template to determine
  325. // what to be returned.
  326. describe('dev mode import usage check', () => {
  327. test('components', () => {
  328. const { content } = compile(`
  329. <script setup lang="ts">
  330. import { FooBar, FooBaz, FooQux, foo } from './x'
  331. const fooBar: FooBar = 1
  332. </script>
  333. <template>
  334. <FooBaz></FooBaz>
  335. <foo-qux/>
  336. <foo/>
  337. FooBar
  338. </template>
  339. `)
  340. // FooBar: should not be matched by plain text or incorrect case
  341. // FooBaz: used as PascalCase component
  342. // FooQux: used as kebab-case component
  343. // foo: lowercase component
  344. expect(content).toMatch(`return { fooBar, FooBaz, FooQux, foo }`)
  345. assertCode(content)
  346. })
  347. test('directive', () => {
  348. const { content } = compile(`
  349. <script setup lang="ts">
  350. import { vMyDir } from './x'
  351. </script>
  352. <template>
  353. <div v-my-dir></div>
  354. </template>
  355. `)
  356. expect(content).toMatch(`return { vMyDir }`)
  357. assertCode(content)
  358. })
  359. // https://github.com/vuejs/core/issues/4599
  360. test('attribute expressions', () => {
  361. const { content } = compile(`
  362. <script setup lang="ts">
  363. import { bar, baz } from './x'
  364. const cond = true
  365. </script>
  366. <template>
  367. <div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
  368. </template>
  369. `)
  370. expect(content).toMatch(`return { cond, bar, baz }`)
  371. assertCode(content)
  372. })
  373. test('imported ref as template ref', () => {
  374. const { content } = compile(`
  375. <script setup lang="ts">
  376. import { aref } from './x'
  377. </script>
  378. <template>
  379. <div ref="aref"></div>
  380. </template>
  381. `)
  382. expect(content).toMatch(`return { aref }`)
  383. assertCode(content)
  384. })
  385. test('vue interpolations', () => {
  386. const { content } = compile(`
  387. <script setup lang="ts">
  388. import { x, y, z, x$y } from './x'
  389. </script>
  390. <template>
  391. <div :id="z + 'y'">{{ x }} {{ yy }} {{ x$y }}</div>
  392. </template>
  393. `)
  394. // x: used in interpolation
  395. // y: should not be matched by {{ yy }} or 'y' in binding exps
  396. // x$y: #4274 should escape special chars when creating Regex
  397. expect(content).toMatch(`return { x, z, x$y }`)
  398. assertCode(content)
  399. })
  400. // #4340 interpolations in template strings
  401. test('js template string interpolations', () => {
  402. const { content } = compile(`
  403. <script setup lang="ts">
  404. import { VAR, VAR2, VAR3 } from './x'
  405. </script>
  406. <template>
  407. {{ \`\${VAR}VAR2\${VAR3}\` }}
  408. </template>
  409. `)
  410. // VAR2 should not be matched
  411. expect(content).toMatch(`return { VAR, VAR3 }`)
  412. assertCode(content)
  413. })
  414. // edge case: last tag in template
  415. test('last tag', () => {
  416. const { content } = compile(`
  417. <script setup lang="ts">
  418. import { FooBaz, Last } from './x'
  419. </script>
  420. <template>
  421. <FooBaz></FooBaz>
  422. <Last/>
  423. </template>
  424. `)
  425. expect(content).toMatch(`return { FooBaz, Last }`)
  426. assertCode(content)
  427. })
  428. test('TS annotations', () => {
  429. const { content } = compile(`
  430. <script setup lang="ts">
  431. import { Foo, Baz, Qux, Fred } from './x'
  432. const a = 1
  433. function b() {}
  434. </script>
  435. <template>
  436. {{ a as Foo }}
  437. {{ Baz }}
  438. <Comp v-slot="{ data }: Qux">{{ data }}</Comp>
  439. <div v-for="{ z = x as Qux } in list as Fred"/>
  440. </template>
  441. `)
  442. expect(content).toMatch(`return { a, b, Baz }`)
  443. assertCode(content)
  444. })
  445. })
  446. // describe('inlineTemplate mode', () => {
  447. // test('should work', () => {
  448. // const { content } = compile(
  449. // `
  450. // <script setup>
  451. // import { ref } from 'vue'
  452. // const count = ref(0)
  453. // </script>
  454. // <template>
  455. // <div>{{ count }}</div>
  456. // <div>static</div>
  457. // </template>
  458. // `,
  459. // { inlineTemplate: true }
  460. // )
  461. // // check snapshot and make sure helper imports and
  462. // // hoists are placed correctly.
  463. // assertCode(content)
  464. // // in inline mode, no need to call expose() since nothing is exposed
  465. // // anyway!
  466. // expect(content).not.toMatch(`expose()`)
  467. // })
  468. // test('with defineExpose()', () => {
  469. // const { content } = compile(
  470. // `
  471. // <script setup>
  472. // const count = ref(0)
  473. // defineExpose({ count })
  474. // </script>
  475. // `,
  476. // { inlineTemplate: true }
  477. // )
  478. // assertCode(content)
  479. // expect(content).toMatch(`setup(__props, { expose })`)
  480. // expect(content).toMatch(`expose({ count })`)
  481. // })
  482. // test('referencing scope components and directives', () => {
  483. // const { content } = compile(
  484. // `
  485. // <script setup>
  486. // import ChildComp from './Child.vue'
  487. // import SomeOtherComp from './Other.vue'
  488. // import vMyDir from './my-dir'
  489. // </script>
  490. // <template>
  491. // <div v-my-dir></div>
  492. // <ChildComp/>
  493. // <some-other-comp/>
  494. // </template>
  495. // `,
  496. // { inlineTemplate: true }
  497. // )
  498. // expect(content).toMatch('[_unref(vMyDir)]')
  499. // expect(content).toMatch('_createVNode(ChildComp)')
  500. // // kebab-case component support
  501. // expect(content).toMatch('_createVNode(SomeOtherComp)')
  502. // assertCode(content)
  503. // })
  504. // test('avoid unref() when necessary', () => {
  505. // // function, const, component import
  506. // const { content } = compile(
  507. // `<script setup>
  508. // import { ref } from 'vue'
  509. // import Foo, { bar } from './Foo.vue'
  510. // import other from './util'
  511. // import * as tree from './tree'
  512. // const count = ref(0)
  513. // const constant = {}
  514. // const maybe = foo()
  515. // let lett = 1
  516. // function fn() {}
  517. // </script>
  518. // <template>
  519. // <Foo>{{ bar }}</Foo>
  520. // <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  521. // {{ tree.foo() }}
  522. // </template>
  523. // `,
  524. // { inlineTemplate: true }
  525. // )
  526. // // no need to unref vue component import
  527. // expect(content).toMatch(`createVNode(Foo,`)
  528. // // #2699 should unref named imports from .vue
  529. // expect(content).toMatch(`unref(bar)`)
  530. // // should unref other imports
  531. // expect(content).toMatch(`unref(other)`)
  532. // // no need to unref constant literals
  533. // expect(content).not.toMatch(`unref(constant)`)
  534. // // should directly use .value for known refs
  535. // expect(content).toMatch(`count.value`)
  536. // // should unref() on const bindings that may be refs
  537. // expect(content).toMatch(`unref(maybe)`)
  538. // // should unref() on let bindings
  539. // expect(content).toMatch(`unref(lett)`)
  540. // // no need to unref namespace import (this also preserves tree-shaking)
  541. // expect(content).toMatch(`tree.foo()`)
  542. // // no need to unref function declarations
  543. // expect(content).toMatch(`{ onClick: fn }`)
  544. // // no need to mark constant fns in patch flag
  545. // expect(content).not.toMatch(`PROPS`)
  546. // assertCode(content)
  547. // })
  548. // test('v-model codegen', () => {
  549. // const { content } = compile(
  550. // `<script setup>
  551. // import { ref } from 'vue'
  552. // const count = ref(0)
  553. // const maybe = foo()
  554. // let lett = 1
  555. // </script>
  556. // <template>
  557. // <input v-model="count">
  558. // <input v-model="maybe">
  559. // <input v-model="lett">
  560. // </template>
  561. // `,
  562. // { inlineTemplate: true }
  563. // )
  564. // // known const ref: set value
  565. // expect(content).toMatch(`(count).value = $event`)
  566. // // const but maybe ref: assign if ref, otherwise do nothing
  567. // expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
  568. // // let: handle both cases
  569. // expect(content).toMatch(
  570. // `_isRef(lett) ? (lett).value = $event : lett = $event`
  571. // )
  572. // assertCode(content)
  573. // })
  574. // test('template assignment expression codegen', () => {
  575. // const { content } = compile(
  576. // `<script setup>
  577. // import { ref } from 'vue'
  578. // const count = ref(0)
  579. // const maybe = foo()
  580. // let lett = 1
  581. // let v = ref(1)
  582. // </script>
  583. // <template>
  584. // <div @click="count = 1"/>
  585. // <div @click="maybe = count"/>
  586. // <div @click="lett = count"/>
  587. // <div @click="v += 1"/>
  588. // <div @click="v -= 1"/>
  589. // <div @click="() => {
  590. // let a = '' + lett
  591. // v = a
  592. // }"/>
  593. // <div @click="() => {
  594. // // nested scopes
  595. // (()=>{
  596. // let x = a
  597. // (()=>{
  598. // let z = x
  599. // let z2 = z
  600. // })
  601. // let lz = z
  602. // })
  603. // v = a
  604. // }"/>
  605. // </template>
  606. // `,
  607. // { inlineTemplate: true }
  608. // )
  609. // // known const ref: set value
  610. // expect(content).toMatch(`count.value = 1`)
  611. // // const but maybe ref: only assign after check
  612. // expect(content).toMatch(`maybe.value = count.value`)
  613. // // let: handle both cases
  614. // expect(content).toMatch(
  615. // `_isRef(lett) ? lett.value = count.value : lett = count.value`
  616. // )
  617. // expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
  618. // expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
  619. // expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
  620. // expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
  621. // assertCode(content)
  622. // })
  623. // test('template update expression codegen', () => {
  624. // const { content } = compile(
  625. // `<script setup>
  626. // import { ref } from 'vue'
  627. // const count = ref(0)
  628. // const maybe = foo()
  629. // let lett = 1
  630. // </script>
  631. // <template>
  632. // <div @click="count++"/>
  633. // <div @click="--count"/>
  634. // <div @click="maybe++"/>
  635. // <div @click="--maybe"/>
  636. // <div @click="lett++"/>
  637. // <div @click="--lett"/>
  638. // </template>
  639. // `,
  640. // { inlineTemplate: true }
  641. // )
  642. // // known const ref: set value
  643. // expect(content).toMatch(`count.value++`)
  644. // expect(content).toMatch(`--count.value`)
  645. // // const but maybe ref (non-ref case ignored)
  646. // expect(content).toMatch(`maybe.value++`)
  647. // expect(content).toMatch(`--maybe.value`)
  648. // // let: handle both cases
  649. // expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  650. // expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  651. // assertCode(content)
  652. // })
  653. // test('template destructure assignment codegen', () => {
  654. // const { content } = compile(
  655. // `<script setup>
  656. // import { ref } from 'vue'
  657. // const val = {}
  658. // const count = ref(0)
  659. // const maybe = foo()
  660. // let lett = 1
  661. // </script>
  662. // <template>
  663. // <div @click="({ count } = val)"/>
  664. // <div @click="[maybe] = val"/>
  665. // <div @click="({ lett } = val)"/>
  666. // </template>
  667. // `,
  668. // { inlineTemplate: true }
  669. // )
  670. // // known const ref: set value
  671. // expect(content).toMatch(`({ count: count.value } = val)`)
  672. // // const but maybe ref (non-ref case ignored)
  673. // expect(content).toMatch(`[maybe.value] = val`)
  674. // // let: assumes non-ref
  675. // expect(content).toMatch(`{ lett: lett } = val`)
  676. // assertCode(content)
  677. // })
  678. // test('ssr codegen', () => {
  679. // const { content } = compile(
  680. // `
  681. // <script setup>
  682. // import { ref } from 'vue'
  683. // const count = ref(0)
  684. // </script>
  685. // <template>
  686. // <div>{{ count }}</div>
  687. // <div>static</div>
  688. // </template>
  689. // <style>
  690. // div { color: v-bind(count) }
  691. // </style>
  692. // `,
  693. // {
  694. // inlineTemplate: true,
  695. // templateOptions: {
  696. // ssr: true
  697. // }
  698. // }
  699. // )
  700. // expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  701. // expect(content).toMatch(`return (_ctx, _push`)
  702. // expect(content).toMatch(`ssrInterpolate`)
  703. // expect(content).not.toMatch(`useCssVars`)
  704. // expect(content).toMatch(`"--${mockId}-count": (count.value)`)
  705. // assertCode(content)
  706. // })
  707. // })
  708. describe('with TypeScript', () => {
  709. test('hoist type declarations', () => {
  710. const { content } = compile(`
  711. <script setup lang="ts">
  712. export interface Foo {}
  713. type Bar = {}
  714. </script>`)
  715. assertCode(content)
  716. })
  717. test('defineProps/Emit w/ runtime options', () => {
  718. const { content } = compile(`
  719. <script setup lang="ts">
  720. const props = defineProps({ foo: String })
  721. const emit = defineEmits(['a', 'b'])
  722. </script>
  723. `)
  724. assertCode(content)
  725. expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
  726. props: { foo: String },
  727. emits: ['a', 'b'],
  728. setup(__props, { emit }) {`)
  729. })
  730. test('defineProps w/ type', () => {
  731. const { content, bindings } = compile(`
  732. <script setup lang="ts">
  733. interface Test {}
  734. type Alias = number[]
  735. defineProps<{
  736. string: string
  737. number: number
  738. boolean: boolean
  739. object: object
  740. objectLiteral: { a: number }
  741. fn: (n: number) => void
  742. functionRef: Function
  743. objectRef: Object
  744. dateTime: Date
  745. array: string[]
  746. arrayRef: Array<any>
  747. tuple: [number, number]
  748. set: Set<string>
  749. literal: 'foo'
  750. optional?: any
  751. recordRef: Record<string, null>
  752. interface: Test
  753. alias: Alias
  754. method(): void
  755. symbol: symbol
  756. union: string | number
  757. literalUnion: 'foo' | 'bar'
  758. literalUnionNumber: 1 | 2 | 3 | 4 | 5
  759. literalUnionMixed: 'foo' | 1 | boolean
  760. intersection: Test & {}
  761. foo: ((item: any) => boolean) | null
  762. }>()
  763. </script>`)
  764. assertCode(content)
  765. expect(content).toMatch(`string: { type: String, required: true }`)
  766. expect(content).toMatch(`number: { type: Number, required: true }`)
  767. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  768. expect(content).toMatch(`object: { type: Object, required: true }`)
  769. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  770. expect(content).toMatch(`fn: { type: Function, required: true }`)
  771. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  772. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  773. expect(content).toMatch(`dateTime: { type: Date, required: true }`)
  774. expect(content).toMatch(`array: { type: Array, required: true }`)
  775. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  776. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  777. expect(content).toMatch(`set: { type: Set, required: true }`)
  778. expect(content).toMatch(`literal: { type: String, required: true }`)
  779. expect(content).toMatch(`optional: { type: null, required: false }`)
  780. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  781. expect(content).toMatch(`interface: { type: Object, required: true }`)
  782. expect(content).toMatch(`alias: { type: Array, required: true }`)
  783. expect(content).toMatch(`method: { type: Function, required: true }`)
  784. expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
  785. expect(content).toMatch(
  786. `union: { type: [String, Number], required: true }`
  787. )
  788. expect(content).toMatch(`literalUnion: { type: String, required: true }`)
  789. expect(content).toMatch(
  790. `literalUnionNumber: { type: Number, required: true }`
  791. )
  792. expect(content).toMatch(
  793. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  794. )
  795. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  796. expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
  797. expect(bindings).toStrictEqual({
  798. string: BindingTypes.PROPS,
  799. number: BindingTypes.PROPS,
  800. boolean: BindingTypes.PROPS,
  801. object: BindingTypes.PROPS,
  802. objectLiteral: BindingTypes.PROPS,
  803. fn: BindingTypes.PROPS,
  804. functionRef: BindingTypes.PROPS,
  805. objectRef: BindingTypes.PROPS,
  806. dateTime: BindingTypes.PROPS,
  807. array: BindingTypes.PROPS,
  808. arrayRef: BindingTypes.PROPS,
  809. tuple: BindingTypes.PROPS,
  810. set: BindingTypes.PROPS,
  811. literal: BindingTypes.PROPS,
  812. optional: BindingTypes.PROPS,
  813. recordRef: BindingTypes.PROPS,
  814. interface: BindingTypes.PROPS,
  815. alias: BindingTypes.PROPS,
  816. method: BindingTypes.PROPS,
  817. symbol: BindingTypes.PROPS,
  818. union: BindingTypes.PROPS,
  819. literalUnion: BindingTypes.PROPS,
  820. literalUnionNumber: BindingTypes.PROPS,
  821. literalUnionMixed: BindingTypes.PROPS,
  822. intersection: BindingTypes.PROPS,
  823. foo: BindingTypes.PROPS
  824. })
  825. })
  826. test('defineProps w/ interface', () => {
  827. const { content, bindings } = compile(`
  828. <script setup lang="ts">
  829. interface Props { x?: number }
  830. defineProps<Props>()
  831. </script>
  832. `)
  833. assertCode(content)
  834. expect(content).toMatch(`x: { type: Number, required: false }`)
  835. expect(bindings).toStrictEqual({
  836. x: BindingTypes.PROPS
  837. })
  838. })
  839. test('defineProps w/ exported interface', () => {
  840. const { content, bindings } = compile(`
  841. <script setup lang="ts">
  842. export interface Props { x?: number }
  843. defineProps<Props>()
  844. </script>
  845. `)
  846. assertCode(content)
  847. expect(content).toMatch(`x: { type: Number, required: false }`)
  848. expect(bindings).toStrictEqual({
  849. x: BindingTypes.PROPS
  850. })
  851. })
  852. test('defineProps w/ exported interface in normal script', () => {
  853. const { content, bindings } = compile(`
  854. <script lang="ts">
  855. export interface Props { x?: number }
  856. </script>
  857. <script setup lang="ts">
  858. defineProps<Props>()
  859. </script>
  860. `)
  861. assertCode(content)
  862. expect(content).toMatch(`x: { type: Number, required: false }`)
  863. expect(bindings).toStrictEqual({
  864. x: BindingTypes.PROPS
  865. })
  866. })
  867. test('defineProps w/ type alias', () => {
  868. const { content, bindings } = compile(`
  869. <script setup lang="ts">
  870. type Props = { x?: number }
  871. defineProps<Props>()
  872. </script>
  873. `)
  874. assertCode(content)
  875. expect(content).toMatch(`x: { type: Number, required: false }`)
  876. expect(bindings).toStrictEqual({
  877. x: BindingTypes.PROPS
  878. })
  879. })
  880. test('defineProps w/ exported type alias', () => {
  881. const { content, bindings } = compile(`
  882. <script setup lang="ts">
  883. export type Props = { x?: number }
  884. defineProps<Props>()
  885. </script>
  886. `)
  887. assertCode(content)
  888. expect(content).toMatch(`x: { type: Number, required: false }`)
  889. expect(bindings).toStrictEqual({
  890. x: BindingTypes.PROPS
  891. })
  892. })
  893. test('withDefaults (static)', () => {
  894. const { content, bindings } = compile(`
  895. <script setup lang="ts">
  896. const props = withDefaults(defineProps<{
  897. foo?: string
  898. bar?: number;
  899. baz: boolean;
  900. qux?(): number
  901. }>(), {
  902. foo: 'hi',
  903. qux() { return 1 }
  904. })
  905. </script>
  906. `)
  907. assertCode(content)
  908. expect(content).toMatch(
  909. `foo: { type: String, required: false, default: 'hi' }`
  910. )
  911. expect(content).toMatch(`bar: { type: Number, required: false }`)
  912. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  913. expect(content).toMatch(
  914. `qux: { type: Function, required: false, default() { return 1 } }`
  915. )
  916. expect(content).toMatch(
  917. `{ foo: string, bar?: number, baz: boolean, qux(): number }`
  918. )
  919. expect(content).toMatch(`const props = __props`)
  920. expect(bindings).toStrictEqual({
  921. foo: BindingTypes.PROPS,
  922. bar: BindingTypes.PROPS,
  923. baz: BindingTypes.PROPS,
  924. qux: BindingTypes.PROPS,
  925. props: BindingTypes.SETUP_CONST
  926. })
  927. })
  928. test('withDefaults (dynamic)', () => {
  929. const { content } = compile(`
  930. <script setup lang="ts">
  931. import { defaults } from './foo'
  932. const props = withDefaults(defineProps<{
  933. foo?: string
  934. bar?: number
  935. baz: boolean
  936. }>(), { ...defaults })
  937. </script>
  938. `)
  939. assertCode(content)
  940. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  941. expect(content).toMatch(
  942. `
  943. _mergeDefaults({
  944. foo: { type: String, required: false },
  945. bar: { type: Number, required: false },
  946. baz: { type: Boolean, required: true }
  947. }, { ...defaults })`.trim()
  948. )
  949. })
  950. test('defineEmits w/ type', () => {
  951. const { content } = compile(`
  952. <script setup lang="ts">
  953. const emit = defineEmits<(e: 'foo' | 'bar') => void>()
  954. </script>
  955. `)
  956. assertCode(content)
  957. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  958. expect(content).toMatch(`emits: ["foo", "bar"]`)
  959. })
  960. test('defineEmits w/ type (union)', () => {
  961. const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
  962. expect(() =>
  963. compile(`
  964. <script setup lang="ts">
  965. const emit = defineEmits<${type}>()
  966. </script>
  967. `)
  968. ).toThrow()
  969. })
  970. test('defineEmits w/ type (type literal w/ call signatures)', () => {
  971. const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}`
  972. const { content } = compile(`
  973. <script setup lang="ts">
  974. const emit = defineEmits<${type}>()
  975. </script>
  976. `)
  977. assertCode(content)
  978. expect(content).toMatch(`emit: (${type}),`)
  979. expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
  980. })
  981. test('defineEmits w/ type (interface)', () => {
  982. const { content } = compile(`
  983. <script setup lang="ts">
  984. interface Emits { (e: 'foo' | 'bar'): void }
  985. const emit = defineEmits<Emits>()
  986. </script>
  987. `)
  988. assertCode(content)
  989. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  990. expect(content).toMatch(`emits: ["foo", "bar"]`)
  991. })
  992. test('defineEmits w/ type (exported interface)', () => {
  993. const { content } = compile(`
  994. <script setup lang="ts">
  995. export interface Emits { (e: 'foo' | 'bar'): void }
  996. const emit = defineEmits<Emits>()
  997. </script>
  998. `)
  999. assertCode(content)
  1000. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1001. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1002. })
  1003. test('defineEmits w/ type (type alias)', () => {
  1004. const { content } = compile(`
  1005. <script setup lang="ts">
  1006. type Emits = { (e: 'foo' | 'bar'): void }
  1007. const emit = defineEmits<Emits>()
  1008. </script>
  1009. `)
  1010. assertCode(content)
  1011. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1012. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1013. })
  1014. test('defineEmits w/ type (exported type alias)', () => {
  1015. const { content } = compile(`
  1016. <script setup lang="ts">
  1017. export type Emits = { (e: 'foo' | 'bar'): void }
  1018. const emit = defineEmits<Emits>()
  1019. </script>
  1020. `)
  1021. assertCode(content)
  1022. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1023. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1024. })
  1025. test('defineEmits w/ type (referenced function type)', () => {
  1026. const { content } = compile(`
  1027. <script setup lang="ts">
  1028. type Emits = (e: 'foo' | 'bar') => void
  1029. const emit = defineEmits<Emits>()
  1030. </script>
  1031. `)
  1032. assertCode(content)
  1033. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1034. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1035. })
  1036. test('defineEmits w/ type (referenced exported function type)', () => {
  1037. const { content } = compile(`
  1038. <script setup lang="ts">
  1039. export type Emits = (e: 'foo' | 'bar') => void
  1040. const emit = defineEmits<Emits>()
  1041. </script>
  1042. `)
  1043. assertCode(content)
  1044. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1045. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1046. })
  1047. // https://github.com/vuejs/core/issues/5393
  1048. test('defineEmits w/ type (interface ts type)', () => {
  1049. const { content } = compile(`
  1050. <script setup lang="ts">
  1051. interface Emits { (e: 'foo'): void }
  1052. const emit: Emits = defineEmits(['foo'])
  1053. </script>
  1054. `)
  1055. assertCode(content)
  1056. expect(content).toMatch(`setup(__props, { emit }) {`)
  1057. expect(content).toMatch(`emits: ['foo']`)
  1058. })
  1059. test('runtime Enum', () => {
  1060. const { content, bindings } = compile(
  1061. `<script setup lang="ts">
  1062. enum Foo { A = 123 }
  1063. </script>`
  1064. )
  1065. assertCode(content)
  1066. expect(bindings).toStrictEqual({
  1067. Foo: BindingTypes.SETUP_CONST
  1068. })
  1069. })
  1070. test('runtime Enum in normal script', () => {
  1071. const { content, bindings } = compile(
  1072. `<script lang="ts">
  1073. export enum D { D = "D" }
  1074. const enum C { C = "C" }
  1075. enum B { B = "B" }
  1076. </script>
  1077. <script setup lang="ts">
  1078. enum Foo { A = 123 }
  1079. </script>`
  1080. )
  1081. assertCode(content)
  1082. expect(bindings).toStrictEqual({
  1083. D: BindingTypes.SETUP_CONST,
  1084. C: BindingTypes.SETUP_CONST,
  1085. B: BindingTypes.SETUP_CONST,
  1086. Foo: BindingTypes.SETUP_CONST
  1087. })
  1088. })
  1089. test('const Enum', () => {
  1090. const { content, bindings } = compile(
  1091. `<script setup lang="ts">
  1092. const enum Foo { A = 123 }
  1093. </script>`
  1094. )
  1095. assertCode(content)
  1096. expect(bindings).toStrictEqual({
  1097. Foo: BindingTypes.SETUP_CONST
  1098. })
  1099. })
  1100. test('import type', () => {
  1101. const { content } = compile(
  1102. `<script setup lang="ts">
  1103. import type { Foo } from './main.ts'
  1104. import { type Bar, Baz } from './main.ts'
  1105. </script>`
  1106. )
  1107. expect(content).toMatch(`return { Baz }`)
  1108. assertCode(content)
  1109. })
  1110. })
  1111. describe('errors', () => {
  1112. test('<script> and <script setup> must have same lang', () => {
  1113. expect(() =>
  1114. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  1115. ).toThrow(`<script> and <script setup> must have the same language type`)
  1116. })
  1117. const moduleErrorMsg = `cannot contain ES module exports`
  1118. test('non-type named exports', () => {
  1119. expect(() =>
  1120. compile(`<script setup>
  1121. export const a = 1
  1122. </script>`)
  1123. ).toThrow(moduleErrorMsg)
  1124. expect(() =>
  1125. compile(`<script setup>
  1126. export * from './foo'
  1127. </script>`)
  1128. ).toThrow(moduleErrorMsg)
  1129. expect(() =>
  1130. compile(`<script setup>
  1131. const bar = 1
  1132. export { bar as default }
  1133. </script>`)
  1134. ).toThrow(moduleErrorMsg)
  1135. })
  1136. test('defineProps/Emit() w/ both type and non-type args', () => {
  1137. expect(() => {
  1138. compile(`<script setup lang="ts">
  1139. defineProps<{}>({})
  1140. </script>`)
  1141. }).toThrow(`cannot accept both type and non-type arguments`)
  1142. expect(() => {
  1143. compile(`<script setup lang="ts">
  1144. defineEmits<{}>({})
  1145. </script>`)
  1146. }).toThrow(`cannot accept both type and non-type arguments`)
  1147. })
  1148. test('defineProps/Emit() referencing local var', () => {
  1149. expect(() =>
  1150. compile(`<script setup>
  1151. const bar = 1
  1152. defineProps({
  1153. foo: {
  1154. default: () => bar
  1155. }
  1156. })
  1157. </script>`)
  1158. ).toThrow(`cannot reference locally declared variables`)
  1159. expect(() =>
  1160. compile(`<script setup>
  1161. const bar = 'hello'
  1162. defineEmits([bar])
  1163. </script>`)
  1164. ).toThrow(`cannot reference locally declared variables`)
  1165. // #4644
  1166. expect(() =>
  1167. compile(`
  1168. <script>const bar = 1</script>
  1169. <script setup>
  1170. defineProps({
  1171. foo: {
  1172. default: () => bar
  1173. }
  1174. })
  1175. </script>`)
  1176. ).not.toThrow(`cannot reference locally declared variables`)
  1177. })
  1178. test('should allow defineProps/Emit() referencing scope var', () => {
  1179. assertCode(
  1180. compile(`<script setup>
  1181. const bar = 1
  1182. defineProps({
  1183. foo: {
  1184. default: bar => bar + 1
  1185. }
  1186. })
  1187. defineEmits({
  1188. foo: bar => bar > 1
  1189. })
  1190. </script>`).content
  1191. )
  1192. })
  1193. test('should allow defineProps/Emit() referencing imported binding', () => {
  1194. assertCode(
  1195. compile(`<script setup>
  1196. import { bar } from './bar'
  1197. defineProps({
  1198. foo: {
  1199. default: () => bar
  1200. }
  1201. })
  1202. defineEmits({
  1203. foo: () => bar > 1
  1204. })
  1205. </script>`).content
  1206. )
  1207. })
  1208. })
  1209. })
  1210. describe('SFC analyze <script> bindings', () => {
  1211. it('can parse decorators syntax in typescript block', () => {
  1212. const { scriptAst } = compile(`
  1213. <script lang="ts">
  1214. import { Options, Vue } from 'vue-class-component';
  1215. @Options({
  1216. components: {
  1217. HelloWorld,
  1218. },
  1219. props: ['foo', 'bar']
  1220. })
  1221. export default class Home extends Vue {}
  1222. </script>
  1223. `)
  1224. expect(scriptAst).toBeDefined()
  1225. })
  1226. it('recognizes props array declaration', () => {
  1227. const { bindings } = compile(`
  1228. <script>
  1229. export default {
  1230. props: ['foo', 'bar']
  1231. }
  1232. </script>
  1233. `)
  1234. expect(bindings).toStrictEqual({
  1235. foo: BindingTypes.PROPS,
  1236. bar: BindingTypes.PROPS
  1237. })
  1238. expect(bindings!.__isScriptSetup).toBe(false)
  1239. })
  1240. it('recognizes props object declaration', () => {
  1241. const { bindings } = compile(`
  1242. <script>
  1243. export default {
  1244. props: {
  1245. foo: String,
  1246. bar: {
  1247. type: String,
  1248. },
  1249. baz: null,
  1250. qux: [String, Number]
  1251. }
  1252. }
  1253. </script>
  1254. `)
  1255. expect(bindings).toStrictEqual({
  1256. foo: BindingTypes.PROPS,
  1257. bar: BindingTypes.PROPS,
  1258. baz: BindingTypes.PROPS,
  1259. qux: BindingTypes.PROPS
  1260. })
  1261. expect(bindings!.__isScriptSetup).toBe(false)
  1262. })
  1263. it('recognizes setup return', () => {
  1264. const { bindings } = compile(`
  1265. <script>
  1266. const bar = 2
  1267. export default {
  1268. setup() {
  1269. return {
  1270. foo: 1,
  1271. bar
  1272. }
  1273. }
  1274. }
  1275. </script>
  1276. `)
  1277. expect(bindings).toStrictEqual({
  1278. foo: BindingTypes.SETUP_MAYBE_REF,
  1279. bar: BindingTypes.SETUP_MAYBE_REF
  1280. })
  1281. expect(bindings!.__isScriptSetup).toBe(false)
  1282. })
  1283. it('recognizes exported vars', () => {
  1284. const { bindings } = compile(`
  1285. <script>
  1286. export const foo = 2
  1287. </script>
  1288. <script setup>
  1289. console.log(foo)
  1290. </script>
  1291. `)
  1292. expect(bindings).toStrictEqual({
  1293. foo: BindingTypes.SETUP_CONST
  1294. })
  1295. })
  1296. it('recognizes async setup return', () => {
  1297. const { bindings } = compile(`
  1298. <script>
  1299. const bar = 2
  1300. export default {
  1301. async setup() {
  1302. return {
  1303. foo: 1,
  1304. bar
  1305. }
  1306. }
  1307. }
  1308. </script>
  1309. `)
  1310. expect(bindings).toStrictEqual({
  1311. foo: BindingTypes.SETUP_MAYBE_REF,
  1312. bar: BindingTypes.SETUP_MAYBE_REF
  1313. })
  1314. expect(bindings!.__isScriptSetup).toBe(false)
  1315. })
  1316. it('recognizes data return', () => {
  1317. const { bindings } = compile(`
  1318. <script>
  1319. const bar = 2
  1320. export default {
  1321. data() {
  1322. return {
  1323. foo: null,
  1324. bar
  1325. }
  1326. }
  1327. }
  1328. </script>
  1329. `)
  1330. expect(bindings).toStrictEqual({
  1331. foo: BindingTypes.DATA,
  1332. bar: BindingTypes.DATA
  1333. })
  1334. })
  1335. it('recognizes methods', () => {
  1336. const { bindings } = compile(`
  1337. <script>
  1338. export default {
  1339. methods: {
  1340. foo() {}
  1341. }
  1342. }
  1343. </script>
  1344. `)
  1345. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1346. })
  1347. it('recognizes computeds', () => {
  1348. const { bindings } = compile(`
  1349. <script>
  1350. export default {
  1351. computed: {
  1352. foo() {},
  1353. bar: {
  1354. get() {},
  1355. set() {},
  1356. }
  1357. }
  1358. }
  1359. </script>
  1360. `)
  1361. expect(bindings).toStrictEqual({
  1362. foo: BindingTypes.OPTIONS,
  1363. bar: BindingTypes.OPTIONS
  1364. })
  1365. })
  1366. it('recognizes injections array declaration', () => {
  1367. const { bindings } = compile(`
  1368. <script>
  1369. export default {
  1370. inject: ['foo', 'bar']
  1371. }
  1372. </script>
  1373. `)
  1374. expect(bindings).toStrictEqual({
  1375. foo: BindingTypes.OPTIONS,
  1376. bar: BindingTypes.OPTIONS
  1377. })
  1378. })
  1379. it('recognizes injections object declaration', () => {
  1380. const { bindings } = compile(`
  1381. <script>
  1382. export default {
  1383. inject: {
  1384. foo: {},
  1385. bar: {},
  1386. }
  1387. }
  1388. </script>
  1389. `)
  1390. expect(bindings).toStrictEqual({
  1391. foo: BindingTypes.OPTIONS,
  1392. bar: BindingTypes.OPTIONS
  1393. })
  1394. })
  1395. it('works for mixed bindings', () => {
  1396. const { bindings } = compile(`
  1397. <script>
  1398. export default {
  1399. inject: ['foo'],
  1400. props: {
  1401. bar: String,
  1402. },
  1403. setup() {
  1404. return {
  1405. baz: null,
  1406. }
  1407. },
  1408. data() {
  1409. return {
  1410. qux: null
  1411. }
  1412. },
  1413. methods: {
  1414. quux() {}
  1415. },
  1416. computed: {
  1417. quuz() {}
  1418. }
  1419. }
  1420. </script>
  1421. `)
  1422. expect(bindings).toStrictEqual({
  1423. foo: BindingTypes.OPTIONS,
  1424. bar: BindingTypes.PROPS,
  1425. baz: BindingTypes.SETUP_MAYBE_REF,
  1426. qux: BindingTypes.DATA,
  1427. quux: BindingTypes.OPTIONS,
  1428. quuz: BindingTypes.OPTIONS
  1429. })
  1430. })
  1431. it('works for script setup', () => {
  1432. const { bindings } = compile(`
  1433. <script setup>
  1434. import { ref as r } from 'vue'
  1435. defineProps({
  1436. foo: String
  1437. })
  1438. const a = r(1)
  1439. let b = 2
  1440. const c = 3
  1441. const { d } = someFoo()
  1442. let { e } = someBar()
  1443. </script>
  1444. `)
  1445. expect(bindings).toStrictEqual({
  1446. r: BindingTypes.SETUP_CONST,
  1447. a: BindingTypes.SETUP_REF,
  1448. b: BindingTypes.SETUP_LET,
  1449. c: BindingTypes.SETUP_CONST,
  1450. d: BindingTypes.SETUP_MAYBE_REF,
  1451. e: BindingTypes.SETUP_LET,
  1452. foo: BindingTypes.PROPS
  1453. })
  1454. })
  1455. describe('auto name inference', () => {
  1456. test('basic', () => {
  1457. const { content } = compile(
  1458. `<script setup>const a = 1</script>
  1459. <template>{{ a }}</template>`,
  1460. undefined,
  1461. {
  1462. filename: 'FooBar.vue'
  1463. }
  1464. )
  1465. expect(content).toMatch(`export default {
  1466. __name: 'FooBar'`)
  1467. assertCode(content)
  1468. })
  1469. test('do not overwrite manual name (object)', () => {
  1470. const { content } = compile(
  1471. `<script>
  1472. export default {
  1473. name: 'Baz'
  1474. }
  1475. </script>
  1476. <script setup>const a = 1</script>
  1477. <template>{{ a }}</template>`,
  1478. undefined,
  1479. {
  1480. filename: 'FooBar.vue'
  1481. }
  1482. )
  1483. expect(content).not.toMatch(`name: 'FooBar'`)
  1484. expect(content).toMatch(`name: 'Baz'`)
  1485. assertCode(content)
  1486. })
  1487. test('do not overwrite manual name (call)', () => {
  1488. const { content } = compile(
  1489. `<script>
  1490. import { defineComponent } from 'vue'
  1491. export default defineComponent({
  1492. name: 'Baz'
  1493. })
  1494. </script>
  1495. <script setup>const a = 1</script>
  1496. <template>{{ a }}</template>`,
  1497. undefined,
  1498. {
  1499. filename: 'FooBar.vue'
  1500. }
  1501. )
  1502. expect(content).not.toMatch(`name: 'FooBar'`)
  1503. expect(content).toMatch(`name: 'Baz'`)
  1504. assertCode(content)
  1505. })
  1506. // #12591
  1507. test('should not error when performing ts expression check for v-on inline statement', () => {
  1508. compile(`
  1509. <script setup lang="ts">
  1510. import { foo } from './foo'
  1511. </script>
  1512. <template>
  1513. <div @click="$emit('update:a');"></div>
  1514. </template>
  1515. `)
  1516. })
  1517. // #12841
  1518. test('should not error when performing ts expression check for v-slot destructured default value', () => {
  1519. compile(`
  1520. <script setup lang="ts">
  1521. import FooComp from './Foo.vue'
  1522. </script>
  1523. <template>
  1524. <FooComp>
  1525. <template #bar="{ bar = { baz: '' } }">
  1526. {{ bar.baz }}
  1527. </template>
  1528. </FooComp>
  1529. </template>
  1530. `)
  1531. })
  1532. })
  1533. })