2021-06-30 03:22:26 +08:00
|
|
|
import { BindingTypes } from '@vue/compiler-core'
|
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', () => {
|
2021-08-18 03:52:48 +08:00
|
|
|
const { content, bindings } = 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>
|
2021-08-18 03:52:48 +08:00
|
|
|
|
|
|
|
<script>
|
|
|
|
import { xx } from './x'
|
|
|
|
let aa = 1
|
|
|
|
const bb = 2
|
|
|
|
function cc() {}
|
|
|
|
class dd {}
|
|
|
|
</script>
|
2020-07-10 06:18:46 +08:00
|
|
|
`)
|
2021-08-18 03:52:48 +08:00
|
|
|
expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
x: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
a: BindingTypes.SETUP_LET,
|
|
|
|
b: BindingTypes.SETUP_CONST,
|
|
|
|
c: BindingTypes.SETUP_CONST,
|
|
|
|
d: BindingTypes.SETUP_CONST,
|
|
|
|
xx: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
aa: BindingTypes.SETUP_LET,
|
|
|
|
bb: BindingTypes.SETUP_CONST,
|
|
|
|
cc: BindingTypes.SETUP_CONST,
|
|
|
|
dd: BindingTypes.SETUP_CONST
|
|
|
|
})
|
2020-07-10 06:18:46 +08:00
|
|
|
assertCode(content)
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
|
2021-12-11 17:50:09 +08:00
|
|
|
test('binding analysis for destructure', () => {
|
2021-09-10 00:28:59 +08:00
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup>
|
|
|
|
const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
expect(content).toMatch('return { foo, bar, baz, y, z }')
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
bar: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
baz: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
y: BindingTypes.SETUP_MAYBE_REF,
|
|
|
|
z: BindingTypes.SETUP_MAYBE_REF
|
|
|
|
})
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
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
|
|
|
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
|
2021-06-26 01:14:49 +08:00
|
|
|
expect(content).toMatch(`setup(__props, { expose }) {`)
|
2020-11-25 04:12:59 +08:00
|
|
|
// 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 {
|
|
|
|
props: {
|
2020-11-25 04:12:59 +08:00
|
|
|
foo: String
|
|
|
|
},`)
|
|
|
|
})
|
|
|
|
|
2021-06-26 02:05:59 +08:00
|
|
|
test('defineProps w/ external definition', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>
|
|
|
|
import { propsModel } from './props'
|
|
|
|
const props = defineProps(propsModel)
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`export default {
|
|
|
|
props: propsModel,`)
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:52:48 +08:00
|
|
|
// #4764
|
|
|
|
test('defineProps w/ leading code', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>import { x } from './x'
|
|
|
|
const props = defineProps({})
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
// props declaration should be inside setup, not moved along with the import
|
|
|
|
expect(content).not.toMatch(`const props = __props\nimport`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
test('defineEmits()', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup>
|
|
|
|
const myEmit = defineEmits(['foo', 'bar'])
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
myEmit: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
// should remove defineOptions import and call
|
|
|
|
expect(content).not.toMatch('defineEmits')
|
2020-11-25 04:12:59 +08:00
|
|
|
// should generate correct setup signature
|
2021-06-26 01:14:49 +08:00
|
|
|
expect(content).toMatch(`setup(__props, { expose, emit: myEmit }) {`)
|
2020-11-25 04:12:59 +08:00
|
|
|
// should include context options in default export
|
|
|
|
expect(content).toMatch(`export default {
|
|
|
|
emits: ['foo', 'bar'],`)
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2021-09-06 06:02:50 +08:00
|
|
|
test('defineProps/defineEmits in multi-variable declaration', () => {
|
2021-06-29 04:27:30 +08:00
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>
|
|
|
|
const props = defineProps(['item']),
|
|
|
|
a = 1,
|
|
|
|
emit = defineEmits(['a']);
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`const a = 1;`) // test correct removal
|
|
|
|
expect(content).toMatch(`props: ['item'],`)
|
|
|
|
expect(content).toMatch(`emits: ['a'],`)
|
|
|
|
})
|
|
|
|
|
2021-09-06 06:02:50 +08:00
|
|
|
test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
|
2021-06-29 04:27:30 +08:00
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>
|
|
|
|
const props = defineProps(['item']),
|
|
|
|
emit = defineEmits(['a']);
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`props: ['item'],`)
|
|
|
|
expect(content).toMatch(`emits: ['a'],`)
|
|
|
|
})
|
|
|
|
|
2021-06-26 01:14:49 +08:00
|
|
|
test('defineExpose()', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup>
|
|
|
|
defineExpose({ foo: 123 })
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
// should remove defineOptions import and call
|
|
|
|
expect(content).not.toMatch('defineExpose')
|
|
|
|
// should generate correct setup signature
|
|
|
|
expect(content).toMatch(`setup(__props, { expose }) {`)
|
|
|
|
// should replace callee
|
|
|
|
expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
|
|
|
|
})
|
|
|
|
|
2020-11-20 05:17:54 +08:00
|
|
|
describe('<script> and <script setup> co-usage', () => {
|
2021-08-24 06:40:16 +08:00
|
|
|
describe('spaces in ExportDefaultDeclaration node', () => {
|
|
|
|
// #4371
|
|
|
|
test('with many spaces and newline', () => {
|
|
|
|
// #4371
|
|
|
|
const { content } = compile(`
|
|
|
|
<script>
|
|
|
|
export const n = 1
|
2021-10-09 00:52:48 +08:00
|
|
|
export default
|
|
|
|
{
|
2021-08-24 06:40:16 +08:00
|
|
|
some:'option'
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
<script setup>
|
|
|
|
import { x } from './x'
|
|
|
|
x()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with minimal spaces', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script>
|
|
|
|
export const n = 1
|
2021-10-09 00:52:48 +08:00
|
|
|
export default{
|
2021-08-24 06:40:16 +08:00
|
|
|
some:'option'
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
<script setup>
|
|
|
|
import { x } from './x'
|
|
|
|
x()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-20 05:17:54 +08:00
|
|
|
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)
|
|
|
|
})
|
2021-08-24 07:02:54 +08:00
|
|
|
|
|
|
|
// #4395
|
|
|
|
test('script setup first, lang="ts", script block content export default', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { x } from './x'
|
|
|
|
x()
|
|
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
|
|
export default {
|
|
|
|
name: "test"
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
// ensure __default__ is declared before used
|
|
|
|
expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
2020-11-20 05:17:54 +08:00
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
describe('imports', () => {
|
|
|
|
test('should hoist and expose imports', () => {
|
|
|
|
assertCode(
|
2020-11-29 02:51:32 +08:00
|
|
|
compile(`<script setup>
|
|
|
|
import { ref } from 'vue'
|
|
|
|
import 'foo/css'
|
|
|
|
</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
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-02-13 17:06:34 +08:00
|
|
|
// #2740
|
|
|
|
test('should allow defineProps/Emit at the start of imports', () => {
|
|
|
|
assertCode(
|
|
|
|
compile(`<script setup>
|
2021-06-29 04:03:27 +08:00
|
|
|
import { ref } from 'vue'
|
2021-02-13 17:06:34 +08:00
|
|
|
defineProps(['foo'])
|
2021-06-23 03:02:56 +08:00
|
|
|
defineEmits(['bar'])
|
2021-02-13 17:06:34 +08:00
|
|
|
const r = ref(0)
|
|
|
|
</script>`).content
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('dedupe between user & helper', () => {
|
2021-06-30 03:22:26 +08:00
|
|
|
const { content } = compile(
|
|
|
|
`
|
2020-11-13 07:11:25 +08:00
|
|
|
<script setup>
|
|
|
|
import { ref } from 'vue'
|
2021-07-15 03:40:09 +08:00
|
|
|
let foo = $ref(1)
|
2020-11-13 07:11:25 +08:00
|
|
|
</script>
|
2021-06-30 03:22:26 +08:00
|
|
|
`,
|
2021-11-25 17:39:36 +08:00
|
|
|
{ refTransform: true }
|
2021-06-30 03:22:26 +08:00
|
|
|
)
|
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
|
|
|
)
|
|
|
|
})
|
2021-08-17 22:10:26 +08:00
|
|
|
})
|
2021-07-23 00:04:46 +08:00
|
|
|
|
2021-08-17 22:10:26 +08:00
|
|
|
// in dev mode, declared bindings are returned as an object from setup()
|
|
|
|
// when using TS, users may import types which should not be returned as
|
|
|
|
// values, so we need to check import usage in the template to determine
|
|
|
|
// what to be returned.
|
|
|
|
describe('dev mode import usage check', () => {
|
|
|
|
test('components', () => {
|
2021-07-23 00:04:46 +08:00
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
2021-08-17 22:10:26 +08:00
|
|
|
import { FooBar, FooBaz, FooQux, foo } from './x'
|
2021-07-23 00:04:46 +08:00
|
|
|
const fooBar: FooBar = 1
|
|
|
|
</script>
|
|
|
|
<template>
|
2021-08-17 22:10:26 +08:00
|
|
|
<FooBaz></FooBaz>
|
2021-07-23 00:04:46 +08:00
|
|
|
<foo-qux/>
|
2021-08-17 22:10:26 +08:00
|
|
|
<foo/>
|
|
|
|
FooBar
|
2021-07-23 00:04:46 +08:00
|
|
|
</template>
|
|
|
|
`)
|
2021-08-17 22:10:26 +08:00
|
|
|
// FooBar: should not be matched by plain text or incorrect case
|
2021-07-23 00:04:46 +08:00
|
|
|
// FooBaz: used as PascalCase component
|
|
|
|
// FooQux: used as kebab-case component
|
2021-08-17 22:10:26 +08:00
|
|
|
// foo: lowercase component
|
|
|
|
expect(content).toMatch(`return { fooBar, FooBaz, FooQux, foo }`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('directive', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { vMyDir } from './x'
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div v-my-dir></div>
|
|
|
|
</template>
|
|
|
|
`)
|
|
|
|
expect(content).toMatch(`return { vMyDir }`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2021-09-17 01:47:11 +08:00
|
|
|
// https://github.com/vuejs/vue-next/issues/4599
|
|
|
|
test('attribute expressions', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { bar, baz } from './x'
|
|
|
|
const cond = true
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
|
|
|
|
</template>
|
|
|
|
`)
|
|
|
|
expect(content).toMatch(`return { cond, bar, baz }`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2021-08-17 22:10:26 +08:00
|
|
|
test('vue interpolations', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { x, y, z, x$y } from './x'
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div :id="z + 'y'">{{ x }} {{ yy }} {{ x$y }}</div>
|
|
|
|
</template>
|
|
|
|
`)
|
2021-07-23 00:04:46 +08:00
|
|
|
// x: used in interpolation
|
2021-07-23 00:53:08 +08:00
|
|
|
// y: should not be matched by {{ yy }} or 'y' in binding exps
|
2021-08-10 00:17:22 +08:00
|
|
|
// x$y: #4274 should escape special chars when creating Regex
|
2021-08-17 22:10:26 +08:00
|
|
|
expect(content).toMatch(`return { x, z, x$y }`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
// #4340 interpolations in tempalte strings
|
|
|
|
test('js template string interpolations', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { VAR, VAR2, VAR3 } from './x'
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
{{ \`\${VAR}VAR2\${VAR3}\` }}
|
|
|
|
</template>
|
|
|
|
`)
|
|
|
|
// VAR2 should not be matched
|
|
|
|
expect(content).toMatch(`return { VAR, VAR3 }`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
// edge case: last tag in template
|
|
|
|
test('last tag', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { FooBaz, Last } from './x'
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<FooBaz></FooBaz>
|
|
|
|
<Last/>
|
|
|
|
</template>
|
|
|
|
`)
|
|
|
|
expect(content).toMatch(`return { FooBaz, Last }`)
|
2021-08-17 06:03:03 +08:00
|
|
|
assertCode(content)
|
2021-07-23 00:04:46 +08:00
|
|
|
})
|
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)
|
2021-06-26 01:14:49 +08:00
|
|
|
// in inline mode, no need to call expose() since nothing is exposed
|
|
|
|
// anyway!
|
|
|
|
expect(content).not.toMatch(`expose()`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with defineExpose()', () => {
|
|
|
|
const { content } = compile(
|
|
|
|
`
|
|
|
|
<script setup>
|
|
|
|
const count = ref(0)
|
|
|
|
defineExpose({ count })
|
|
|
|
</script>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`setup(__props, { expose })`)
|
|
|
|
expect(content).toMatch(`expose({ count })`)
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
|
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'
|
2021-06-29 06:55:27 +08:00
|
|
|
import vMyDir from './my-dir'
|
2020-11-24 05:32:24 +08:00
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div v-my-dir></div>
|
|
|
|
<ChildComp/>
|
|
|
|
<some-other-comp/>
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ inlineTemplate: true }
|
|
|
|
)
|
2021-06-29 06:55:27 +08:00
|
|
|
expect(content).toMatch('[_unref(vMyDir)]')
|
2020-11-24 05:32:24 +08:00
|
|
|
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-12-02 00:52:29 +08:00
|
|
|
import Foo, { bar } from './Foo.vue'
|
2020-11-13 07:11:25 +08:00
|
|
|
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>
|
2020-12-02 00:52:29 +08:00
|
|
|
<Foo>{{ bar }}</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
|
2020-12-02 00:52:29 +08:00
|
|
|
expect(content).toMatch(`createVNode(Foo,`)
|
|
|
|
// #2699 should unref named imports from .vue
|
|
|
|
expect(content).toMatch(`unref(bar)`)
|
2020-11-13 07:11:25 +08:00
|
|
|
// 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
|
2021-09-23 04:54:59 +08:00
|
|
|
expect(content).toMatch(`(count).value = $event`)
|
|
|
|
// const but maybe ref: assign if ref, otherwise do nothing
|
|
|
|
expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
|
2020-11-19 08:38:18 +08:00
|
|
|
// let: handle both cases
|
|
|
|
expect(content).toMatch(
|
2021-09-23 04:54:59 +08:00
|
|
|
`_isRef(lett) ? (lett).value = $event : lett = $event`
|
2020-11-19 08:38:18 +08:00
|
|
|
)
|
|
|
|
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
|
2021-05-29 05:58:46 +08:00
|
|
|
let v = ref(1)
|
2020-11-19 08:38:18 +08:00
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div @click="count = 1"/>
|
|
|
|
<div @click="maybe = count"/>
|
|
|
|
<div @click="lett = count"/>
|
2021-05-29 05:58:46 +08:00
|
|
|
<div @click="v += 1"/>
|
|
|
|
<div @click="v -= 1"/>
|
2021-09-06 06:02:50 +08:00
|
|
|
<div @click="() => {
|
2021-10-09 00:52:48 +08:00
|
|
|
let a = '' + lett
|
2021-09-06 06:02:50 +08:00
|
|
|
v = a
|
|
|
|
}"/>
|
|
|
|
<div @click="() => {
|
|
|
|
// nested scopes
|
|
|
|
(()=>{
|
|
|
|
let x = a
|
|
|
|
(()=>{
|
|
|
|
let z = x
|
|
|
|
let z2 = z
|
|
|
|
})
|
|
|
|
let lz = z
|
2021-10-09 00:52:48 +08:00
|
|
|
})
|
2021-09-06 06:02:50 +08:00
|
|
|
v = a
|
|
|
|
}"/>
|
2020-11-19 08:38:18 +08:00
|
|
|
</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
|
|
|
)
|
2021-05-29 05:58:46 +08:00
|
|
|
expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
|
|
|
|
expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
|
2021-09-06 06:02:50 +08:00
|
|
|
expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
|
|
|
|
expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
|
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
|
|
|
const props = defineProps({ foo: String })
|
2021-06-23 03:02:56 +08:00
|
|
|
const emit = defineEmits(['a', 'b'])
|
2020-11-13 07:11:25 +08:00
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
2021-09-02 00:40:36 +08:00
|
|
|
expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
|
2020-11-13 07:11:25 +08:00
|
|
|
props: { foo: String },
|
|
|
|
emits: ['a', 'b'],
|
2021-06-26 01:14:49 +08:00
|
|
|
setup(__props, { expose, 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-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
|
2021-09-06 06:13:12 +08:00
|
|
|
dateTime: Date
|
2020-11-25 04:12:59 +08:00
|
|
|
array: string[]
|
|
|
|
arrayRef: Array<any>
|
|
|
|
tuple: [number, number]
|
|
|
|
set: Set<string>
|
|
|
|
literal: 'foo'
|
|
|
|
optional?: any
|
|
|
|
recordRef: Record<string, null>
|
|
|
|
interface: Test
|
|
|
|
alias: Alias
|
2021-06-29 03:39:04 +08:00
|
|
|
method(): void
|
2021-09-17 01:45:55 +08:00
|
|
|
symbol: symbol
|
2020-11-25 04:12:59 +08:00
|
|
|
|
|
|
|
union: string | number
|
|
|
|
literalUnion: 'foo' | 'bar'
|
2021-08-17 05:40:00 +08:00
|
|
|
literalUnionNumber: 1 | 2 | 3 | 4 | 5
|
2020-11-25 04:12:59 +08:00
|
|
|
literalUnionMixed: 'foo' | 1 | boolean
|
|
|
|
intersection: Test & {}
|
2021-07-19 23:09:24 +08:00
|
|
|
foo: ((item: any) => boolean) | null
|
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 }`)
|
2021-09-06 06:13:12 +08:00
|
|
|
expect(content).toMatch(`dateTime: { type: Date, required: true }`)
|
2020-07-10 06:18:46 +08:00
|
|
|
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 }`)
|
2021-06-29 03:39:04 +08:00
|
|
|
expect(content).toMatch(`method: { type: Function, required: true }`)
|
2021-09-17 01:45:55 +08:00
|
|
|
expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
|
2020-07-10 06:18:46 +08:00
|
|
|
expect(content).toMatch(
|
|
|
|
`union: { type: [String, Number], required: true }`
|
|
|
|
)
|
2021-08-17 05:40:00 +08:00
|
|
|
expect(content).toMatch(`literalUnion: { type: String, required: true }`)
|
2020-07-10 06:18:46 +08:00
|
|
|
expect(content).toMatch(
|
2021-08-17 05:40:00 +08:00
|
|
|
`literalUnionNumber: { type: Number, required: true }`
|
2020-07-10 00:16:08 +08:00
|
|
|
)
|
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 }`)
|
2021-07-19 23:09:24 +08:00
|
|
|
expect(content).toMatch(`foo: { type: [Function, null], 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,
|
2021-09-06 06:13:12 +08:00
|
|
|
dateTime: BindingTypes.PROPS,
|
2020-11-19 04:17:50 +08:00
|
|
|
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,
|
2021-06-29 03:39:04 +08:00
|
|
|
method: BindingTypes.PROPS,
|
2021-09-17 01:45:55 +08:00
|
|
|
symbol: BindingTypes.PROPS,
|
2020-11-19 04:17:50 +08:00
|
|
|
union: BindingTypes.PROPS,
|
|
|
|
literalUnion: BindingTypes.PROPS,
|
2021-08-17 05:40:00 +08:00
|
|
|
literalUnionNumber: BindingTypes.PROPS,
|
2020-11-19 04:17:50 +08:00
|
|
|
literalUnionMixed: BindingTypes.PROPS,
|
2021-07-19 23:09:24 +08:00
|
|
|
intersection: BindingTypes.PROPS,
|
|
|
|
foo: BindingTypes.PROPS
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
2020-07-10 00:16:08 +08:00
|
|
|
})
|
|
|
|
|
2021-06-28 23:39:24 +08:00
|
|
|
test('defineProps w/ interface', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
interface Props { x?: number }
|
|
|
|
defineProps<Props>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`x: { type: Number, required: false }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
x: BindingTypes.PROPS
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('defineProps w/ exported interface', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
export interface Props { x?: number }
|
|
|
|
defineProps<Props>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`x: { type: Number, required: false }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
x: BindingTypes.PROPS
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-06 06:10:15 +08:00
|
|
|
test('defineProps w/ exported interface in normal script', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script lang="ts">
|
|
|
|
export interface Props { x?: number }
|
|
|
|
</script>
|
|
|
|
<script setup lang="ts">
|
|
|
|
defineProps<Props>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`x: { type: Number, required: false }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
x: BindingTypes.PROPS
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-28 23:39:24 +08:00
|
|
|
test('defineProps w/ type alias', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
type Props = { x?: number }
|
|
|
|
defineProps<Props>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`x: { type: Number, required: false }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
x: BindingTypes.PROPS
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('defineProps w/ exported type alias', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
export type Props = { x?: number }
|
|
|
|
defineProps<Props>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`x: { type: Number, required: false }`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
x: BindingTypes.PROPS
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-27 09:11:57 +08:00
|
|
|
test('withDefaults (static)', () => {
|
|
|
|
const { content, bindings } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
foo?: string
|
2021-09-06 05:36:46 +08:00
|
|
|
bar?: number;
|
|
|
|
baz: boolean;
|
2021-09-02 23:26:52 +08:00
|
|
|
qux?(): number
|
2021-06-27 09:11:57 +08:00
|
|
|
}>(), {
|
2021-09-02 23:26:52 +08:00
|
|
|
foo: 'hi',
|
|
|
|
qux() { return 1 }
|
2021-06-27 09:11:57 +08:00
|
|
|
})
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(
|
|
|
|
`foo: { type: String, required: false, default: 'hi' }`
|
|
|
|
)
|
|
|
|
expect(content).toMatch(`bar: { type: Number, required: false }`)
|
2021-09-02 23:26:52 +08:00
|
|
|
expect(content).toMatch(`baz: { type: Boolean, required: true }`)
|
|
|
|
expect(content).toMatch(
|
|
|
|
`qux: { type: Function, required: false, default() { return 1 } }`
|
|
|
|
)
|
|
|
|
expect(content).toMatch(
|
|
|
|
`{ foo: string, bar?: number, baz: boolean, qux(): number }`
|
|
|
|
)
|
2021-06-27 09:11:57 +08:00
|
|
|
expect(content).toMatch(`const props = __props`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.PROPS,
|
|
|
|
bar: BindingTypes.PROPS,
|
2021-09-02 23:26:52 +08:00
|
|
|
baz: BindingTypes.PROPS,
|
|
|
|
qux: BindingTypes.PROPS,
|
2021-06-27 09:11:57 +08:00
|
|
|
props: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('withDefaults (dynamic)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { defaults } from './foo'
|
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
foo?: string
|
|
|
|
bar?: number
|
2021-09-02 23:26:52 +08:00
|
|
|
baz: boolean
|
2021-06-27 09:11:57 +08:00
|
|
|
}>(), { ...defaults })
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
|
|
|
|
expect(content).toMatch(
|
|
|
|
`
|
|
|
|
_mergeDefaults({
|
|
|
|
foo: { type: String, required: false },
|
2021-09-02 23:26:52 +08:00
|
|
|
bar: { type: Number, required: false },
|
|
|
|
baz: { type: Boolean, required: true }
|
2021-06-27 09:11:57 +08:00
|
|
|
}, { ...defaults })`.trim()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
test('defineEmits 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">
|
2021-06-23 03:02:56 +08:00
|
|
|
const emit = defineEmits<(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),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2020-11-13 07:11:25 +08:00
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
test('defineEmits w/ type (union)', () => {
|
2020-11-25 04:12:59 +08:00
|
|
|
const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
|
2021-03-30 04:38:07 +08:00
|
|
|
expect(() =>
|
|
|
|
compile(`
|
|
|
|
<script setup lang="ts">
|
2021-06-23 03:02:56 +08:00
|
|
|
const emit = defineEmits<${type}>()
|
2021-03-30 04:38:07 +08:00
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
).toThrow()
|
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
test('defineEmits w/ type (type literal w/ call signatures)', () => {
|
2021-03-30 04:38:07 +08:00
|
|
|
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">
|
2021-06-23 03:02:56 +08:00
|
|
|
const emit = defineEmits<${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}),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
|
2020-07-10 00:16:08 +08:00
|
|
|
})
|
2021-06-29 04:03:27 +08:00
|
|
|
|
|
|
|
test('defineEmits w/ type (interface)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
interface Emits { (e: 'foo' | 'bar'): void }
|
|
|
|
const emit = defineEmits<Emits>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2021-06-29 04:03:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('defineEmits w/ type (exported interface)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
export interface Emits { (e: 'foo' | 'bar'): void }
|
|
|
|
const emit = defineEmits<Emits>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2021-06-29 04:03:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('defineEmits w/ type (type alias)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
type Emits = { (e: 'foo' | 'bar'): void }
|
|
|
|
const emit = defineEmits<Emits>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2021-06-29 04:03:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('defineEmits w/ type (exported type alias)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
export type Emits = { (e: 'foo' | 'bar'): void }
|
|
|
|
const emit = defineEmits<Emits>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2021-06-29 04:03:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('defineEmits w/ type (referenced function type)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
type Emits = (e: 'foo' | 'bar') => void
|
|
|
|
const emit = defineEmits<Emits>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2021-06-29 04:03:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('defineEmits w/ type (referenced exported function type)', () => {
|
|
|
|
const { content } = compile(`
|
|
|
|
<script setup lang="ts">
|
|
|
|
export type Emits = (e: 'foo' | 'bar') => void
|
|
|
|
const emit = defineEmits<Emits>()
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
|
2021-09-02 23:51:41 +08:00
|
|
|
expect(content).toMatch(`emits: ["foo", "bar"]`)
|
2021-06-29 04:03:27 +08:00
|
|
|
})
|
2021-07-01 00:03:42 +08:00
|
|
|
|
|
|
|
test('runtime Enum', () => {
|
|
|
|
const { content, bindings } = compile(
|
|
|
|
`<script setup lang="ts">
|
|
|
|
enum Foo { A = 123 }
|
|
|
|
</script>`
|
|
|
|
)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
Foo: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
2021-07-20 05:31:13 +08:00
|
|
|
|
2021-10-08 23:57:39 +08:00
|
|
|
test('runtime Enum in normal script', () => {
|
|
|
|
const { content, bindings } = compile(
|
|
|
|
`<script lang="ts">
|
|
|
|
export enum D { D = "D" }
|
|
|
|
const enum C { C = "C" }
|
|
|
|
enum B { B = "B" }
|
|
|
|
</script>
|
|
|
|
<script setup lang="ts">
|
|
|
|
enum Foo { A = 123 }
|
|
|
|
</script>`
|
|
|
|
)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
D: BindingTypes.SETUP_CONST,
|
|
|
|
C: BindingTypes.SETUP_CONST,
|
|
|
|
B: BindingTypes.SETUP_CONST,
|
|
|
|
Foo: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-20 05:31:13 +08:00
|
|
|
test('const Enum', () => {
|
|
|
|
const { content, bindings } = compile(
|
|
|
|
`<script setup lang="ts">
|
|
|
|
const enum Foo { A = 123 }
|
|
|
|
</script>`
|
|
|
|
)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
Foo: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
2020-07-09 09:11:57 +08:00
|
|
|
})
|
|
|
|
|
2020-07-11 06:00:13 +08:00
|
|
|
describe('async/await detection', () => {
|
2021-09-17 04:23:46 +08:00
|
|
|
function assertAwaitDetection(code: string, shouldAsync = true) {
|
2021-06-30 03:22:26 +08:00
|
|
|
const { content } = compile(`<script setup>${code}</script>`, {
|
2021-11-25 17:39:36 +08:00
|
|
|
refTransform: true
|
2021-06-30 03:22:26 +08:00
|
|
|
})
|
2021-07-07 02:31:53 +08:00
|
|
|
if (shouldAsync) {
|
|
|
|
expect(content).toMatch(`let __temp, __restore`)
|
|
|
|
}
|
2020-11-17 07:27:15 +08:00
|
|
|
expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
|
2021-09-17 04:23:46 +08:00
|
|
|
assertCode(content)
|
2021-09-17 04:49:59 +08:00
|
|
|
return content
|
2020-07-11 06:00:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
test('expression statement', () => {
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`await foo`)
|
2020-07-11 06:00:13 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('variable', () => {
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`const a = 1 + (await foo)`)
|
2020-07-11 06:00:13 +08:00
|
|
|
})
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
test('ref', () => {
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`let a = $ref(1 + (await foo))`)
|
|
|
|
})
|
|
|
|
|
2021-09-17 04:49:59 +08:00
|
|
|
// #4448
|
2021-09-17 04:23:46 +08:00
|
|
|
test('nested await', () => {
|
|
|
|
assertAwaitDetection(`await (await foo)`)
|
|
|
|
assertAwaitDetection(`await ((await foo))`)
|
|
|
|
assertAwaitDetection(`await (await (await foo))`)
|
2020-07-11 06:00:13 +08:00
|
|
|
})
|
|
|
|
|
2021-09-17 04:49:59 +08:00
|
|
|
// should prepend semicolon
|
2021-09-17 05:07:29 +08:00
|
|
|
test('nested leading await in expression statement', () => {
|
2021-09-17 04:49:59 +08:00
|
|
|
const code = assertAwaitDetection(`foo()\nawait 1 + await 2`)
|
|
|
|
expect(code).toMatch(`foo()\n;(`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// #4596 should NOT prepend semicolon
|
|
|
|
test('single line conditions', () => {
|
|
|
|
const code = assertAwaitDetection(`if (false) await foo()`)
|
|
|
|
expect(code).not.toMatch(`if (false) ;(`)
|
|
|
|
})
|
|
|
|
|
2020-07-11 06:00:13 +08:00
|
|
|
test('nested statements', () => {
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
|
2020-07-11 06:00:13 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should ignore await inside functions', () => {
|
|
|
|
// function declaration
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`async function foo() { await bar }`, false)
|
2020-07-11 06:00:13 +08:00
|
|
|
// function expression
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`const foo = async () => { await bar }`, false)
|
2020-07-11 06:00:13 +08:00
|
|
|
// object method
|
2021-09-17 04:23:46 +08:00
|
|
|
assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
|
2020-07-11 06:00:13 +08:00
|
|
|
// class method
|
|
|
|
assertAwaitDetection(
|
|
|
|
`const cls = class Foo { async method() { await bar }}`,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
test('defineProps/Emit() w/ both type and non-type args', () => {
|
|
|
|
expect(() => {
|
|
|
|
compile(`<script setup lang="ts">
|
|
|
|
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">
|
2021-06-23 03:02:56 +08:00
|
|
|
defineEmits<{}>({})
|
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-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>
|
|
|
|
const bar = 'hello'
|
2021-06-23 03:02:56 +08:00
|
|
|
defineEmits([bar])
|
2020-11-25 04:12:59 +08:00
|
|
|
</script>`)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
2021-09-22 22:10:20 +08:00
|
|
|
|
|
|
|
// #4644
|
|
|
|
expect(() =>
|
|
|
|
compile(`
|
|
|
|
<script>const bar = 1</script>
|
|
|
|
<script setup>
|
|
|
|
defineProps({
|
|
|
|
foo: {
|
|
|
|
default: () => bar
|
|
|
|
}
|
|
|
|
})
|
|
|
|
</script>`)
|
|
|
|
).not.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>
|
|
|
|
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
|
|
|
})
|
2021-06-23 03:02:56 +08:00
|
|
|
defineEmits({
|
2020-11-25 04:12:59 +08:00
|
|
|
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 { bar } from './bar'
|
|
|
|
defineProps({
|
|
|
|
foo: {
|
|
|
|
default: () => bar
|
|
|
|
}
|
|
|
|
})
|
2021-06-23 03:02:56 +08:00
|
|
|
defineEmits({
|
2020-11-25 04:12:59 +08:00
|
|
|
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()
|
|
|
|
})
|
2021-03-30 04:11:31 +08:00
|
|
|
|
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
|
|
|
|
})
|
2021-03-30 04:11:31 +08:00
|
|
|
expect(bindings!.__isScriptSetup).toBe(false)
|
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
|
|
|
})
|
2021-03-30 04:11:31 +08:00
|
|
|
expect(bindings!.__isScriptSetup).toBe(false)
|
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
|
|
|
})
|
2021-03-30 04:11:31 +08:00
|
|
|
expect(bindings!.__isScriptSetup).toBe(false)
|
2020-08-29 04:21:03 +08:00
|
|
|
})
|
|
|
|
|
2021-09-17 05:22:29 +08:00
|
|
|
it('recognizes exported vars', () => {
|
|
|
|
const { bindings } = compile(`
|
|
|
|
<script>
|
|
|
|
export const foo = 2
|
|
|
|
</script>
|
|
|
|
<script setup>
|
|
|
|
console.log(foo)
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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
|
|
|
})
|
2021-03-30 04:11:31 +08:00
|
|
|
expect(bindings!.__isScriptSetup).toBe(false)
|
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>
|
2021-06-29 04:03:27 +08:00
|
|
|
import { ref as r } from 'vue'
|
2020-11-25 04:12:59 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|