compileScript.spec.ts 42 KB

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