1
- import { parse , SFCScriptCompileOptions } from '../src'
1
+ import { parse , SFCScriptCompileOptions , compileScript } from '../src'
2
2
import { parse as babelParse } from '@babel/parser'
3
3
import { babelParserDefautPlugins } from '@vue/shared'
4
4
5
5
function compile ( src : string , options ?: SFCScriptCompileOptions ) {
6
- return parse ( src , options ) . descriptor . scriptTransformed !
6
+ const { descriptor } = parse ( src )
7
+ return compileScript ( descriptor , options )
7
8
}
8
9
9
10
function assertCode ( code : string ) {
@@ -370,24 +371,23 @@ describe('SFC compile <script setup>', () => {
370
371
371
372
describe ( 'errors' , ( ) => {
372
373
test ( '<script> and <script setup> must have same lang' , ( ) => {
373
- expect (
374
- parse ( `<script>foo()</script><script setup lang="ts">bar()</script>` )
375
- . errors [ 0 ] . message
376
- ) . toMatch ( `<script> and <script setup> must have the same language type` )
374
+ expect ( ( ) =>
375
+ compile ( `<script>foo()</script><script setup lang="ts">bar()</script>` )
376
+ ) . toThrow ( `<script> and <script setup> must have the same language type` )
377
377
} )
378
378
379
379
test ( 'export local as default' , ( ) => {
380
- expect (
381
- parse ( `<script setup>
380
+ expect ( ( ) =>
381
+ compile ( `<script setup>
382
382
const bar = 1
383
383
export { bar as default }
384
- </script>` ) . errors [ 0 ] . message
385
- ) . toMatch ( `Cannot export locally defined variable as default` )
384
+ </script>` )
385
+ ) . toThrow ( `Cannot export locally defined variable as default` )
386
386
} )
387
387
388
388
test ( 'export default referencing local var' , ( ) => {
389
- expect (
390
- parse ( `<script setup>
389
+ expect ( ( ) =>
390
+ compile ( `<script setup>
391
391
const bar = 1
392
392
export default {
393
393
props: {
@@ -396,19 +396,19 @@ describe('SFC compile <script setup>', () => {
396
396
}
397
397
}
398
398
}
399
- </script>` ) . errors [ 0 ] . message
400
- ) . toMatch ( `cannot reference locally declared variables` )
399
+ </script>` )
400
+ ) . toThrow ( `cannot reference locally declared variables` )
401
401
} )
402
402
403
403
test ( 'export default referencing exports' , ( ) => {
404
- expect (
405
- parse ( `<script setup>
404
+ expect ( ( ) =>
405
+ compile ( `<script setup>
406
406
export const bar = 1
407
407
export default {
408
408
props: bar
409
409
}
410
- </script>` ) . errors [ 0 ] . message
411
- ) . toMatch ( `cannot reference locally declared variables` )
410
+ </script>` )
411
+ ) . toThrow ( `cannot reference locally declared variables` )
412
412
} )
413
413
414
414
test ( 'should allow export default referencing scope var' , ( ) => {
@@ -458,62 +458,62 @@ describe('SFC compile <script setup>', () => {
458
458
} )
459
459
460
460
test ( 'error on duplicated default export' , ( ) => {
461
- expect (
462
- parse ( `
461
+ expect ( ( ) =>
462
+ compile ( `
463
463
<script>
464
464
export default {}
465
465
</script>
466
466
<script setup>
467
467
export default {}
468
468
</script>
469
- ` ) . errors [ 0 ] . message
470
- ) . toMatch ( `Default export is already declared` )
469
+ ` )
470
+ ) . toThrow ( `Default export is already declared` )
471
471
472
- expect (
473
- parse ( `
472
+ expect ( ( ) =>
473
+ compile ( `
474
474
<script>
475
475
export default {}
476
476
</script>
477
477
<script setup>
478
478
const x = {}
479
479
export { x as default }
480
480
</script>
481
- ` ) . errors [ 0 ] . message
482
- ) . toMatch ( `Default export is already declared` )
481
+ ` )
482
+ ) . toThrow ( `Default export is already declared` )
483
483
484
- expect (
485
- parse ( `
484
+ expect ( ( ) =>
485
+ compile ( `
486
486
<script>
487
487
export default {}
488
488
</script>
489
489
<script setup>
490
490
export { x as default } from './y'
491
491
</script>
492
- ` ) . errors [ 0 ] . message
493
- ) . toMatch ( `Default export is already declared` )
492
+ ` )
493
+ ) . toThrow ( `Default export is already declared` )
494
494
495
- expect (
496
- parse ( `
495
+ expect ( ( ) =>
496
+ compile ( `
497
497
<script>
498
498
export { x as default } from './y'
499
499
</script>
500
500
<script setup>
501
501
export default {}
502
502
</script>
503
- ` ) . errors [ 0 ] . message
504
- ) . toMatch ( `Default export is already declared` )
503
+ ` )
504
+ ) . toThrow ( `Default export is already declared` )
505
505
506
- expect (
507
- parse ( `
506
+ expect ( ( ) =>
507
+ compile ( `
508
508
<script>
509
509
const x = {}
510
510
export { x as default }
511
511
</script>
512
512
<script setup>
513
513
export default {}
514
514
</script>
515
- ` ) . errors [ 0 ] . message
516
- ) . toMatch ( `Default export is already declared` )
515
+ ` )
516
+ ) . toThrow ( `Default export is already declared` )
517
517
} )
518
518
} )
519
519
} )
0 commit comments