compileScript.spec.ts 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { compileSFCScript as compile, assertCode, mockId } from './utils'
  3. describe('SFC compile <script setup>', () => {
  4. test('should compile JS syntax', () => {
  5. const { content } = compile(`
  6. <script setup lang='js'>
  7. const a = 1
  8. const b = 2
  9. </script>
  10. `)
  11. expect(content).toMatch(`return { a, b }`)
  12. assertCode(content)
  13. })
  14. test('should expose top level declarations', () => {
  15. const { content, bindings } = compile(`
  16. <script setup>
  17. import { x } from './x'
  18. let a = 1
  19. const b = 2
  20. function c() {}
  21. class d {}
  22. </script>
  23. <script>
  24. import { xx } from './x'
  25. let aa = 1
  26. const bb = 2
  27. function cc() {}
  28. class dd {}
  29. </script>
  30. `)
  31. expect(content).toMatch(
  32. `return { get aa() { return aa }, set aa(v) { aa = v }, ` +
  33. `bb, cc, dd, get a() { return a }, set a(v) { a = v }, b, c, d, ` +
  34. `get xx() { return xx }, get x() { return x } }`
  35. )
  36. expect(bindings).toStrictEqual({
  37. x: BindingTypes.SETUP_MAYBE_REF,
  38. a: BindingTypes.SETUP_LET,
  39. b: BindingTypes.LITERAL_CONST,
  40. c: BindingTypes.SETUP_CONST,
  41. d: BindingTypes.SETUP_CONST,
  42. xx: BindingTypes.SETUP_MAYBE_REF,
  43. aa: BindingTypes.SETUP_LET,
  44. bb: BindingTypes.LITERAL_CONST,
  45. cc: BindingTypes.SETUP_CONST,
  46. dd: BindingTypes.SETUP_CONST
  47. })
  48. assertCode(content)
  49. })
  50. test('binding analysis for destructure', () => {
  51. const { content, bindings } = compile(`
  52. <script setup>
  53. const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
  54. </script>
  55. `)
  56. expect(content).toMatch('return { foo, bar, baz, y, z }')
  57. expect(bindings).toStrictEqual({
  58. foo: BindingTypes.SETUP_MAYBE_REF,
  59. bar: BindingTypes.SETUP_MAYBE_REF,
  60. baz: BindingTypes.SETUP_MAYBE_REF,
  61. y: BindingTypes.SETUP_MAYBE_REF,
  62. z: BindingTypes.SETUP_MAYBE_REF
  63. })
  64. assertCode(content)
  65. })
  66. test('defineProps()', () => {
  67. const { content, bindings } = compile(`
  68. <script setup>
  69. const props = defineProps({
  70. foo: String
  71. })
  72. const bar = 1
  73. </script>
  74. `)
  75. // should generate working code
  76. assertCode(content)
  77. // should analyze bindings
  78. expect(bindings).toStrictEqual({
  79. foo: BindingTypes.PROPS,
  80. bar: BindingTypes.LITERAL_CONST,
  81. props: BindingTypes.SETUP_REACTIVE_CONST
  82. })
  83. // should remove defineOptions import and call
  84. expect(content).not.toMatch('defineProps')
  85. // should generate correct setup signature
  86. expect(content).toMatch(`setup(__props, { expose }) {`)
  87. // should assign user identifier to it
  88. expect(content).toMatch(`const props = __props`)
  89. // should include context options in default export
  90. expect(content).toMatch(`export default {
  91. props: {
  92. foo: String
  93. },`)
  94. })
  95. test('defineProps w/ external definition', () => {
  96. const { content } = compile(`
  97. <script setup>
  98. import { propsModel } from './props'
  99. const props = defineProps(propsModel)
  100. </script>
  101. `)
  102. assertCode(content)
  103. expect(content).toMatch(`export default {
  104. props: propsModel,`)
  105. })
  106. // #4764
  107. test('defineProps w/ leading code', () => {
  108. const { content } = compile(`
  109. <script setup>import { x } from './x'
  110. const props = defineProps({})
  111. </script>
  112. `)
  113. // props declaration should be inside setup, not moved along with the import
  114. expect(content).not.toMatch(`const props = __props\nimport`)
  115. assertCode(content)
  116. })
  117. test('defineEmits()', () => {
  118. const { content, bindings } = compile(`
  119. <script setup>
  120. const myEmit = defineEmits(['foo', 'bar'])
  121. </script>
  122. `)
  123. assertCode(content)
  124. expect(bindings).toStrictEqual({
  125. myEmit: BindingTypes.SETUP_CONST
  126. })
  127. // should remove defineOptions import and call
  128. expect(content).not.toMatch('defineEmits')
  129. // should generate correct setup signature
  130. expect(content).toMatch(`setup(__props, { expose, emit: myEmit }) {`)
  131. // should include context options in default export
  132. expect(content).toMatch(`export default {
  133. emits: ['foo', 'bar'],`)
  134. })
  135. test('defineProps/defineEmits in multi-variable declaration', () => {
  136. const { content } = compile(`
  137. <script setup>
  138. const props = defineProps(['item']),
  139. a = 1,
  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. // #6757
  149. test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
  150. const { content } = compile(`
  151. <script setup>
  152. const a = 1,
  153. props = defineProps(['item']),
  154. emit = defineEmits(['a']);
  155. </script>
  156. `)
  157. assertCode(content)
  158. expect(content).toMatch(`const a = 1;`) // test correct removal
  159. expect(content).toMatch(`props: ['item'],`)
  160. expect(content).toMatch(`emits: ['a'],`)
  161. })
  162. // #7422
  163. test('defineProps/defineEmits in multi-variable declaration fix #7422', () => {
  164. const { content } = compile(`
  165. <script setup>
  166. const props = defineProps(['item']),
  167. emits = defineEmits(['foo']),
  168. a = 0,
  169. b = 0;
  170. </script>
  171. `)
  172. assertCode(content)
  173. expect(content).toMatch(`props: ['item'],`)
  174. expect(content).toMatch(`emits: ['foo'],`)
  175. expect(content).toMatch(`const a = 0,`)
  176. expect(content).toMatch(`b = 0;`)
  177. })
  178. test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
  179. const { content } = compile(`
  180. <script setup>
  181. const props = defineProps(['item']),
  182. emit = defineEmits(['a']);
  183. </script>
  184. `)
  185. assertCode(content)
  186. expect(content).toMatch(`props: ['item'],`)
  187. expect(content).toMatch(`emits: ['a'],`)
  188. })
  189. describe('defineOptions()', () => {
  190. test('basic usage', () => {
  191. const { content } = compile(`
  192. <script setup>
  193. defineOptions({ name: 'FooApp' })
  194. </script>
  195. `)
  196. assertCode(content)
  197. // should remove defineOptions import and call
  198. expect(content).not.toMatch('defineOptions')
  199. // should include context options in default export
  200. expect(content).toMatch(
  201. `export default /*#__PURE__*/Object.assign({ name: 'FooApp' }, `
  202. )
  203. })
  204. it('should emit an error with two defineProps', () => {
  205. expect(() =>
  206. compile(`
  207. <script setup>
  208. defineOptions({ name: 'FooApp' })
  209. defineOptions({ name: 'BarApp' })
  210. </script>
  211. `)
  212. ).toThrowError('[@vue/compiler-sfc] duplicate defineOptions() call')
  213. })
  214. it('should emit an error with props or emits property', () => {
  215. expect(() =>
  216. compile(`
  217. <script setup>
  218. defineOptions({ props: { foo: String } })
  219. </script>
  220. `)
  221. ).toThrowError(
  222. '[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.'
  223. )
  224. expect(() =>
  225. compile(`
  226. <script setup>
  227. defineOptions({ emits: ['update'] })
  228. </script>
  229. `)
  230. ).toThrowError(
  231. '[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead.'
  232. )
  233. })
  234. it('should emit an error with type generic', () => {
  235. expect(() =>
  236. compile(`
  237. <script setup lang="ts">
  238. defineOptions<{ name: 'FooApp' }>()
  239. </script>
  240. `)
  241. ).toThrowError(
  242. '[@vue/compiler-sfc] defineOptions() cannot accept type arguments'
  243. )
  244. })
  245. })
  246. test('defineExpose()', () => {
  247. const { content } = compile(`
  248. <script setup>
  249. defineExpose({ foo: 123 })
  250. </script>
  251. `)
  252. assertCode(content)
  253. // should remove defineOptions import and call
  254. expect(content).not.toMatch('defineExpose')
  255. // should generate correct setup signature
  256. expect(content).toMatch(`setup(__props, { expose }) {`)
  257. // should replace callee
  258. expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
  259. })
  260. test('<script> after <script setup> the script content not end with `\\n`', () => {
  261. const { content } = compile(`
  262. <script setup>
  263. import { x } from './x'
  264. </script>
  265. <script>const n = 1</script>
  266. `)
  267. assertCode(content)
  268. })
  269. describe('<script> and <script setup> co-usage', () => {
  270. test('script first', () => {
  271. const { content } = compile(`
  272. <script>
  273. export const n = 1
  274. export default {}
  275. </script>
  276. <script setup>
  277. import { x } from './x'
  278. x()
  279. </script>
  280. `)
  281. assertCode(content)
  282. })
  283. test('script setup first', () => {
  284. const { content } = compile(`
  285. <script setup>
  286. import { x } from './x'
  287. x()
  288. </script>
  289. <script>
  290. export const n = 1
  291. export default {}
  292. </script>
  293. `)
  294. assertCode(content)
  295. })
  296. test('script setup first, named default export', () => {
  297. const { content } = compile(`
  298. <script setup>
  299. import { x } from './x'
  300. x()
  301. </script>
  302. <script>
  303. export const n = 1
  304. const def = {}
  305. export { def as default }
  306. </script>
  307. `)
  308. assertCode(content)
  309. })
  310. // #4395
  311. test('script setup first, lang="ts", script block content export default', () => {
  312. const { content } = compile(`
  313. <script setup lang="ts">
  314. import { x } from './x'
  315. x()
  316. </script>
  317. <script lang="ts">
  318. export default {
  319. name: "test"
  320. }
  321. </script>
  322. `)
  323. // ensure __default__ is declared before used
  324. expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
  325. assertCode(content)
  326. })
  327. describe('spaces in ExportDefaultDeclaration node', () => {
  328. // #4371
  329. test('with many spaces and newline', () => {
  330. // #4371
  331. const { content } = compile(`
  332. <script>
  333. export const n = 1
  334. export default
  335. {
  336. some:'option'
  337. }
  338. </script>
  339. <script setup>
  340. import { x } from './x'
  341. x()
  342. </script>
  343. `)
  344. assertCode(content)
  345. })
  346. test('with minimal spaces', () => {
  347. const { content } = compile(`
  348. <script>
  349. export const n = 1
  350. export default{
  351. some:'option'
  352. }
  353. </script>
  354. <script setup>
  355. import { x } from './x'
  356. x()
  357. </script>
  358. `)
  359. assertCode(content)
  360. })
  361. })
  362. test('export call expression as default', () => {
  363. const { content } = compile(`
  364. <script>
  365. function fn() {
  366. return "hello, world";
  367. }
  368. export default fn();
  369. </script>
  370. <script setup>
  371. console.log('foo')
  372. </script>
  373. `)
  374. assertCode(content)
  375. })
  376. })
  377. describe('imports', () => {
  378. test('should hoist and expose imports', () => {
  379. assertCode(
  380. compile(`<script setup>
  381. import { ref } from 'vue'
  382. import 'foo/css'
  383. </script>`).content
  384. )
  385. })
  386. test('should extract comment for import or type declarations', () => {
  387. assertCode(
  388. compile(`
  389. <script setup>
  390. import a from 'a' // comment
  391. import b from 'b'
  392. </script>
  393. `).content
  394. )
  395. })
  396. // #2740
  397. test('should allow defineProps/Emit at the start of imports', () => {
  398. assertCode(
  399. compile(`<script setup>
  400. import { ref } from 'vue'
  401. defineProps(['foo'])
  402. defineEmits(['bar'])
  403. const r = ref(0)
  404. </script>`).content
  405. )
  406. })
  407. test('dedupe between user & helper', () => {
  408. const { content } = compile(
  409. `
  410. <script setup>
  411. import { ref } from 'vue'
  412. let foo = $ref(1)
  413. </script>
  414. `,
  415. { reactivityTransform: true }
  416. )
  417. assertCode(content)
  418. expect(content).toMatch(`import { ref } from 'vue'`)
  419. })
  420. test('import dedupe between <script> and <script setup>', () => {
  421. const { content } = compile(`
  422. <script>
  423. import { x } from './x'
  424. </script>
  425. <script setup>
  426. import { x } from './x'
  427. x()
  428. </script>
  429. `)
  430. assertCode(content)
  431. expect(content.indexOf(`import { x }`)).toEqual(
  432. content.lastIndexOf(`import { x }`)
  433. )
  434. })
  435. describe('import ref/reactive function from other place', () => {
  436. test('import directly', () => {
  437. const { bindings } = compile(`
  438. <script setup>
  439. import { ref, reactive } from './foo'
  440. const foo = ref(1)
  441. const bar = reactive(1)
  442. </script>
  443. `)
  444. expect(bindings).toStrictEqual({
  445. ref: BindingTypes.SETUP_MAYBE_REF,
  446. reactive: BindingTypes.SETUP_MAYBE_REF,
  447. foo: BindingTypes.SETUP_MAYBE_REF,
  448. bar: BindingTypes.SETUP_MAYBE_REF
  449. })
  450. })
  451. test('import w/ alias', () => {
  452. const { bindings } = compile(`
  453. <script setup>
  454. import { ref as _ref, reactive as _reactive } from './foo'
  455. const foo = ref(1)
  456. const bar = reactive(1)
  457. </script>
  458. `)
  459. expect(bindings).toStrictEqual({
  460. _reactive: BindingTypes.SETUP_MAYBE_REF,
  461. _ref: BindingTypes.SETUP_MAYBE_REF,
  462. foo: BindingTypes.SETUP_MAYBE_REF,
  463. bar: BindingTypes.SETUP_MAYBE_REF
  464. })
  465. })
  466. test('aliased usage before import site', () => {
  467. const { bindings } = compile(`
  468. <script setup>
  469. const bar = x(1)
  470. import { reactive as x } from 'vue'
  471. </script>
  472. `)
  473. expect(bindings).toStrictEqual({
  474. bar: BindingTypes.SETUP_REACTIVE_CONST,
  475. x: BindingTypes.SETUP_CONST
  476. })
  477. })
  478. })
  479. test('should support module string names syntax', () => {
  480. const { content, bindings } = compile(`
  481. <script>
  482. import { "😏" as foo } from './foo'
  483. </script>
  484. <script setup>
  485. import { "😏" as foo } from './foo'
  486. </script>
  487. `)
  488. assertCode(content)
  489. expect(bindings).toStrictEqual({
  490. foo: BindingTypes.SETUP_MAYBE_REF
  491. })
  492. })
  493. })
  494. // in dev mode, declared bindings are returned as an object from setup()
  495. // when using TS, users may import types which should not be returned as
  496. // values, so we need to check import usage in the template to determine
  497. // what to be returned.
  498. describe('dev mode import usage check', () => {
  499. test('components', () => {
  500. const { content } = compile(`
  501. <script setup lang="ts">
  502. import { FooBar, FooBaz, FooQux, foo } from './x'
  503. const fooBar: FooBar = 1
  504. </script>
  505. <template>
  506. <FooBaz></FooBaz>
  507. <foo-qux/>
  508. <foo/>
  509. FooBar
  510. </template>
  511. `)
  512. // FooBar: should not be matched by plain text or incorrect case
  513. // FooBaz: used as PascalCase component
  514. // FooQux: used as kebab-case component
  515. // foo: lowercase component
  516. expect(content).toMatch(
  517. `return { fooBar, get FooBaz() { return FooBaz }, ` +
  518. `get FooQux() { return FooQux }, get foo() { return foo } }`
  519. )
  520. assertCode(content)
  521. })
  522. test('directive', () => {
  523. const { content } = compile(`
  524. <script setup lang="ts">
  525. import { vMyDir } from './x'
  526. </script>
  527. <template>
  528. <div v-my-dir></div>
  529. </template>
  530. `)
  531. expect(content).toMatch(`return { get vMyDir() { return vMyDir } }`)
  532. assertCode(content)
  533. })
  534. // https://github.com/vuejs/core/issues/4599
  535. test('attribute expressions', () => {
  536. const { content } = compile(`
  537. <script setup lang="ts">
  538. import { bar, baz } from './x'
  539. const cond = true
  540. </script>
  541. <template>
  542. <div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
  543. </template>
  544. `)
  545. expect(content).toMatch(
  546. `return { cond, get bar() { return bar }, get baz() { return baz } }`
  547. )
  548. assertCode(content)
  549. })
  550. test('vue interpolations', () => {
  551. const { content } = compile(`
  552. <script setup lang="ts">
  553. import { x, y, z, x$y } from './x'
  554. </script>
  555. <template>
  556. <div :id="z + 'y'">{{ x }} {{ yy }} {{ x$y }}</div>
  557. </template>
  558. `)
  559. // x: used in interpolation
  560. // y: should not be matched by {{ yy }} or 'y' in binding exps
  561. // x$y: #4274 should escape special chars when creating Regex
  562. expect(content).toMatch(
  563. `return { get x() { return x }, get z() { return z }, get x$y() { return x$y } }`
  564. )
  565. assertCode(content)
  566. })
  567. // #4340 interpolations in template strings
  568. test('js template string interpolations', () => {
  569. const { content } = compile(`
  570. <script setup lang="ts">
  571. import { VAR, VAR2, VAR3 } from './x'
  572. </script>
  573. <template>
  574. {{ \`\${VAR}VAR2\${VAR3}\` }}
  575. </template>
  576. `)
  577. // VAR2 should not be matched
  578. expect(content).toMatch(
  579. `return { get VAR() { return VAR }, get VAR3() { return VAR3 } }`
  580. )
  581. assertCode(content)
  582. })
  583. // edge case: last tag in template
  584. test('last tag', () => {
  585. const { content } = compile(`
  586. <script setup lang="ts">
  587. import { FooBaz, Last } from './x'
  588. </script>
  589. <template>
  590. <FooBaz></FooBaz>
  591. <Last/>
  592. </template>
  593. `)
  594. expect(content).toMatch(
  595. `return { get FooBaz() { return FooBaz }, get Last() { return Last } }`
  596. )
  597. assertCode(content)
  598. })
  599. test('TS annotations', () => {
  600. const { content } = compile(`
  601. <script setup lang="ts">
  602. import { Foo, Bar, Baz, Qux, Fred } from './x'
  603. const a = 1
  604. function b() {}
  605. </script>
  606. <template>
  607. {{ a as Foo }}
  608. {{ b<Bar>() }}
  609. {{ Baz }}
  610. <Comp v-slot="{ data }: Qux">{{ data }}</Comp>
  611. <div v-for="{ z = x as Qux } in list as Fred"/>
  612. </template>
  613. `)
  614. expect(content).toMatch(`return { a, b, get Baz() { return Baz } }`)
  615. assertCode(content)
  616. })
  617. // vuejs/vue#12591
  618. test('v-on inline statement', () => {
  619. // should not error
  620. compile(`
  621. <script setup lang="ts">
  622. import { foo } from './foo'
  623. </script>
  624. <template>
  625. <div @click="$emit('update:a');"></div>
  626. </template>
  627. `)
  628. })
  629. })
  630. describe('inlineTemplate mode', () => {
  631. test('should work', () => {
  632. const { content } = compile(
  633. `
  634. <script setup>
  635. import { ref } from 'vue'
  636. const count = ref(0)
  637. </script>
  638. <template>
  639. <div>{{ count }}</div>
  640. <div>static</div>
  641. </template>
  642. `,
  643. { inlineTemplate: true }
  644. )
  645. // check snapshot and make sure helper imports and
  646. // hoists are placed correctly.
  647. assertCode(content)
  648. // in inline mode, no need to call expose() since nothing is exposed
  649. // anyway!
  650. expect(content).not.toMatch(`expose()`)
  651. })
  652. test('with defineExpose()', () => {
  653. const { content } = compile(
  654. `
  655. <script setup>
  656. const count = ref(0)
  657. defineExpose({ count })
  658. </script>
  659. `,
  660. { inlineTemplate: true }
  661. )
  662. assertCode(content)
  663. expect(content).toMatch(`setup(__props, { expose })`)
  664. expect(content).toMatch(`expose({ count })`)
  665. })
  666. test('referencing scope components and directives', () => {
  667. const { content } = compile(
  668. `
  669. <script setup>
  670. import ChildComp from './Child.vue'
  671. import SomeOtherComp from './Other.vue'
  672. import vMyDir from './my-dir'
  673. </script>
  674. <template>
  675. <div v-my-dir></div>
  676. <ChildComp/>
  677. <some-other-comp/>
  678. </template>
  679. `,
  680. { inlineTemplate: true }
  681. )
  682. expect(content).toMatch('[_unref(vMyDir)]')
  683. expect(content).toMatch('_createVNode(ChildComp)')
  684. // kebab-case component support
  685. expect(content).toMatch('_createVNode(SomeOtherComp)')
  686. assertCode(content)
  687. })
  688. test('avoid unref() when necessary', () => {
  689. // function, const, component import
  690. const { content } = compile(
  691. `<script setup>
  692. import { ref } from 'vue'
  693. import Foo, { bar } from './Foo.vue'
  694. import other from './util'
  695. import * as tree from './tree'
  696. const count = ref(0)
  697. const constant = {}
  698. const maybe = foo()
  699. let lett = 1
  700. function fn() {}
  701. </script>
  702. <template>
  703. <Foo>{{ bar }}</Foo>
  704. <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  705. {{ tree.foo() }}
  706. </template>
  707. `,
  708. { inlineTemplate: true }
  709. )
  710. // no need to unref vue component import
  711. expect(content).toMatch(`createVNode(Foo,`)
  712. // #2699 should unref named imports from .vue
  713. expect(content).toMatch(`unref(bar)`)
  714. // should unref other imports
  715. expect(content).toMatch(`unref(other)`)
  716. // no need to unref constant literals
  717. expect(content).not.toMatch(`unref(constant)`)
  718. // should directly use .value for known refs
  719. expect(content).toMatch(`count.value`)
  720. // should unref() on const bindings that may be refs
  721. expect(content).toMatch(`unref(maybe)`)
  722. // should unref() on let bindings
  723. expect(content).toMatch(`unref(lett)`)
  724. // no need to unref namespace import (this also preserves tree-shaking)
  725. expect(content).toMatch(`tree.foo()`)
  726. // no need to unref function declarations
  727. expect(content).toMatch(`{ onClick: fn }`)
  728. // no need to mark constant fns in patch flag
  729. expect(content).not.toMatch(`PROPS`)
  730. assertCode(content)
  731. })
  732. test('v-model codegen', () => {
  733. const { content } = compile(
  734. `<script setup>
  735. import { ref } from 'vue'
  736. const count = ref(0)
  737. const maybe = foo()
  738. let lett = 1
  739. </script>
  740. <template>
  741. <input v-model="count">
  742. <input v-model="maybe">
  743. <input v-model="lett">
  744. </template>
  745. `,
  746. { inlineTemplate: true }
  747. )
  748. // known const ref: set value
  749. expect(content).toMatch(`(count).value = $event`)
  750. // const but maybe ref: assign if ref, otherwise do nothing
  751. expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
  752. // let: handle both cases
  753. expect(content).toMatch(
  754. `_isRef(lett) ? (lett).value = $event : lett = $event`
  755. )
  756. assertCode(content)
  757. })
  758. test('v-model should not generate ref assignment code for non-setup bindings', () => {
  759. const { content } = compile(
  760. `<script setup>
  761. import { ref } from 'vue'
  762. const count = ref(0)
  763. </script>
  764. <script>
  765. export default {
  766. data() { return { foo: 123 } }
  767. }
  768. </script>
  769. <template>
  770. <input v-model="foo">
  771. </template>
  772. `,
  773. { inlineTemplate: true }
  774. )
  775. expect(content).not.toMatch(`_isRef(foo)`)
  776. })
  777. test('template assignment expression codegen', () => {
  778. const { content } = compile(
  779. `<script setup>
  780. import { ref } from 'vue'
  781. const count = ref(0)
  782. const maybe = foo()
  783. let lett = 1
  784. let v = ref(1)
  785. </script>
  786. <template>
  787. <div @click="count = 1"/>
  788. <div @click="maybe = count"/>
  789. <div @click="lett = count"/>
  790. <div @click="v += 1"/>
  791. <div @click="v -= 1"/>
  792. <div @click="() => {
  793. let a = '' + lett
  794. v = a
  795. }"/>
  796. <div @click="() => {
  797. // nested scopes
  798. (()=>{
  799. let x = a
  800. (()=>{
  801. let z = x
  802. let z2 = z
  803. })
  804. let lz = z
  805. })
  806. v = a
  807. }"/>
  808. </template>
  809. `,
  810. { inlineTemplate: true }
  811. )
  812. // known const ref: set value
  813. expect(content).toMatch(`count.value = 1`)
  814. // const but maybe ref: only assign after check
  815. expect(content).toMatch(`maybe.value = count.value`)
  816. // let: handle both cases
  817. expect(content).toMatch(
  818. `_isRef(lett) ? lett.value = count.value : lett = count.value`
  819. )
  820. expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
  821. expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
  822. expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
  823. expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
  824. assertCode(content)
  825. })
  826. test('template update expression codegen', () => {
  827. const { content } = compile(
  828. `<script setup>
  829. import { ref } from 'vue'
  830. const count = ref(0)
  831. const maybe = foo()
  832. let lett = 1
  833. </script>
  834. <template>
  835. <div @click="count++"/>
  836. <div @click="--count"/>
  837. <div @click="maybe++"/>
  838. <div @click="--maybe"/>
  839. <div @click="lett++"/>
  840. <div @click="--lett"/>
  841. </template>
  842. `,
  843. { inlineTemplate: true }
  844. )
  845. // known const ref: set value
  846. expect(content).toMatch(`count.value++`)
  847. expect(content).toMatch(`--count.value`)
  848. // const but maybe ref (non-ref case ignored)
  849. expect(content).toMatch(`maybe.value++`)
  850. expect(content).toMatch(`--maybe.value`)
  851. // let: handle both cases
  852. expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  853. expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  854. assertCode(content)
  855. })
  856. test('template destructure assignment codegen', () => {
  857. const { content } = compile(
  858. `<script setup>
  859. import { ref } from 'vue'
  860. const val = {}
  861. const count = ref(0)
  862. const maybe = foo()
  863. let lett = 1
  864. </script>
  865. <template>
  866. <div @click="({ count } = val)"/>
  867. <div @click="[maybe] = val"/>
  868. <div @click="({ lett } = val)"/>
  869. </template>
  870. `,
  871. { inlineTemplate: true }
  872. )
  873. // known const ref: set value
  874. expect(content).toMatch(`({ count: count.value } = val)`)
  875. // const but maybe ref (non-ref case ignored)
  876. expect(content).toMatch(`[maybe.value] = val`)
  877. // let: assumes non-ref
  878. expect(content).toMatch(`{ lett: lett } = val`)
  879. assertCode(content)
  880. })
  881. test('ssr codegen', () => {
  882. const { content } = compile(
  883. `
  884. <script setup>
  885. import { ref } from 'vue'
  886. const count = ref(0)
  887. </script>
  888. <template>
  889. <div>{{ count }}</div>
  890. <div>static</div>
  891. </template>
  892. <style>
  893. div { color: v-bind(count) }
  894. </style>
  895. `,
  896. {
  897. inlineTemplate: true,
  898. templateOptions: {
  899. ssr: true
  900. }
  901. }
  902. )
  903. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  904. expect(content).toMatch(`return (_ctx, _push`)
  905. expect(content).toMatch(`ssrInterpolate`)
  906. expect(content).not.toMatch(`useCssVars`)
  907. expect(content).toMatch(`"--${mockId}-count": (count.value)`)
  908. assertCode(content)
  909. })
  910. })
  911. describe('with TypeScript', () => {
  912. test('hoist type declarations', () => {
  913. const { content } = compile(`
  914. <script setup lang="ts">
  915. export interface Foo {}
  916. type Bar = {}
  917. </script>`)
  918. assertCode(content)
  919. })
  920. test('defineProps/Emit w/ runtime options', () => {
  921. const { content } = compile(`
  922. <script setup lang="ts">
  923. const props = defineProps({ foo: String })
  924. const emit = defineEmits(['a', 'b'])
  925. </script>
  926. `)
  927. assertCode(content)
  928. expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
  929. props: { foo: String },
  930. emits: ['a', 'b'],
  931. setup(__props, { expose, emit }) {`)
  932. })
  933. test('defineProps w/ type', () => {
  934. const { content, bindings } = compile(`
  935. <script setup lang="ts">
  936. interface Test {}
  937. type Alias = number[]
  938. defineProps<{
  939. string: string
  940. number: number
  941. boolean: boolean
  942. object: object
  943. objectLiteral: { a: number }
  944. fn: (n: number) => void
  945. functionRef: Function
  946. objectRef: Object
  947. dateTime: Date
  948. array: string[]
  949. arrayRef: Array<any>
  950. tuple: [number, number]
  951. set: Set<string>
  952. literal: 'foo'
  953. optional?: any
  954. recordRef: Record<string, null>
  955. interface: Test
  956. alias: Alias
  957. method(): void
  958. symbol: symbol
  959. extract: Extract<1 | 2 | boolean, 2>
  960. exclude: Exclude<1 | 2 | boolean, 2>
  961. uppercase: Uppercase<'foo'>
  962. params: Parameters<(foo: any) => void>
  963. nonNull: NonNullable<string | null>
  964. objectOrFn: {
  965. (): void
  966. foo: string
  967. }
  968. union: string | number
  969. literalUnion: 'foo' | 'bar'
  970. literalUnionNumber: 1 | 2 | 3 | 4 | 5
  971. literalUnionMixed: 'foo' | 1 | boolean
  972. intersection: Test & {}
  973. intersection2: 'foo' & ('foo' | 'bar')
  974. foo: ((item: any) => boolean) | null
  975. unknown: UnknownType
  976. unknownUnion: UnknownType | string
  977. unknownIntersection: UnknownType & Object
  978. }>()
  979. </script>`)
  980. assertCode(content)
  981. expect(content).toMatch(`string: { type: String, required: true }`)
  982. expect(content).toMatch(`number: { type: Number, required: true }`)
  983. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  984. expect(content).toMatch(`object: { type: Object, required: true }`)
  985. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  986. expect(content).toMatch(`fn: { type: Function, required: true }`)
  987. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  988. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  989. expect(content).toMatch(`dateTime: { type: Date, required: true }`)
  990. expect(content).toMatch(`array: { type: Array, required: true }`)
  991. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  992. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  993. expect(content).toMatch(`set: { type: Set, required: true }`)
  994. expect(content).toMatch(`literal: { type: String, required: true }`)
  995. expect(content).toMatch(`optional: { type: null, required: false }`)
  996. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  997. expect(content).toMatch(`interface: { type: Object, required: true }`)
  998. expect(content).toMatch(`alias: { type: Array, required: true }`)
  999. expect(content).toMatch(`method: { type: Function, required: true }`)
  1000. expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
  1001. expect(content).toMatch(
  1002. `objectOrFn: { type: [Function, Object], required: true },`
  1003. )
  1004. expect(content).toMatch(`extract: { type: Number, required: true }`)
  1005. expect(content).toMatch(
  1006. `exclude: { type: [Number, Boolean], required: true }`
  1007. )
  1008. expect(content).toMatch(`uppercase: { type: String, required: true }`)
  1009. expect(content).toMatch(`params: { type: Array, required: true }`)
  1010. expect(content).toMatch(`nonNull: { type: String, required: true }`)
  1011. expect(content).toMatch(
  1012. `union: { type: [String, Number], required: true }`
  1013. )
  1014. expect(content).toMatch(`literalUnion: { type: String, required: true }`)
  1015. expect(content).toMatch(
  1016. `literalUnionNumber: { type: Number, required: true }`
  1017. )
  1018. expect(content).toMatch(
  1019. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  1020. )
  1021. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  1022. expect(content).toMatch(`intersection2: { type: String, required: true }`)
  1023. expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
  1024. expect(content).toMatch(`unknown: { type: null, required: true }`)
  1025. // uninon containing unknown type: skip check
  1026. expect(content).toMatch(`unknownUnion: { type: null, required: true }`)
  1027. // intersection containing unknown type: narrow to the known types
  1028. expect(content).toMatch(
  1029. `unknownIntersection: { type: Object, required: true }`
  1030. )
  1031. expect(bindings).toStrictEqual({
  1032. string: BindingTypes.PROPS,
  1033. number: BindingTypes.PROPS,
  1034. boolean: BindingTypes.PROPS,
  1035. object: BindingTypes.PROPS,
  1036. objectLiteral: BindingTypes.PROPS,
  1037. fn: BindingTypes.PROPS,
  1038. functionRef: BindingTypes.PROPS,
  1039. objectRef: BindingTypes.PROPS,
  1040. dateTime: BindingTypes.PROPS,
  1041. array: BindingTypes.PROPS,
  1042. arrayRef: BindingTypes.PROPS,
  1043. tuple: BindingTypes.PROPS,
  1044. set: BindingTypes.PROPS,
  1045. literal: BindingTypes.PROPS,
  1046. optional: BindingTypes.PROPS,
  1047. recordRef: BindingTypes.PROPS,
  1048. interface: BindingTypes.PROPS,
  1049. alias: BindingTypes.PROPS,
  1050. method: BindingTypes.PROPS,
  1051. symbol: BindingTypes.PROPS,
  1052. objectOrFn: BindingTypes.PROPS,
  1053. extract: BindingTypes.PROPS,
  1054. exclude: BindingTypes.PROPS,
  1055. union: BindingTypes.PROPS,
  1056. literalUnion: BindingTypes.PROPS,
  1057. literalUnionNumber: BindingTypes.PROPS,
  1058. literalUnionMixed: BindingTypes.PROPS,
  1059. intersection: BindingTypes.PROPS,
  1060. intersection2: BindingTypes.PROPS,
  1061. foo: BindingTypes.PROPS,
  1062. uppercase: BindingTypes.PROPS,
  1063. params: BindingTypes.PROPS,
  1064. nonNull: BindingTypes.PROPS,
  1065. unknown: BindingTypes.PROPS,
  1066. unknownUnion: BindingTypes.PROPS,
  1067. unknownIntersection: BindingTypes.PROPS
  1068. })
  1069. })
  1070. test('defineProps w/ interface', () => {
  1071. const { content, bindings } = compile(`
  1072. <script setup lang="ts">
  1073. interface Props { x?: number }
  1074. defineProps<Props>()
  1075. </script>
  1076. `)
  1077. assertCode(content)
  1078. expect(content).toMatch(`x: { type: Number, required: false }`)
  1079. expect(bindings).toStrictEqual({
  1080. x: BindingTypes.PROPS
  1081. })
  1082. })
  1083. test('defineProps w/ extends interface', () => {
  1084. const { content, bindings } = compile(`
  1085. <script lang="ts">
  1086. interface Foo { x?: number }
  1087. </script>
  1088. <script setup lang="ts">
  1089. interface Bar extends Foo { y?: number }
  1090. interface Props extends Bar {
  1091. z: number
  1092. y: string
  1093. }
  1094. defineProps<Props>()
  1095. </script>
  1096. `)
  1097. assertCode(content)
  1098. expect(content).toMatch(`z: { type: Number, required: true }`)
  1099. expect(content).toMatch(`y: { type: String, required: true }`)
  1100. expect(content).toMatch(`x: { type: Number, required: false }`)
  1101. expect(bindings).toStrictEqual({
  1102. x: BindingTypes.PROPS,
  1103. y: BindingTypes.PROPS,
  1104. z: BindingTypes.PROPS
  1105. })
  1106. })
  1107. test('defineProps w/ exported interface', () => {
  1108. const { content, bindings } = compile(`
  1109. <script setup lang="ts">
  1110. export interface Props { x?: number }
  1111. defineProps<Props>()
  1112. </script>
  1113. `)
  1114. assertCode(content)
  1115. expect(content).toMatch(`x: { type: Number, required: false }`)
  1116. expect(bindings).toStrictEqual({
  1117. x: BindingTypes.PROPS
  1118. })
  1119. })
  1120. test('defineProps w/ exported interface in normal script', () => {
  1121. const { content, bindings } = compile(`
  1122. <script lang="ts">
  1123. export interface Props { x?: number }
  1124. </script>
  1125. <script setup lang="ts">
  1126. defineProps<Props>()
  1127. </script>
  1128. `)
  1129. assertCode(content)
  1130. expect(content).toMatch(`x: { type: Number, required: false }`)
  1131. expect(bindings).toStrictEqual({
  1132. x: BindingTypes.PROPS
  1133. })
  1134. })
  1135. test('defineProps w/ type alias', () => {
  1136. const { content, bindings } = compile(`
  1137. <script setup lang="ts">
  1138. type Props = { x?: number }
  1139. defineProps<Props>()
  1140. </script>
  1141. `)
  1142. assertCode(content)
  1143. expect(content).toMatch(`x: { type: Number, required: false }`)
  1144. expect(bindings).toStrictEqual({
  1145. x: BindingTypes.PROPS
  1146. })
  1147. })
  1148. test('defineProps w/ exported type alias', () => {
  1149. const { content, bindings } = compile(`
  1150. <script setup lang="ts">
  1151. export type Props = { x?: number }
  1152. defineProps<Props>()
  1153. </script>
  1154. `)
  1155. assertCode(content)
  1156. expect(content).toMatch(`x: { type: Number, required: false }`)
  1157. expect(bindings).toStrictEqual({
  1158. x: BindingTypes.PROPS
  1159. })
  1160. })
  1161. test('defineProps w/ TS assertion', () => {
  1162. const { content, bindings } = compile(`
  1163. <script setup lang="ts">
  1164. defineProps(['foo'])! as any
  1165. </script>
  1166. `)
  1167. expect(content).toMatch(`props: ['foo']`)
  1168. assertCode(content)
  1169. expect(bindings).toStrictEqual({
  1170. foo: BindingTypes.PROPS
  1171. })
  1172. })
  1173. test('withDefaults (static)', () => {
  1174. const { content, bindings } = compile(`
  1175. <script setup lang="ts">
  1176. const props = withDefaults(defineProps<{
  1177. foo?: string
  1178. bar?: number;
  1179. baz: boolean;
  1180. qux?(): number;
  1181. quux?(): void
  1182. quuxx?: Promise<string>;
  1183. fred?: string
  1184. }>(), {
  1185. foo: 'hi',
  1186. qux() { return 1 },
  1187. ['quux']() { },
  1188. async quuxx() { return await Promise.resolve('hi') },
  1189. get fred() { return 'fred' }
  1190. })
  1191. </script>
  1192. `)
  1193. assertCode(content)
  1194. expect(content).toMatch(
  1195. `foo: { type: String, required: false, default: 'hi' }`
  1196. )
  1197. expect(content).toMatch(`bar: { type: Number, required: false }`)
  1198. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  1199. expect(content).toMatch(
  1200. `qux: { type: Function, required: false, default() { return 1 } }`
  1201. )
  1202. expect(content).toMatch(
  1203. `quux: { type: Function, required: false, default() { } }`
  1204. )
  1205. expect(content).toMatch(
  1206. `quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`
  1207. )
  1208. expect(content).toMatch(
  1209. `fred: { type: String, required: false, get default() { return 'fred' } }`
  1210. )
  1211. expect(content).toMatch(
  1212. `{ foo: string, bar?: number, baz: boolean, qux(): number, quux(): void, quuxx: Promise<string>, fred: string }`
  1213. )
  1214. expect(content).toMatch(`const props = __props`)
  1215. expect(bindings).toStrictEqual({
  1216. foo: BindingTypes.PROPS,
  1217. bar: BindingTypes.PROPS,
  1218. baz: BindingTypes.PROPS,
  1219. qux: BindingTypes.PROPS,
  1220. quux: BindingTypes.PROPS,
  1221. quuxx: BindingTypes.PROPS,
  1222. fred: BindingTypes.PROPS,
  1223. props: BindingTypes.SETUP_CONST
  1224. })
  1225. })
  1226. test('withDefaults (static) + normal script', () => {
  1227. const { content } = compile(`
  1228. <script lang="ts">
  1229. interface Props {
  1230. a?: string;
  1231. }
  1232. </script>
  1233. <script setup lang="ts">
  1234. const props = withDefaults(defineProps<Props>(), {
  1235. a: "a",
  1236. });
  1237. </script>
  1238. `)
  1239. assertCode(content)
  1240. })
  1241. // #7111
  1242. test('withDefaults (static) w/ production mode', () => {
  1243. const { content } = compile(
  1244. `
  1245. <script setup lang="ts">
  1246. const props = withDefaults(defineProps<{
  1247. foo: () => void
  1248. bar: boolean
  1249. baz: boolean | (() => void)
  1250. qux: string | number
  1251. }>(), {
  1252. baz: true,
  1253. qux: 'hi'
  1254. })
  1255. </script>
  1256. `,
  1257. { isProd: true }
  1258. )
  1259. assertCode(content)
  1260. expect(content).toMatch(`const props = __props`)
  1261. // foo has no default value, the Function can be dropped
  1262. expect(content).toMatch(`foo: null`)
  1263. expect(content).toMatch(`bar: { type: Boolean }`)
  1264. expect(content).toMatch(
  1265. `baz: { type: [Boolean, Function], default: true }`
  1266. )
  1267. expect(content).toMatch(`qux: { default: 'hi' }`)
  1268. })
  1269. test('withDefaults (dynamic)', () => {
  1270. const { content } = compile(`
  1271. <script setup lang="ts">
  1272. import { defaults } from './foo'
  1273. const props = withDefaults(defineProps<{
  1274. foo?: string
  1275. bar?: number
  1276. baz: boolean
  1277. }>(), { ...defaults })
  1278. </script>
  1279. `)
  1280. assertCode(content)
  1281. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  1282. expect(content).toMatch(
  1283. `
  1284. _mergeDefaults({
  1285. foo: { type: String, required: false },
  1286. bar: { type: Number, required: false },
  1287. baz: { type: Boolean, required: true }
  1288. }, { ...defaults })`.trim()
  1289. )
  1290. })
  1291. // #7111
  1292. test('withDefaults (dynamic) w/ production mode', () => {
  1293. const { content } = compile(
  1294. `
  1295. <script setup lang="ts">
  1296. import { defaults } from './foo'
  1297. const props = withDefaults(defineProps<{
  1298. foo: () => void
  1299. bar: boolean
  1300. baz: boolean | (() => void)
  1301. qux: string | number
  1302. }>(), { ...defaults })
  1303. </script>
  1304. `,
  1305. { isProd: true }
  1306. )
  1307. assertCode(content)
  1308. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  1309. expect(content).toMatch(
  1310. `
  1311. _mergeDefaults({
  1312. foo: { type: Function },
  1313. bar: { type: Boolean },
  1314. baz: { type: [Boolean, Function] },
  1315. qux: null
  1316. }, { ...defaults })`.trim()
  1317. )
  1318. })
  1319. test('defineEmits w/ type', () => {
  1320. const { content } = compile(`
  1321. <script setup lang="ts">
  1322. const emit = defineEmits<(e: 'foo' | 'bar') => void>()
  1323. </script>
  1324. `)
  1325. assertCode(content)
  1326. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1327. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1328. })
  1329. test('defineEmits w/ type (union)', () => {
  1330. const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
  1331. expect(() =>
  1332. compile(`
  1333. <script setup lang="ts">
  1334. const emit = defineEmits<${type}>()
  1335. </script>
  1336. `)
  1337. ).toThrow()
  1338. })
  1339. test('defineEmits w/ type (type literal w/ call signatures)', () => {
  1340. const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}`
  1341. const { content } = compile(`
  1342. <script setup lang="ts">
  1343. const emit = defineEmits<${type}>()
  1344. </script>
  1345. `)
  1346. assertCode(content)
  1347. expect(content).toMatch(`emit: (${type}),`)
  1348. expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
  1349. })
  1350. test('defineEmits w/ type (interface)', () => {
  1351. const { content } = compile(`
  1352. <script setup lang="ts">
  1353. interface Emits { (e: 'foo' | 'bar'): void }
  1354. const emit = defineEmits<Emits>()
  1355. </script>
  1356. `)
  1357. assertCode(content)
  1358. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1359. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1360. })
  1361. test('defineEmits w/ type (exported interface)', () => {
  1362. const { content } = compile(`
  1363. <script setup lang="ts">
  1364. export interface Emits { (e: 'foo' | 'bar'): void }
  1365. const emit = defineEmits<Emits>()
  1366. </script>
  1367. `)
  1368. assertCode(content)
  1369. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1370. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1371. })
  1372. test('defineEmits w/ type from normal script', () => {
  1373. const { content } = compile(`
  1374. <script lang="ts">
  1375. export interface Emits { (e: 'foo' | 'bar'): void }
  1376. </script>
  1377. <script setup lang="ts">
  1378. const emit = defineEmits<Emits>()
  1379. </script>
  1380. `)
  1381. assertCode(content)
  1382. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1383. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1384. })
  1385. test('defineEmits w/ type (type alias)', () => {
  1386. const { content } = compile(`
  1387. <script setup lang="ts">
  1388. type Emits = { (e: 'foo' | 'bar'): void }
  1389. const emit = defineEmits<Emits>()
  1390. </script>
  1391. `)
  1392. assertCode(content)
  1393. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1394. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1395. })
  1396. test('defineEmits w/ type (exported type alias)', () => {
  1397. const { content } = compile(`
  1398. <script setup lang="ts">
  1399. export type Emits = { (e: 'foo' | 'bar'): void }
  1400. const emit = defineEmits<Emits>()
  1401. </script>
  1402. `)
  1403. assertCode(content)
  1404. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1405. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1406. })
  1407. test('defineEmits w/ type (referenced function type)', () => {
  1408. const { content } = compile(`
  1409. <script setup lang="ts">
  1410. type Emits = (e: 'foo' | 'bar') => void
  1411. const emit = defineEmits<Emits>()
  1412. </script>
  1413. `)
  1414. assertCode(content)
  1415. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1416. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1417. })
  1418. test('defineEmits w/ type (referenced exported function type)', () => {
  1419. const { content } = compile(`
  1420. <script setup lang="ts">
  1421. export type Emits = (e: 'foo' | 'bar') => void
  1422. const emit = defineEmits<Emits>()
  1423. </script>
  1424. `)
  1425. assertCode(content)
  1426. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1427. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1428. })
  1429. // #5393
  1430. test('defineEmits w/ type (interface ts type)', () => {
  1431. const { content } = compile(`
  1432. <script setup lang="ts">
  1433. interface Emits { (e: 'foo'): void }
  1434. const emit: Emits = defineEmits(['foo'])
  1435. </script>
  1436. `)
  1437. assertCode(content)
  1438. expect(content).toMatch(`setup(__props, { expose, emit }) {`)
  1439. expect(content).toMatch(`emits: ['foo']`)
  1440. })
  1441. test('runtime Enum', () => {
  1442. const { content, bindings } = compile(
  1443. `<script setup lang="ts">
  1444. enum Foo { A = 123 }
  1445. </script>`
  1446. )
  1447. assertCode(content)
  1448. expect(bindings).toStrictEqual({
  1449. Foo: BindingTypes.LITERAL_CONST
  1450. })
  1451. })
  1452. test('runtime Enum in normal script', () => {
  1453. const { content, bindings } = compile(
  1454. `<script lang="ts">
  1455. export enum D { D = "D" }
  1456. const enum C { C = "C" }
  1457. enum B { B = "B" }
  1458. </script>
  1459. <script setup lang="ts">
  1460. enum Foo { A = 123 }
  1461. </script>`
  1462. )
  1463. assertCode(content)
  1464. expect(bindings).toStrictEqual({
  1465. D: BindingTypes.LITERAL_CONST,
  1466. C: BindingTypes.LITERAL_CONST,
  1467. B: BindingTypes.LITERAL_CONST,
  1468. Foo: BindingTypes.LITERAL_CONST
  1469. })
  1470. })
  1471. test('const Enum', () => {
  1472. const { content, bindings } = compile(
  1473. `<script setup lang="ts">
  1474. const enum Foo { A = 123 }
  1475. </script>`,
  1476. { hoistStatic: true }
  1477. )
  1478. assertCode(content)
  1479. expect(bindings).toStrictEqual({
  1480. Foo: BindingTypes.LITERAL_CONST
  1481. })
  1482. })
  1483. test('import type', () => {
  1484. const { content } = compile(
  1485. `<script setup lang="ts">
  1486. import type { Foo } from './main.ts'
  1487. import { type Bar, Baz } from './main.ts'
  1488. </script>`
  1489. )
  1490. expect(content).toMatch(`return { get Baz() { return Baz } }`)
  1491. assertCode(content)
  1492. })
  1493. })
  1494. describe('async/await detection', () => {
  1495. function assertAwaitDetection(code: string, shouldAsync = true) {
  1496. const { content } = compile(`<script setup>${code}</script>`, {
  1497. reactivityTransform: true
  1498. })
  1499. if (shouldAsync) {
  1500. expect(content).toMatch(`let __temp, __restore`)
  1501. }
  1502. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  1503. assertCode(content)
  1504. return content
  1505. }
  1506. test('expression statement', () => {
  1507. assertAwaitDetection(`await foo`)
  1508. })
  1509. test('variable', () => {
  1510. assertAwaitDetection(`const a = 1 + (await foo)`)
  1511. })
  1512. test('ref', () => {
  1513. assertAwaitDetection(`let a = $ref(1 + (await foo))`)
  1514. })
  1515. // #4448
  1516. test('nested await', () => {
  1517. assertAwaitDetection(`await (await foo)`)
  1518. assertAwaitDetection(`await ((await foo))`)
  1519. assertAwaitDetection(`await (await (await foo))`)
  1520. })
  1521. // should prepend semicolon
  1522. test('nested leading await in expression statement', () => {
  1523. const code = assertAwaitDetection(`foo()\nawait 1 + await 2`)
  1524. expect(code).toMatch(`foo()\n;(`)
  1525. })
  1526. // #4596 should NOT prepend semicolon
  1527. test('single line conditions', () => {
  1528. const code = assertAwaitDetection(`if (false) await foo()`)
  1529. expect(code).not.toMatch(`if (false) ;(`)
  1530. })
  1531. test('nested statements', () => {
  1532. assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
  1533. })
  1534. test('multiple `if` nested statements', () => {
  1535. assertAwaitDetection(`if (ok) {
  1536. let a = 'foo'
  1537. await 0 + await 1
  1538. await 2
  1539. } else if (a) {
  1540. await 10
  1541. if (b) {
  1542. await 0 + await 1
  1543. } else {
  1544. let a = 'foo'
  1545. await 2
  1546. }
  1547. if (b) {
  1548. await 3
  1549. await 4
  1550. }
  1551. } else {
  1552. await 5
  1553. }`)
  1554. })
  1555. test('multiple `if while` nested statements', () => {
  1556. assertAwaitDetection(`if (ok) {
  1557. while (d) {
  1558. await 5
  1559. }
  1560. while (d) {
  1561. await 5
  1562. await 6
  1563. if (c) {
  1564. let f = 10
  1565. 10 + await 7
  1566. } else {
  1567. await 8
  1568. await 9
  1569. }
  1570. }
  1571. }`)
  1572. })
  1573. test('multiple `if for` nested statements', () => {
  1574. assertAwaitDetection(`if (ok) {
  1575. for (let a of [1,2,3]) {
  1576. await a
  1577. }
  1578. for (let a of [1,2,3]) {
  1579. await a
  1580. await a
  1581. }
  1582. }`)
  1583. })
  1584. test('should ignore await inside functions', () => {
  1585. // function declaration
  1586. assertAwaitDetection(`async function foo() { await bar }`, false)
  1587. // function expression
  1588. assertAwaitDetection(`const foo = async () => { await bar }`, false)
  1589. // object method
  1590. assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
  1591. // class method
  1592. assertAwaitDetection(
  1593. `const cls = class Foo { async method() { await bar }}`,
  1594. false
  1595. )
  1596. })
  1597. })
  1598. describe('errors', () => {
  1599. test('<script> and <script setup> must have same lang', () => {
  1600. expect(() =>
  1601. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  1602. ).toThrow(`<script> and <script setup> must have the same language type`)
  1603. })
  1604. const moduleErrorMsg = `cannot contain ES module exports`
  1605. test('non-type named exports', () => {
  1606. expect(() =>
  1607. compile(`<script setup>
  1608. export const a = 1
  1609. </script>`)
  1610. ).toThrow(moduleErrorMsg)
  1611. expect(() =>
  1612. compile(`<script setup>
  1613. export * from './foo'
  1614. </script>`)
  1615. ).toThrow(moduleErrorMsg)
  1616. expect(() =>
  1617. compile(`<script setup>
  1618. const bar = 1
  1619. export { bar as default }
  1620. </script>`)
  1621. ).toThrow(moduleErrorMsg)
  1622. })
  1623. test('defineProps/Emit() w/ both type and non-type args', () => {
  1624. expect(() => {
  1625. compile(`<script setup lang="ts">
  1626. defineProps<{}>({})
  1627. </script>`)
  1628. }).toThrow(`cannot accept both type and non-type arguments`)
  1629. expect(() => {
  1630. compile(`<script setup lang="ts">
  1631. defineEmits<{}>({})
  1632. </script>`)
  1633. }).toThrow(`cannot accept both type and non-type arguments`)
  1634. })
  1635. test('defineProps/Emit() referencing local var', () => {
  1636. expect(() =>
  1637. compile(`<script setup>
  1638. let bar = 1
  1639. defineProps({
  1640. foo: {
  1641. default: () => bar
  1642. }
  1643. })
  1644. </script>`)
  1645. ).toThrow(`cannot reference locally declared variables`)
  1646. expect(() =>
  1647. compile(`<script setup>
  1648. let bar = 'hello'
  1649. defineEmits([bar])
  1650. </script>`)
  1651. ).toThrow(`cannot reference locally declared variables`)
  1652. // #4644
  1653. expect(() =>
  1654. compile(`
  1655. <script>const bar = 1</script>
  1656. <script setup>
  1657. defineProps({
  1658. foo: {
  1659. default: () => bar
  1660. }
  1661. })
  1662. </script>`)
  1663. ).not.toThrow(`cannot reference locally declared variables`)
  1664. })
  1665. test('should allow defineProps/Emit() referencing scope var', () => {
  1666. assertCode(
  1667. compile(`<script setup>
  1668. const bar = 1
  1669. defineProps({
  1670. foo: {
  1671. default: bar => bar + 1
  1672. }
  1673. })
  1674. defineEmits({
  1675. foo: bar => bar > 1
  1676. })
  1677. </script>`).content
  1678. )
  1679. })
  1680. test('should allow defineProps/Emit() referencing imported binding', () => {
  1681. assertCode(
  1682. compile(`<script setup>
  1683. import { bar } from './bar'
  1684. defineProps({
  1685. foo: {
  1686. default: () => bar
  1687. }
  1688. })
  1689. defineEmits({
  1690. foo: () => bar > 1
  1691. })
  1692. </script>`).content
  1693. )
  1694. })
  1695. })
  1696. })
  1697. describe('SFC analyze <script> bindings', () => {
  1698. it('can parse decorators syntax in typescript block', () => {
  1699. const { scriptAst } = compile(`
  1700. <script lang="ts">
  1701. import { Options, Vue } from 'vue-class-component';
  1702. @Options({
  1703. components: {
  1704. HelloWorld,
  1705. },
  1706. props: ['foo', 'bar']
  1707. })
  1708. export default class Home extends Vue {}
  1709. </script>
  1710. `)
  1711. expect(scriptAst).toBeDefined()
  1712. })
  1713. it('recognizes props array declaration', () => {
  1714. const { bindings } = compile(`
  1715. <script>
  1716. export default {
  1717. props: ['foo', 'bar']
  1718. }
  1719. </script>
  1720. `)
  1721. expect(bindings).toStrictEqual({
  1722. foo: BindingTypes.PROPS,
  1723. bar: BindingTypes.PROPS
  1724. })
  1725. expect(bindings!.__isScriptSetup).toBe(false)
  1726. })
  1727. it('recognizes props object declaration', () => {
  1728. const { bindings } = compile(`
  1729. <script>
  1730. export default {
  1731. props: {
  1732. foo: String,
  1733. bar: {
  1734. type: String,
  1735. },
  1736. baz: null,
  1737. qux: [String, Number]
  1738. }
  1739. }
  1740. </script>
  1741. `)
  1742. expect(bindings).toStrictEqual({
  1743. foo: BindingTypes.PROPS,
  1744. bar: BindingTypes.PROPS,
  1745. baz: BindingTypes.PROPS,
  1746. qux: BindingTypes.PROPS
  1747. })
  1748. expect(bindings!.__isScriptSetup).toBe(false)
  1749. })
  1750. it('recognizes setup return', () => {
  1751. const { bindings } = compile(`
  1752. <script>
  1753. const bar = 2
  1754. export default {
  1755. setup() {
  1756. return {
  1757. foo: 1,
  1758. bar
  1759. }
  1760. }
  1761. }
  1762. </script>
  1763. `)
  1764. expect(bindings).toStrictEqual({
  1765. foo: BindingTypes.SETUP_MAYBE_REF,
  1766. bar: BindingTypes.SETUP_MAYBE_REF
  1767. })
  1768. expect(bindings!.__isScriptSetup).toBe(false)
  1769. })
  1770. it('recognizes exported vars', () => {
  1771. const { bindings } = compile(`
  1772. <script>
  1773. export const foo = 2
  1774. </script>
  1775. <script setup>
  1776. console.log(foo)
  1777. </script>
  1778. `)
  1779. expect(bindings).toStrictEqual({
  1780. foo: BindingTypes.LITERAL_CONST
  1781. })
  1782. })
  1783. it('recognizes async setup return', () => {
  1784. const { bindings } = compile(`
  1785. <script>
  1786. const bar = 2
  1787. export default {
  1788. async setup() {
  1789. return {
  1790. foo: 1,
  1791. bar
  1792. }
  1793. }
  1794. }
  1795. </script>
  1796. `)
  1797. expect(bindings).toStrictEqual({
  1798. foo: BindingTypes.SETUP_MAYBE_REF,
  1799. bar: BindingTypes.SETUP_MAYBE_REF
  1800. })
  1801. expect(bindings!.__isScriptSetup).toBe(false)
  1802. })
  1803. it('recognizes data return', () => {
  1804. const { bindings } = compile(`
  1805. <script>
  1806. const bar = 2
  1807. export default {
  1808. data() {
  1809. return {
  1810. foo: null,
  1811. bar
  1812. }
  1813. }
  1814. }
  1815. </script>
  1816. `)
  1817. expect(bindings).toStrictEqual({
  1818. foo: BindingTypes.DATA,
  1819. bar: BindingTypes.DATA
  1820. })
  1821. })
  1822. it('recognizes methods', () => {
  1823. const { bindings } = compile(`
  1824. <script>
  1825. export default {
  1826. methods: {
  1827. foo() {}
  1828. }
  1829. }
  1830. </script>
  1831. `)
  1832. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1833. })
  1834. it('recognizes computeds', () => {
  1835. const { bindings } = compile(`
  1836. <script>
  1837. export default {
  1838. computed: {
  1839. foo() {},
  1840. bar: {
  1841. get() {},
  1842. set() {},
  1843. }
  1844. }
  1845. }
  1846. </script>
  1847. `)
  1848. expect(bindings).toStrictEqual({
  1849. foo: BindingTypes.OPTIONS,
  1850. bar: BindingTypes.OPTIONS
  1851. })
  1852. })
  1853. it('recognizes injections array declaration', () => {
  1854. const { bindings } = compile(`
  1855. <script>
  1856. export default {
  1857. inject: ['foo', 'bar']
  1858. }
  1859. </script>
  1860. `)
  1861. expect(bindings).toStrictEqual({
  1862. foo: BindingTypes.OPTIONS,
  1863. bar: BindingTypes.OPTIONS
  1864. })
  1865. })
  1866. it('recognizes injections object declaration', () => {
  1867. const { bindings } = compile(`
  1868. <script>
  1869. export default {
  1870. inject: {
  1871. foo: {},
  1872. bar: {},
  1873. }
  1874. }
  1875. </script>
  1876. `)
  1877. expect(bindings).toStrictEqual({
  1878. foo: BindingTypes.OPTIONS,
  1879. bar: BindingTypes.OPTIONS
  1880. })
  1881. })
  1882. it('works for mixed bindings', () => {
  1883. const { bindings } = compile(`
  1884. <script>
  1885. export default {
  1886. inject: ['foo'],
  1887. props: {
  1888. bar: String,
  1889. },
  1890. setup() {
  1891. return {
  1892. baz: null,
  1893. }
  1894. },
  1895. data() {
  1896. return {
  1897. qux: null
  1898. }
  1899. },
  1900. methods: {
  1901. quux() {}
  1902. },
  1903. computed: {
  1904. quuz() {}
  1905. }
  1906. }
  1907. </script>
  1908. `)
  1909. expect(bindings).toStrictEqual({
  1910. foo: BindingTypes.OPTIONS,
  1911. bar: BindingTypes.PROPS,
  1912. baz: BindingTypes.SETUP_MAYBE_REF,
  1913. qux: BindingTypes.DATA,
  1914. quux: BindingTypes.OPTIONS,
  1915. quuz: BindingTypes.OPTIONS
  1916. })
  1917. })
  1918. it('works for script setup', () => {
  1919. const { bindings } = compile(`
  1920. <script setup>
  1921. import { ref as r } from 'vue'
  1922. defineProps({
  1923. foo: String
  1924. })
  1925. const a = r(1)
  1926. let b = 2
  1927. const c = 3
  1928. const { d } = someFoo()
  1929. let { e } = someBar()
  1930. </script>
  1931. `)
  1932. expect(bindings).toStrictEqual({
  1933. r: BindingTypes.SETUP_CONST,
  1934. a: BindingTypes.SETUP_REF,
  1935. b: BindingTypes.SETUP_LET,
  1936. c: BindingTypes.LITERAL_CONST,
  1937. d: BindingTypes.SETUP_MAYBE_REF,
  1938. e: BindingTypes.SETUP_LET,
  1939. foo: BindingTypes.PROPS
  1940. })
  1941. })
  1942. describe('auto name inference', () => {
  1943. test('basic', () => {
  1944. const { content } = compile(
  1945. `<script setup>const a = 1</script>
  1946. <template>{{ a }}</template>`,
  1947. undefined,
  1948. {
  1949. filename: 'FooBar.vue'
  1950. }
  1951. )
  1952. expect(content).toMatch(`export default {
  1953. __name: 'FooBar'`)
  1954. assertCode(content)
  1955. })
  1956. test('do not overwrite manual name (object)', () => {
  1957. const { content } = compile(
  1958. `<script>
  1959. export default {
  1960. name: 'Baz'
  1961. }
  1962. </script>
  1963. <script setup>const a = 1</script>
  1964. <template>{{ a }}</template>`,
  1965. undefined,
  1966. {
  1967. filename: 'FooBar.vue'
  1968. }
  1969. )
  1970. expect(content).not.toMatch(`name: 'FooBar'`)
  1971. expect(content).toMatch(`name: 'Baz'`)
  1972. assertCode(content)
  1973. })
  1974. test('do not overwrite manual name (call)', () => {
  1975. const { content } = compile(
  1976. `<script>
  1977. import { defineComponent } from 'vue'
  1978. export default defineComponent({
  1979. name: 'Baz'
  1980. })
  1981. </script>
  1982. <script setup>const a = 1</script>
  1983. <template>{{ a }}</template>`,
  1984. undefined,
  1985. {
  1986. filename: 'FooBar.vue'
  1987. }
  1988. )
  1989. expect(content).not.toMatch(`name: 'FooBar'`)
  1990. expect(content).toMatch(`name: 'Baz'`)
  1991. assertCode(content)
  1992. })
  1993. })
  1994. })