2020-11-21 03:22:51 +08:00
|
|
|
import { BindingTypes } from '@vue/compiler-dom'
|
2020-11-17 07:27:15 +08:00
|
|
|
import { compileSFCScript as compile, assertCode } from './utils'
|
2020-07-09 09:11:57 +08:00
|
|
|
|
|
|
|
describe('SFC compile <script setup>', () => {
|
2020-10-30 03:03:39 +08:00
|
|
|
test('should expose top level declarations', () => {
|
2020-07-10 06:18:46 +08:00
|
|
|
const { content } = compile(`
|
2020-07-09 09:11:57 +08:00
|
|
|
<script setup>
|
|
|
|
import { x } from './x'
|
2020-10-30 03:03:39 +08:00
|
|
|
let a = 1
|
|
|
|
const b = 2
|
|
|
|
function c() {}
|
|
|
|
class d {}
|
2020-07-09 09:11:57 +08:00
|
|
|
</script>
|
2020-07-10 06:18:46 +08:00
|
|
|
`)
|
|
|
|
assertCode(content)
|
2020-11-13 07:11:25 +08:00
|
|
|
expect(content).toMatch('return { a, b, c, d, x }')
|
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps()', () => {
|
2020-11-13 07:11:25 +08:00
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps } from 'vue'
|
|
|
|
const props = defineProps({
|
|
|
|
foo: String
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
const bar = 1
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
// should generate working code
|
|
|
|
assertCode(content)
|
|
|
|
// should anayze bindings
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 04:17:50 +08:00
|
|
|
foo: BindingTypes.PROPS,
|
|
|
|
bar: BindingTypes.SETUP_CONST,
|
2020-11-25 04:12:59 +08:00
|
|
|
props: BindingTypes.SETUP_CONST
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// should remove defineOptions import and call
|
2020-11-25 04:12:59 +08:00
|
|
|
expect(content).not.toMatch('defineProps')
|
2020-11-13 07:11:25 +08:00
|
|
|
// should generate correct setup signature
|
2020-11-25 04:12:59 +08:00
|
|
|
expect(content).toMatch(`setup(__props) {`)
|
|
|
|
// should assign user identifier to it
|
|
|
|
expect(content).toMatch(`const props = __props`)
|
2020-11-13 07:11:25 +08:00
|
|
|
// should include context options in default export
|
|
|
|
expect(content).toMatch(`export default {
|
2020-11-17 00:35:30 +08:00
|
|
|
expose: [],
|
2020-11-13 07:11:25 +08:00
|
|
|
props: {
|
2020-11-25 04:12:59 +08:00
|
|
|
foo: String
|
|
|
|
},`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('defineEmit()', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup>
|
|
|
|
import { defineEmit } from 'vue'
|
|
|
|
const myEmit = defineEmit(['foo', 'bar'])
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
myEmit: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
// should remove defineOptions import and call
|
|
|
|
expect(content).not.toMatch('defineEmit')
|
|
|
|
// should generate correct setup signature
|
|
|
|
expect(content).toMatch(`setup(__props, { emit: myEmit }) {`)
|
|
|
|
// should include context options in default export
|
|
|
|
expect(content).toMatch(`export default {
|
|
|
|
expose: [],
|
|
|
|
emits: ['foo', 'bar'],`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:28:35 +08:00
|
|
|
test('<template inherit-attrs="false">', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {}
|
|
|
|
</script>
|
|
|
|
<template inherit-attrs="false">
|
|
|
|
{{ a }}
|
|
|
|
</template>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
|
|
|
|
const { content: content2 } = compile(`
|
|
|
|
<script setup>
|
|
|
|
const a = 1
|
|
|
|
</script>
|
|
|
|
<template inherit-attrs="false">
|
|
|
|
{{ a }}
|
|
|
|
</template>
|
|
|
|
`)
|
|
|
|
assertCode(content2)
|
|
|
|
})
|
|
|
|
|
2020-11-20 05:17:54 +08:00
|
|
|
describe('<script> and <script setup> co-usage', () => {
|
|
|
|
test('script first', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script>
|
|
|
|
export const n = 1
|
|
|
|
</script>
|
|
|
|
<script setup>
|
|
|
|
import { x } from './x'
|
|
|
|
x()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('script setup first', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>
|
|
|
|
import { x } from './x'
|
|
|
|
x()
|
|
|
|
</script>
|
|
|
|
<script>
|
|
|
|
export const n = 1
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
describe('imports', () => {
|
|
|
|
test('should hoist and expose imports', () => {
|
|
|
|
assertCode(
|
|
|
|
compile(`<script setup>import { ref } from 'vue'</script>`).content
|
2020-07-09 09:11:57 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('should extract comment for import or type declarations', () => {
|
|
|
|
assertCode(
|
2020-11-13 07:11:25 +08:00
|
|
|
compile(`
|
|
|
|
<script setup>
|
|
|
|
import a from 'a' // comment
|
|
|
|
import b from 'b'
|
|
|
|
</script>
|
|
|
|
`).content
|
2020-07-09 09:11:57 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('dedupe between user & helper', () => {
|
2020-11-13 07:11:25 +08:00
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
ref: foo = 1
|
|
|
|
</script>
|
|
|
|
`)
|
2020-07-10 06:18:46 +08:00
|
|
|
assertCode(content)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content).toMatch(`import { ref } from 'vue'`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('import dedupe between <script> and <script setup>', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script>
|
|
|
|
import { x } from './x'
|
|
|
|
</script>
|
|
|
|
<script setup>
|
|
|
|
import { x } from './x'
|
|
|
|
x()
|
|
|
|
</script>
|
|
|
|
`)
|
2020-07-10 06:18:46 +08:00
|
|
|
assertCode(content)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content.indexOf(`import { x }`)).toEqual(
|
|
|
|
content.lastIndexOf(`import { x }`)
|
2020-07-09 09:11:57 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-13 07:11:25 +08:00
|
|
|
describe('inlineTemplate mode', () => {
|
|
|
|
test('should work', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`
|
|
|
|
<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
const count = ref(0)
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div>{{ count }}</div>
|
|
|
|
<div>static</div>
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
// check snapshot and make sure helper imports and
|
|
|
|
// hoists are placed correctly.
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2020-11-24 05:32:24 +08:00
|
|
|
test('referencing scope components and directives', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`
|
|
|
|
<script setup>
|
|
|
|
import ChildComp from './Child.vue'
|
|
|
|
import SomeOtherComp from './Other.vue'
|
|
|
|
import vMyDir from './my-dir'
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div v-my-dir></div>
|
|
|
|
<ChildComp/>
|
|
|
|
<some-other-comp/>
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
expect(content).toMatch('[_unref(vMyDir)]')
|
|
|
|
expect(content).toMatch('_createVNode(ChildComp)')
|
|
|
|
// kebab-case component support
|
|
|
|
expect(content).toMatch('_createVNode(SomeOtherComp)')
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2020-11-13 07:11:25 +08:00
|
|
|
test('avoid unref() when necessary', () => {
|
|
|
|
// function, const, component import
|
|
|
|
const { content } = compile(
|
2020-11-18 04:59:09 +08:00
|
|
|
`<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { ref } from 'vue'
|
2020-11-13 07:11:25 +08:00
|
|
|
import Foo from './Foo.vue'
|
|
|
|
import other from './util'
|
|
|
|
const count = ref(0)
|
|
|
|
const constant = {}
|
2020-11-19 11:39:08 +08:00
|
|
|
const maybe = foo()
|
|
|
|
let lett = 1
|
2020-11-13 07:11:25 +08:00
|
|
|
function fn() {}
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<Foo/>
|
2020-11-19 11:39:08 +08:00
|
|
|
<div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
|
2020-11-13 07:11:25 +08:00
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
// no need to unref vue component import
|
|
|
|
expect(content).toMatch(`createVNode(Foo)`)
|
|
|
|
// should unref other imports
|
|
|
|
expect(content).toMatch(`unref(other)`)
|
|
|
|
// no need to unref constant literals
|
|
|
|
expect(content).not.toMatch(`unref(constant)`)
|
2020-11-19 11:39:08 +08:00
|
|
|
// should directly use .value for known refs
|
|
|
|
expect(content).toMatch(`count.value`)
|
|
|
|
// should unref() on const bindings that may be refs
|
|
|
|
expect(content).toMatch(`unref(maybe)`)
|
|
|
|
// should unref() on let bindings
|
|
|
|
expect(content).toMatch(`unref(lett)`)
|
2020-11-13 07:11:25 +08:00
|
|
|
// no need to unref function declarations
|
|
|
|
expect(content).toMatch(`{ onClick: fn }`)
|
|
|
|
// no need to mark constant fns in patch flag
|
|
|
|
expect(content).not.toMatch(`PROPS`)
|
2020-11-19 11:39:08 +08:00
|
|
|
assertCode(content)
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-11-18 04:59:09 +08:00
|
|
|
|
2020-11-19 08:38:18 +08:00
|
|
|
test('v-model codegen', () => {
|
2020-11-18 04:59:09 +08:00
|
|
|
const { content } = compile(
|
|
|
|
`<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
const count = ref(0)
|
2020-11-19 08:38:18 +08:00
|
|
|
const maybe = foo()
|
|
|
|
let lett = 1
|
2020-11-18 04:59:09 +08:00
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<input v-model="count">
|
2020-11-19 08:38:18 +08:00
|
|
|
<input v-model="maybe">
|
|
|
|
<input v-model="lett">
|
2020-11-18 04:59:09 +08:00
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
2020-11-19 08:38:18 +08:00
|
|
|
// known const ref: set value
|
|
|
|
expect(content).toMatch(`count.value = $event`)
|
2020-11-19 11:39:08 +08:00
|
|
|
// const but maybe ref: also assign .value directly since non-ref
|
|
|
|
// won't work
|
|
|
|
expect(content).toMatch(`maybe.value = $event`)
|
2020-11-19 08:38:18 +08:00
|
|
|
// let: handle both cases
|
|
|
|
expect(content).toMatch(
|
|
|
|
`_isRef(lett) ? lett.value = $event : lett = $event`
|
|
|
|
)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('template assignment expression codegen', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
const count = ref(0)
|
|
|
|
const maybe = foo()
|
|
|
|
let lett = 1
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div @click="count = 1"/>
|
|
|
|
<div @click="maybe = count"/>
|
|
|
|
<div @click="lett = count"/>
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
// known const ref: set value
|
|
|
|
expect(content).toMatch(`count.value = 1`)
|
|
|
|
// const but maybe ref: only assign after check
|
2020-11-19 11:39:08 +08:00
|
|
|
expect(content).toMatch(`maybe.value = count.value`)
|
2020-11-19 08:38:18 +08:00
|
|
|
// let: handle both cases
|
|
|
|
expect(content).toMatch(
|
2020-11-19 11:39:08 +08:00
|
|
|
`_isRef(lett) ? lett.value = count.value : lett = count.value`
|
2020-11-19 08:38:18 +08:00
|
|
|
)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('template update expression codegen', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
const count = ref(0)
|
|
|
|
const maybe = foo()
|
|
|
|
let lett = 1
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div @click="count++"/>
|
|
|
|
<div @click="--count"/>
|
|
|
|
<div @click="maybe++"/>
|
|
|
|
<div @click="--maybe"/>
|
|
|
|
<div @click="lett++"/>
|
|
|
|
<div @click="--lett"/>
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
// known const ref: set value
|
|
|
|
expect(content).toMatch(`count.value++`)
|
|
|
|
expect(content).toMatch(`--count.value`)
|
2020-11-19 11:39:08 +08:00
|
|
|
// const but maybe ref (non-ref case ignored)
|
|
|
|
expect(content).toMatch(`maybe.value++`)
|
|
|
|
expect(content).toMatch(`--maybe.value`)
|
2020-11-19 08:38:18 +08:00
|
|
|
// let: handle both cases
|
|
|
|
expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
|
|
|
|
expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
|
2020-11-18 04:59:09 +08:00
|
|
|
assertCode(content)
|
|
|
|
})
|
2020-11-19 11:39:08 +08:00
|
|
|
|
|
|
|
test('template destructure assignment codegen', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
const val = {}
|
|
|
|
const count = ref(0)
|
|
|
|
const maybe = foo()
|
|
|
|
let lett = 1
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div @click="({ count } = val)"/>
|
|
|
|
<div @click="[maybe] = val"/>
|
|
|
|
<div @click="({ lett } = val)"/>
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
// known const ref: set value
|
|
|
|
expect(content).toMatch(`({ count: count.value } = val)`)
|
|
|
|
// const but maybe ref (non-ref case ignored)
|
|
|
|
expect(content).toMatch(`[maybe.value] = val`)
|
|
|
|
// let: assumes non-ref
|
|
|
|
expect(content).toMatch(`{ lett: lett } = val`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
2020-11-21 03:22:51 +08:00
|
|
|
|
|
|
|
test('ssr codegen', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`
|
|
|
|
<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
const count = ref(0)
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div>{{ count }}</div>
|
|
|
|
<div>static</div>
|
|
|
|
</template>
|
|
|
|
<style>
|
|
|
|
div { color: v-bind(count) }
|
|
|
|
</style>
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
inlineTemplate: true,
|
|
|
|
templateOptions: {
|
|
|
|
ssr: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
|
|
|
|
expect(content).toMatch(`return (_ctx, _push`)
|
|
|
|
expect(content).toMatch(`ssrInterpolate`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('with TypeScript', () => {
|
2020-07-09 09:11:57 +08:00
|
|
|
test('hoist type declarations', () => {
|
2020-10-30 03:03:39 +08:00
|
|
|
const { content } = compile(`
|
2020-07-09 09:11:57 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
export interface Foo {}
|
|
|
|
type Bar = {}
|
|
|
|
</script>`)
|
2020-07-10 06:18:46 +08:00
|
|
|
assertCode(content)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps/Emit w/ runtime options', () => {
|
2020-07-10 06:18:46 +08:00
|
|
|
const { content } = compile(`
|
2020-11-13 07:11:25 +08:00
|
|
|
<script setup lang="ts">
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps, defineEmit } from 'vue'
|
|
|
|
const props = defineProps({ foo: String })
|
|
|
|
const emit = defineEmit(['a', 'b'])
|
2020-11-13 07:11:25 +08:00
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
2020-11-17 00:35:30 +08:00
|
|
|
expect(content).toMatch(`export default _defineComponent({
|
|
|
|
expose: [],
|
2020-11-13 07:11:25 +08:00
|
|
|
props: { foo: String },
|
|
|
|
emits: ['a', 'b'],
|
2020-11-25 04:12:59 +08:00
|
|
|
setup(__props, { emit }) {`)
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps w/ type', () => {
|
2020-11-13 07:11:25 +08:00
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup lang="ts">
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps } from 'vue'
|
2020-07-10 00:16:08 +08:00
|
|
|
interface Test {}
|
2020-07-09 09:11:57 +08:00
|
|
|
|
2020-07-10 00:16:08 +08:00
|
|
|
type Alias = number[]
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
defineProps<{
|
|
|
|
string: string
|
|
|
|
number: number
|
|
|
|
boolean: boolean
|
|
|
|
object: object
|
|
|
|
objectLiteral: { a: number }
|
|
|
|
fn: (n: number) => void
|
|
|
|
functionRef: Function
|
|
|
|
objectRef: Object
|
|
|
|
array: string[]
|
|
|
|
arrayRef: Array<any>
|
|
|
|
tuple: [number, number]
|
|
|
|
set: Set<string>
|
|
|
|
literal: 'foo'
|
|
|
|
optional?: any
|
|
|
|
recordRef: Record<string, null>
|
|
|
|
interface: Test
|
|
|
|
alias: Alias
|
|
|
|
|
|
|
|
union: string | number
|
|
|
|
literalUnion: 'foo' | 'bar'
|
|
|
|
literalUnionMixed: 'foo' | 1 | boolean
|
|
|
|
intersection: Test & {}
|
2020-11-13 07:11:25 +08:00
|
|
|
}>()
|
2020-07-10 00:16:08 +08:00
|
|
|
</script>`)
|
2020-07-10 06:18:46 +08:00
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`string: { type: String, required: true }`)
|
|
|
|
expect(content).toMatch(`number: { type: Number, required: true }`)
|
|
|
|
expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
|
|
|
|
expect(content).toMatch(`object: { type: Object, required: true }`)
|
|
|
|
expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
|
|
|
|
expect(content).toMatch(`fn: { type: Function, required: true }`)
|
|
|
|
expect(content).toMatch(`functionRef: { type: Function, required: true }`)
|
|
|
|
expect(content).toMatch(`objectRef: { type: Object, required: true }`)
|
|
|
|
expect(content).toMatch(`array: { type: Array, required: true }`)
|
|
|
|
expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
|
|
|
|
expect(content).toMatch(`tuple: { type: Array, required: true }`)
|
|
|
|
expect(content).toMatch(`set: { type: Set, required: true }`)
|
|
|
|
expect(content).toMatch(`literal: { type: String, required: true }`)
|
|
|
|
expect(content).toMatch(`optional: { type: null, required: false }`)
|
|
|
|
expect(content).toMatch(`recordRef: { type: Object, required: true }`)
|
|
|
|
expect(content).toMatch(`interface: { type: Object, required: true }`)
|
|
|
|
expect(content).toMatch(`alias: { type: Array, required: true }`)
|
|
|
|
expect(content).toMatch(
|
|
|
|
`union: { type: [String, Number], required: true }`
|
|
|
|
)
|
|
|
|
expect(content).toMatch(
|
2020-07-10 00:16:08 +08:00
|
|
|
`literalUnion: { type: [String, String], required: true }`
|
|
|
|
)
|
2020-07-10 06:18:46 +08:00
|
|
|
expect(content).toMatch(
|
2020-07-10 00:16:08 +08:00
|
|
|
`literalUnionMixed: { type: [String, Number, Boolean], required: true }`
|
|
|
|
)
|
2020-07-10 06:18:46 +08:00
|
|
|
expect(content).toMatch(`intersection: { type: Object, required: true }`)
|
2020-11-13 07:11:25 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 04:17:50 +08:00
|
|
|
string: BindingTypes.PROPS,
|
|
|
|
number: BindingTypes.PROPS,
|
|
|
|
boolean: BindingTypes.PROPS,
|
|
|
|
object: BindingTypes.PROPS,
|
|
|
|
objectLiteral: BindingTypes.PROPS,
|
|
|
|
fn: BindingTypes.PROPS,
|
|
|
|
functionRef: BindingTypes.PROPS,
|
|
|
|
objectRef: BindingTypes.PROPS,
|
|
|
|
array: BindingTypes.PROPS,
|
|
|
|
arrayRef: BindingTypes.PROPS,
|
|
|
|
tuple: BindingTypes.PROPS,
|
|
|
|
set: BindingTypes.PROPS,
|
|
|
|
literal: BindingTypes.PROPS,
|
|
|
|
optional: BindingTypes.PROPS,
|
|
|
|
recordRef: BindingTypes.PROPS,
|
|
|
|
interface: BindingTypes.PROPS,
|
|
|
|
alias: BindingTypes.PROPS,
|
|
|
|
union: BindingTypes.PROPS,
|
|
|
|
literalUnion: BindingTypes.PROPS,
|
|
|
|
literalUnionMixed: BindingTypes.PROPS,
|
|
|
|
intersection: BindingTypes.PROPS
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-07-10 00:16:08 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineEmit w/ type', () => {
|
2020-07-10 06:18:46 +08:00
|
|
|
const { content } = compile(`
|
2020-11-13 07:11:25 +08:00
|
|
|
<script setup lang="ts">
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineEmit } from 'vue'
|
|
|
|
const emit = defineEmit<(e: 'foo' | 'bar') => void>()
|
2020-11-13 07:11:25 +08:00
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
2020-11-25 04:12:59 +08:00
|
|
|
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
|
2020-11-13 07:11:25 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
|
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineEmit w/ type (union)', () => {
|
|
|
|
const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
|
2020-11-13 07:11:25 +08:00
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineEmit } from 'vue'
|
|
|
|
const emit = defineEmit<${type}>()
|
2020-07-10 00:16:08 +08:00
|
|
|
</script>
|
|
|
|
`)
|
2020-07-10 06:18:46 +08:00
|
|
|
assertCode(content)
|
2020-11-25 04:12:59 +08:00
|
|
|
expect(content).toMatch(`emit: (${type}),`)
|
2020-07-10 06:18:46 +08:00
|
|
|
expect(content).toMatch(
|
2020-07-10 00:16:08 +08:00
|
|
|
`emits: ["foo", "bar", "baz"] as unknown as undefined`
|
|
|
|
)
|
|
|
|
})
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-07-11 06:00:13 +08:00
|
|
|
describe('async/await detection', () => {
|
|
|
|
function assertAwaitDetection(code: string, shouldAsync = true) {
|
|
|
|
const { content } = compile(`<script setup>${code}</script>`)
|
2020-11-17 07:27:15 +08:00
|
|
|
expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
|
2020-07-11 06:00:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
test('expression statement', () => {
|
|
|
|
assertAwaitDetection(`await foo`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('variable', () => {
|
|
|
|
assertAwaitDetection(`const a = 1 + (await foo)`)
|
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('ref', () => {
|
|
|
|
assertAwaitDetection(`ref: a = 1 + (await foo)`)
|
2020-07-11 06:00:13 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('nested statements', () => {
|
|
|
|
assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should ignore await inside functions', () => {
|
|
|
|
// function declaration
|
2020-10-30 03:03:39 +08:00
|
|
|
assertAwaitDetection(`async function foo() { await bar }`, false)
|
2020-07-11 06:00:13 +08:00
|
|
|
// function expression
|
|
|
|
assertAwaitDetection(`const foo = async () => { await bar }`, false)
|
|
|
|
// object method
|
|
|
|
assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
|
|
|
|
// class method
|
|
|
|
assertAwaitDetection(
|
|
|
|
`const cls = class Foo { async method() { await bar }}`,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
describe('ref: syntax sugar', () => {
|
|
|
|
test('convert ref declarations', () => {
|
|
|
|
const { content, bindings } = compile(`<script setup>
|
2020-10-31 03:29:38 +08:00
|
|
|
ref: foo
|
2020-10-30 03:03:39 +08:00
|
|
|
ref: a = 1
|
|
|
|
ref: b = {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
let c = () => {}
|
|
|
|
let d
|
|
|
|
</script>`)
|
2020-11-17 00:35:30 +08:00
|
|
|
expect(content).toMatch(`import { ref as _ref } from 'vue'`)
|
2020-10-31 03:29:38 +08:00
|
|
|
expect(content).not.toMatch(`ref: foo`)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content).not.toMatch(`ref: a`)
|
2020-10-31 03:29:38 +08:00
|
|
|
expect(content).not.toMatch(`ref: b`)
|
2020-11-17 00:35:30 +08:00
|
|
|
expect(content).toMatch(`const foo = _ref()`)
|
|
|
|
expect(content).toMatch(`const a = _ref(1)`)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content).toMatch(`
|
2020-11-17 00:35:30 +08:00
|
|
|
const b = _ref({
|
2020-10-30 03:03:39 +08:00
|
|
|
count: 0
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
// normal declarations left untouched
|
|
|
|
expect(content).toMatch(`let c = () => {}`)
|
|
|
|
expect(content).toMatch(`let d`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
foo: BindingTypes.SETUP_REF,
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_REF,
|
2020-11-19 04:17:50 +08:00
|
|
|
c: BindingTypes.SETUP_LET,
|
|
|
|
d: BindingTypes.SETUP_LET
|
2020-10-30 03:03:39 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('multi ref declarations', () => {
|
|
|
|
const { content, bindings } = compile(`<script setup>
|
|
|
|
ref: a = 1, b = 2, c = {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(`
|
2020-11-17 00:35:30 +08:00
|
|
|
const a = _ref(1), b = _ref(2), c = _ref({
|
2020-10-30 03:03:39 +08:00
|
|
|
count: 0
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
expect(content).toMatch(`return { a, b, c }`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_REF,
|
|
|
|
c: BindingTypes.SETUP_REF
|
2020-10-30 03:03:39 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should not convert non ref labels', () => {
|
|
|
|
const { content } = compile(`<script setup>
|
|
|
|
foo: a = 1, b = 2, c = {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(`foo: a = 1, b = 2`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('accessing ref binding', () => {
|
|
|
|
const { content } = compile(`<script setup>
|
|
|
|
ref: a = 1
|
|
|
|
console.log(a)
|
|
|
|
function get() {
|
|
|
|
return a + 1
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(`console.log(a.value)`)
|
|
|
|
expect(content).toMatch(`return a.value + 1`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('cases that should not append .value', () => {
|
|
|
|
const { content } = compile(`<script setup>
|
|
|
|
ref: a = 1
|
|
|
|
console.log(b.a)
|
|
|
|
function get(a) {
|
|
|
|
return a + 1
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).not.toMatch(`a.value`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('mutating ref binding', () => {
|
|
|
|
const { content } = compile(`<script setup>
|
|
|
|
ref: a = 1
|
|
|
|
ref: b = { count: 0 }
|
|
|
|
function inc() {
|
|
|
|
a++
|
|
|
|
a = a + 1
|
|
|
|
b.count++
|
|
|
|
b.count = b.count + 1
|
2020-11-19 11:39:08 +08:00
|
|
|
;({ a } = { a: 2 })
|
|
|
|
;[a] = [1]
|
2020-10-30 03:03:39 +08:00
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(`a.value++`)
|
|
|
|
expect(content).toMatch(`a.value = a.value + 1`)
|
|
|
|
expect(content).toMatch(`b.value.count++`)
|
|
|
|
expect(content).toMatch(`b.value.count = b.value.count + 1`)
|
2020-11-19 11:39:08 +08:00
|
|
|
expect(content).toMatch(`;({ a: a.value } = { a: 2 })`)
|
|
|
|
expect(content).toMatch(`;[a.value] = [1]`)
|
2020-10-30 03:03:39 +08:00
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('using ref binding in property shorthand', () => {
|
|
|
|
const { content } = compile(`<script setup>
|
|
|
|
ref: a = 1
|
|
|
|
const b = { a }
|
|
|
|
function test() {
|
|
|
|
const { a } = b
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(`const b = { a: a.value }`)
|
|
|
|
// should not convert destructure
|
|
|
|
expect(content).toMatch(`const { a } = b`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('object destructure', () => {
|
|
|
|
const { content, bindings } = compile(`<script setup>
|
|
|
|
ref: n = 1, ({ a, b: c, d = 1, e: f = 2, ...g } = useFoo())
|
|
|
|
console.log(n, a, c, d, f, g)
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(
|
2020-11-17 00:35:30 +08:00
|
|
|
`const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()`
|
2020-10-30 03:03:39 +08:00
|
|
|
)
|
2020-11-17 00:35:30 +08:00
|
|
|
expect(content).toMatch(`\nconst a = _ref(__a);`)
|
|
|
|
expect(content).not.toMatch(`\nconst b = _ref(__b);`)
|
|
|
|
expect(content).toMatch(`\nconst c = _ref(__c);`)
|
|
|
|
expect(content).toMatch(`\nconst d = _ref(__d);`)
|
|
|
|
expect(content).not.toMatch(`\nconst e = _ref(__e);`)
|
|
|
|
expect(content).toMatch(`\nconst f = _ref(__f);`)
|
|
|
|
expect(content).toMatch(`\nconst g = _ref(__g);`)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content).toMatch(
|
|
|
|
`console.log(n.value, a.value, c.value, d.value, f.value, g.value)`
|
|
|
|
)
|
|
|
|
expect(content).toMatch(`return { n, a, c, d, f, g }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
n: BindingTypes.SETUP_REF,
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
c: BindingTypes.SETUP_REF,
|
|
|
|
d: BindingTypes.SETUP_REF,
|
|
|
|
f: BindingTypes.SETUP_REF,
|
|
|
|
g: BindingTypes.SETUP_REF
|
2020-10-30 03:03:39 +08:00
|
|
|
})
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('array destructure', () => {
|
|
|
|
const { content, bindings } = compile(`<script setup>
|
|
|
|
ref: n = 1, [a, b = 1, ...c] = useFoo()
|
|
|
|
console.log(n, a, b, c)
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(
|
2020-11-17 00:35:30 +08:00
|
|
|
`const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()`
|
2020-10-30 03:03:39 +08:00
|
|
|
)
|
2020-11-17 00:35:30 +08:00
|
|
|
expect(content).toMatch(`\nconst a = _ref(__a);`)
|
|
|
|
expect(content).toMatch(`\nconst b = _ref(__b);`)
|
|
|
|
expect(content).toMatch(`\nconst c = _ref(__c);`)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
|
|
|
|
expect(content).toMatch(`return { n, a, b, c }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
n: BindingTypes.SETUP_REF,
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_REF,
|
|
|
|
c: BindingTypes.SETUP_REF
|
2020-10-30 03:03:39 +08:00
|
|
|
})
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('nested destructure', () => {
|
|
|
|
const { content, bindings } = compile(`<script setup>
|
|
|
|
ref: [{ a: { b }}] = useFoo()
|
|
|
|
ref: ({ c: [d, e] } = useBar())
|
|
|
|
console.log(b, d, e)
|
|
|
|
</script>`)
|
|
|
|
expect(content).toMatch(`const [{ a: { b: __b }}] = useFoo()`)
|
|
|
|
expect(content).toMatch(`const { c: [__d, __e] } = useBar()`)
|
2020-11-17 00:35:30 +08:00
|
|
|
expect(content).not.toMatch(`\nconst a = _ref(__a);`)
|
|
|
|
expect(content).not.toMatch(`\nconst c = _ref(__c);`)
|
|
|
|
expect(content).toMatch(`\nconst b = _ref(__b);`)
|
|
|
|
expect(content).toMatch(`\nconst d = _ref(__d);`)
|
|
|
|
expect(content).toMatch(`\nconst e = _ref(__e);`)
|
2020-10-30 03:03:39 +08:00
|
|
|
expect(content).toMatch(`return { b, d, e }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
b: BindingTypes.SETUP_REF,
|
|
|
|
d: BindingTypes.SETUP_REF,
|
|
|
|
e: BindingTypes.SETUP_REF
|
2020-10-30 03:03:39 +08:00
|
|
|
})
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-09 09:11:57 +08:00
|
|
|
describe('errors', () => {
|
|
|
|
test('<script> and <script setup> must have same lang', () => {
|
2020-07-16 04:27:21 +08:00
|
|
|
expect(() =>
|
|
|
|
compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
|
|
|
|
).toThrow(`<script> and <script setup> must have the same language type`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-11-13 07:11:25 +08:00
|
|
|
const moduleErrorMsg = `cannot contain ES module exports`
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('non-type named exports', () => {
|
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
|
|
|
export const a = 1
|
|
|
|
</script>`)
|
2020-11-13 07:11:25 +08:00
|
|
|
).toThrow(moduleErrorMsg)
|
2020-11-11 05:28:34 +08:00
|
|
|
|
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
|
|
|
export * from './foo'
|
|
|
|
</script>`)
|
2020-11-13 07:11:25 +08:00
|
|
|
).toThrow(moduleErrorMsg)
|
2020-10-30 03:03:39 +08:00
|
|
|
|
2020-07-16 04:27:21 +08:00
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
2020-07-09 09:11:57 +08:00
|
|
|
const bar = 1
|
|
|
|
export { bar as default }
|
2020-07-16 04:27:21 +08:00
|
|
|
</script>`)
|
2020-11-13 07:11:25 +08:00
|
|
|
).toThrow(moduleErrorMsg)
|
2020-10-30 03:03:39 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('ref: non-assignment expressions', () => {
|
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
|
|
|
ref: a = 1, foo()
|
|
|
|
</script>`)
|
|
|
|
).toThrow(`ref: statements can only contain assignment expressions`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps/Emit() w/ both type and non-type args', () => {
|
|
|
|
expect(() => {
|
|
|
|
compile(`<script setup lang="ts">
|
|
|
|
import { defineProps } from 'vue'
|
|
|
|
defineProps<{}>({})
|
|
|
|
</script>`)
|
|
|
|
}).toThrow(`cannot accept both type and non-type arguments`)
|
|
|
|
|
2020-11-13 07:11:25 +08:00
|
|
|
expect(() => {
|
|
|
|
compile(`<script setup lang="ts">
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineEmit } from 'vue'
|
|
|
|
defineEmit<{}>({})
|
2020-11-13 07:11:25 +08:00
|
|
|
</script>`)
|
|
|
|
}).toThrow(`cannot accept both type and non-type arguments`)
|
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps/Emit() referencing local var', () => {
|
2020-07-16 04:27:21 +08:00
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps } from 'vue'
|
2020-11-13 07:11:25 +08:00
|
|
|
const bar = 1
|
2020-11-25 04:12:59 +08:00
|
|
|
defineProps({
|
|
|
|
foo: {
|
|
|
|
default: () => bar
|
2020-07-09 09:11:57 +08:00
|
|
|
}
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-07-16 04:27:21 +08:00
|
|
|
</script>`)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
2020-11-25 04:12:59 +08:00
|
|
|
|
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
|
|
|
import { defineEmit } from 'vue'
|
|
|
|
const bar = 'hello'
|
|
|
|
defineEmit([bar])
|
|
|
|
</script>`)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps/Emit() referencing ref declarations', () => {
|
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
|
|
|
import { defineProps } from 'vue'
|
|
|
|
ref: bar = 1
|
|
|
|
defineProps({
|
|
|
|
bar
|
|
|
|
})
|
|
|
|
</script>`)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
|
|
|
|
2020-07-16 04:27:21 +08:00
|
|
|
expect(() =>
|
|
|
|
compile(`<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineEmit } from 'vue'
|
2020-10-30 03:03:39 +08:00
|
|
|
ref: bar = 1
|
2020-11-25 04:12:59 +08:00
|
|
|
defineEmit({
|
|
|
|
bar
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-07-16 04:27:21 +08:00
|
|
|
</script>`)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('should allow defineProps/Emit() referencing scope var', () => {
|
2020-07-09 09:11:57 +08:00
|
|
|
assertCode(
|
|
|
|
compile(`<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps, defineEmit } from 'vue'
|
2020-07-09 09:11:57 +08:00
|
|
|
const bar = 1
|
2020-11-25 04:12:59 +08:00
|
|
|
defineProps({
|
|
|
|
foo: {
|
|
|
|
default: bar => bar + 1
|
2020-07-09 09:11:57 +08:00
|
|
|
}
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-11-25 04:12:59 +08:00
|
|
|
defineEmit({
|
|
|
|
foo: bar => bar > 1
|
|
|
|
})
|
2020-07-10 06:18:46 +08:00
|
|
|
</script>`).content
|
2020-07-09 09:11:57 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('should allow defineProps/Emit() referencing imported binding', () => {
|
2020-07-09 09:11:57 +08:00
|
|
|
assertCode(
|
|
|
|
compile(`<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps, defineEmit } from 'vue'
|
|
|
|
import { bar } from './bar'
|
|
|
|
defineProps({
|
|
|
|
foo: {
|
|
|
|
default: () => bar
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defineEmit({
|
|
|
|
foo: () => bar > 1
|
|
|
|
})
|
2020-07-10 06:18:46 +08:00
|
|
|
</script>`).content
|
2020-07-09 09:11:57 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
|
|
|
|
describe('SFC analyze <script> bindings', () => {
|
2020-09-15 09:51:15 +08:00
|
|
|
it('can parse decorators syntax in typescript block', () => {
|
|
|
|
const { scriptAst } = compile(`
|
|
|
|
<script lang="ts">
|
|
|
|
import { Options, Vue } from 'vue-class-component';
|
|
|
|
@Options({
|
|
|
|
components: {
|
|
|
|
HelloWorld,
|
|
|
|
},
|
|
|
|
props: ['foo', 'bar']
|
|
|
|
})
|
|
|
|
export default class Home extends Vue {}
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(scriptAst).toBeDefined()
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
it('recognizes props array declaration', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: ['foo', 'bar']
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.PROPS,
|
|
|
|
bar: BindingTypes.PROPS
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes props object declaration', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
foo: String,
|
|
|
|
bar: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
baz: null,
|
|
|
|
qux: [String, Number]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 04:17:50 +08:00
|
|
|
foo: BindingTypes.PROPS,
|
|
|
|
bar: BindingTypes.PROPS,
|
|
|
|
baz: BindingTypes.PROPS,
|
|
|
|
qux: BindingTypes.PROPS
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes setup return', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
const bar = 2
|
|
|
|
export default {
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
foo: 1,
|
|
|
|
bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
foo: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
bar: BindingTypes.SETUP_MAYBE_REF
|
2020-11-19 04:17:50 +08:00
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes async setup return', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
const bar = 2
|
|
|
|
export default {
|
|
|
|
async setup() {
|
|
|
|
return {
|
|
|
|
foo: 1,
|
|
|
|
bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
foo: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
bar: BindingTypes.SETUP_MAYBE_REF
|
2020-11-19 04:17:50 +08:00
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes data return', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
const bar = 2
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
foo: null,
|
|
|
|
bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.DATA,
|
|
|
|
bar: BindingTypes.DATA
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes methods', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
methods: {
|
|
|
|
foo() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes computeds', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
computed: {
|
|
|
|
foo() {},
|
|
|
|
bar: {
|
|
|
|
get() {},
|
|
|
|
set() {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.OPTIONS,
|
|
|
|
bar: BindingTypes.OPTIONS
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes injections array declaration', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
inject: ['foo', 'bar']
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.OPTIONS,
|
|
|
|
bar: BindingTypes.OPTIONS
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('recognizes injections object declaration', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
inject: {
|
|
|
|
foo: {},
|
|
|
|
bar: {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 04:17:50 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.OPTIONS,
|
|
|
|
bar: BindingTypes.OPTIONS
|
|
|
|
})
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('works for mixed bindings', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
inject: ['foo'],
|
|
|
|
props: {
|
|
|
|
bar: String,
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
baz: null,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
qux: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
quux() {}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
quuz() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 04:17:50 +08:00
|
|
|
foo: BindingTypes.OPTIONS,
|
|
|
|
bar: BindingTypes.PROPS,
|
2020-11-19 08:38:18 +08:00
|
|
|
baz: BindingTypes.SETUP_MAYBE_REF,
|
2020-11-19 04:17:50 +08:00
|
|
|
qux: BindingTypes.DATA,
|
|
|
|
quux: BindingTypes.OPTIONS,
|
|
|
|
quuz: BindingTypes.OPTIONS
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('works for script setup', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script setup>
|
2020-11-25 04:12:59 +08:00
|
|
|
import { defineProps, ref as r } from 'vue'
|
|
|
|
defineProps({
|
|
|
|
foo: String
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-11-19 08:38:18 +08:00
|
|
|
|
|
|
|
const a = r(1)
|
|
|
|
let b = 2
|
|
|
|
const c = 3
|
|
|
|
const { d } = someFoo()
|
|
|
|
let { e } = someBar()
|
2020-08-29 04:21:03 +08:00
|
|
|
</script>
|
|
|
|
`)
|
2020-11-19 08:38:18 +08:00
|
|
|
|
2020-08-29 04:21:03 +08:00
|
|
|
expect(bindings).toStrictEqual({
|
2020-11-19 08:38:18 +08:00
|
|
|
r: BindingTypes.SETUP_CONST,
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_LET,
|
|
|
|
c: BindingTypes.SETUP_CONST,
|
|
|
|
d: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
e: BindingTypes.SETUP_LET,
|
2020-11-19 04:17:50 +08:00
|
|
|
foo: BindingTypes.PROPS
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|