+
+ {{ count }} {{ constant }} {{ other }}
+
+ `,
+ { inlineTemplate: true }
+ )
+ assertCode(content)
+ // 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)`)
+ // should unref const w/ call init (e.g. ref())
+ expect(content).toMatch(`unref(count)`)
+ // 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`)
+ })
+ })
+
+ describe('with TypeScript', () => {
test('hoist type declarations', () => {
const { content } = compile(`
+ `)
+ assertCode(content)
+ expect(content).toMatch(`export default defineComponent({
+ props: { foo: String },
+ emits: ['a', 'b'],
+ setup(__props, { props, emit }) {`)
+ })
+
+ test('defineContext w/ type / extract props', () => {
+ const { content, bindings } = compile(`
+ `)
assertCode(content)
expect(content).toMatch(`string: { type: String, required: true }`)
@@ -154,21 +263,57 @@ describe('SFC compile
+ `)
+ assertCode(content)
+ expect(content).toMatch(`props: {},\n emit: (e: 'foo' | 'bar') => void,`)
+ expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
+ })
+
+ test('defineContext w/ type / extract emits (union)', () => {
+ const { content } = compile(`
+
`)
assertCode(content)
expect(content).toMatch(
- `declare function __emit__(e: 'foo' | 'bar'): void`
- )
- expect(content).toMatch(
- `declare function __emit__(e: 'baz', id: number): void`
+ `props: {},\n emit: ((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void),`
)
expect(content).toMatch(
`emits: ["foo", "bar", "baz"] as unknown as undefined`
@@ -220,9 +365,7 @@ describe('SFC compile `)
- expect(content).toMatch(
- `export ${shouldAsync ? `async ` : ``}function setup`
- )
+ expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup()`)
}
test('expression statement', () => {
@@ -459,25 +602,27 @@ describe('SFC compile `)
- ).toThrow(`cannot contain non-type named or * exports`)
+ ).toThrow(moduleErrorMsg)
expect(() =>
compile(``)
- ).toThrow(`cannot contain non-type named or * exports`)
+ ).toThrow(moduleErrorMsg)
expect(() =>
compile(``)
- ).toThrow(`cannot contain non-type named or * exports`)
+ ).toThrow(moduleErrorMsg)
})
test('ref: non-assignment expressions', () => {
@@ -488,97 +633,74 @@ describe('SFC compile `)
+ }).toThrow(`cannot accept both type and non-type arguments`)
+ })
+
+ test('defineContext() referencing local var', () => {
expect(() =>
compile(``)
).toThrow(`cannot reference locally declared variables`)
})
- test('export default referencing ref declarations', () => {
+ test('defineContext() referencing ref declarations', () => {
expect(() =>
compile(``)
).toThrow(`cannot reference locally declared variables`)
})
- test('should allow export default referencing scope var', () => {
+ test('should allow defineContext() referencing scope var', () => {
assertCode(
compile(``).content
)
})
- test('should allow export default referencing imported binding', () => {
+ test('should allow defineContext() referencing imported binding', () => {
assertCode(
compile(``).content
)
})
-
- test('error on duplicated default export', () => {
- expect(() =>
- compile(`
-
-
- `)
- ).toThrow(`Default export is already declared`)
-
- expect(() =>
- compile(`
-
-
- `)
- ).toThrow(`Default export is already declared`)
-
- expect(() =>
- compile(`
-
-
- `)
- ).toThrow(`Default export is already declared`)
- })
})
})
@@ -779,11 +901,12 @@ describe('SFC analyze
`)
expect(bindings).toStrictEqual({
diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts
index ea5bf95f..b90b677f 100644
--- a/packages/compiler-sfc/src/compileScript.ts
+++ b/packages/compiler-sfc/src/compileScript.ts
@@ -15,12 +15,12 @@ import {
TSType,
TSTypeLiteral,
TSFunctionType,
- TSDeclareFunction,
ObjectProperty,
ArrayExpression,
Statement,
Expression,
- LabeledStatement
+ LabeledStatement,
+ TSUnionType
} from '@babel/types'
import { walk } from 'estree-walker'
import { RawSourceMap } from 'source-map'
@@ -143,6 +143,7 @@ export function compileScript(
const refIdentifiers: Set