Skip to content

Commit 4421c00

Browse files
authored
feat(compiler-sfc): analyze script bindings (#1962)
Also expose `scriptAst` and `scriptSetupAst` on returned script block
1 parent 94d94ba commit 4421c00

File tree

4 files changed

+337
-22
lines changed

4 files changed

+337
-22
lines changed

packages/compiler-sfc/__tests__/compileScript.spec.ts

+195
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe('SFC compile <script setup>', () => {
190190
)
191191
assertCode(content)
192192
expect(bindings).toStrictEqual({
193+
foo: 'props',
193194
y: 'setup'
194195
})
195196
})
@@ -517,3 +518,197 @@ describe('SFC compile <script setup>', () => {
517518
})
518519
})
519520
})
521+
522+
describe('SFC analyze <script> bindings', () => {
523+
it('recognizes props array declaration', () => {
524+
const { bindings } = compile(`
525+
<script>
526+
export default {
527+
props: ['foo', 'bar']
528+
}
529+
</script>
530+
`)
531+
expect(bindings).toStrictEqual({ foo: 'props', bar: 'props' })
532+
})
533+
534+
it('recognizes props object declaration', () => {
535+
const { bindings } = compile(`
536+
<script>
537+
export default {
538+
props: {
539+
foo: String,
540+
bar: {
541+
type: String,
542+
},
543+
baz: null,
544+
qux: [String, Number]
545+
}
546+
}
547+
</script>
548+
`)
549+
expect(bindings).toStrictEqual({
550+
foo: 'props',
551+
bar: 'props',
552+
baz: 'props',
553+
qux: 'props'
554+
})
555+
})
556+
557+
it('recognizes setup return', () => {
558+
const { bindings } = compile(`
559+
<script>
560+
const bar = 2
561+
export default {
562+
setup() {
563+
return {
564+
foo: 1,
565+
bar
566+
}
567+
}
568+
}
569+
</script>
570+
`)
571+
expect(bindings).toStrictEqual({ foo: 'setup', bar: 'setup' })
572+
})
573+
574+
it('recognizes async setup return', () => {
575+
const { bindings } = compile(`
576+
<script>
577+
const bar = 2
578+
export default {
579+
async setup() {
580+
return {
581+
foo: 1,
582+
bar
583+
}
584+
}
585+
}
586+
</script>
587+
`)
588+
expect(bindings).toStrictEqual({ foo: 'setup', bar: 'setup' })
589+
})
590+
591+
it('recognizes data return', () => {
592+
const { bindings } = compile(`
593+
<script>
594+
const bar = 2
595+
export default {
596+
data() {
597+
return {
598+
foo: null,
599+
bar
600+
}
601+
}
602+
}
603+
</script>
604+
`)
605+
expect(bindings).toStrictEqual({ foo: 'data', bar: 'data' })
606+
})
607+
608+
it('recognizes methods', () => {
609+
const { bindings } = compile(`
610+
<script>
611+
export default {
612+
methods: {
613+
foo() {}
614+
}
615+
}
616+
</script>
617+
`)
618+
expect(bindings).toStrictEqual({ foo: 'options' })
619+
})
620+
621+
it('recognizes computeds', () => {
622+
const { bindings } = compile(`
623+
<script>
624+
export default {
625+
computed: {
626+
foo() {},
627+
bar: {
628+
get() {},
629+
set() {},
630+
}
631+
}
632+
}
633+
</script>
634+
`)
635+
expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' })
636+
})
637+
638+
it('recognizes injections array declaration', () => {
639+
const { bindings } = compile(`
640+
<script>
641+
export default {
642+
inject: ['foo', 'bar']
643+
}
644+
</script>
645+
`)
646+
expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' })
647+
})
648+
649+
it('recognizes injections object declaration', () => {
650+
const { bindings } = compile(`
651+
<script>
652+
export default {
653+
inject: {
654+
foo: {},
655+
bar: {},
656+
}
657+
}
658+
</script>
659+
`)
660+
expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' })
661+
})
662+
663+
it('works for mixed bindings', () => {
664+
const { bindings } = compile(`
665+
<script>
666+
export default {
667+
inject: ['foo'],
668+
props: {
669+
bar: String,
670+
},
671+
setup() {
672+
return {
673+
baz: null,
674+
}
675+
},
676+
data() {
677+
return {
678+
qux: null
679+
}
680+
},
681+
methods: {
682+
quux() {}
683+
},
684+
computed: {
685+
quuz() {}
686+
}
687+
}
688+
</script>
689+
`)
690+
expect(bindings).toStrictEqual({
691+
foo: 'options',
692+
bar: 'props',
693+
baz: 'setup',
694+
qux: 'data',
695+
quux: 'options',
696+
quuz: 'options'
697+
})
698+
})
699+
700+
it('works for script setup', () => {
701+
const { bindings } = compile(`
702+
<script setup>
703+
export default {
704+
props: {
705+
foo: String,
706+
},
707+
}
708+
</script>
709+
`)
710+
expect(bindings).toStrictEqual({
711+
foo: 'props'
712+
})
713+
})
714+
})

0 commit comments

Comments
 (0)