Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): support resolve type declaration from normal script #5831

Merged
merged 8 commits into from Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1426,6 +1426,24 @@ export default /*#__PURE__*/_defineComponent({



return { emit }
}

})"
`;

exports[`SFC compile <script setup> with TypeScript defineEmits w/ type from normal script 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

export interface Emits { (e: 'foo' | 'bar'): void }

export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
expose();



return { emit }
}

Expand Down Expand Up @@ -1715,6 +1733,30 @@ return { props, defaults }
})"
`;

exports[`SFC compile <script setup> with TypeScript withDefaults (static) + normal script 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

interface Props {
a?: string;
}

export default /*#__PURE__*/_defineComponent({
props: {
a: { type: String, required: false, default: \\"a\\" }
},
setup(__props: any, { expose }) {
expose();

const props = __props as { a: string }



return { props }
}

})"
`;

exports[`SFC compile <script setup> with TypeScript withDefaults (static) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

Expand Down
31 changes: 31 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -1004,6 +1004,22 @@ const emit = defineEmits(['a', 'b'])
})
})

test('withDefaults (static) + normal script', () => {
const { content } = compile(`
<script lang="ts">
interface Props {
a?: string;
}
</script>
<script setup lang="ts">
const props = withDefaults(defineProps<Props>(), {
a: "a",
});
</script>
`)
assertCode(content)
})

test('withDefaults (dynamic)', () => {
const { content } = compile(`
<script setup lang="ts">
Expand Down Expand Up @@ -1085,6 +1101,21 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`emits: ["foo", "bar"]`)
})


test('defineEmits w/ type from normal script', () => {
const { content } = compile(`
<script lang="ts">
export interface Emits { (e: 'foo' | 'bar'): void }
</script>
<script setup lang="ts">
const emit = defineEmits<Emits>()
</script>
`)
assertCode(content)
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
expect(content).toMatch(`emits: ["foo", "bar"]`)
})

test('defineEmits w/ type (type alias)', () => {
const { content } = compile(`
<script setup lang="ts">
Expand Down
41 changes: 24 additions & 17 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -134,6 +134,12 @@ export interface ImportBinding {
isUsedInTemplate: boolean
}

type FromNormalScript = { __fromNormalScript?: boolean }

type PropsDeclType = (TSTypeLiteral | TSInterfaceBody) & FromNormalScript

type EmitsDeclType = (TSFunctionType | TSTypeLiteral | TSInterfaceBody) & FromNormalScript

sxzz marked this conversation as resolved.
Show resolved Hide resolved
/**
* Compile `<script setup>`
* It requires the whole SFC descriptor because we need to handle and merge
Expand Down Expand Up @@ -286,15 +292,11 @@ export function compileScript(
let propsRuntimeDefaults: ObjectExpression | undefined
let propsDestructureDecl: Node | undefined
let propsDestructureRestId: string | undefined
let propsTypeDecl: TSTypeLiteral | TSInterfaceBody | undefined
let propsTypeDecl: PropsDeclType | undefined
let propsTypeDeclRaw: Node | undefined
let propsIdentifier: string | undefined
let emitsRuntimeDecl: Node | undefined
let emitsTypeDecl:
| TSFunctionType
| TSTypeLiteral
| TSInterfaceBody
| undefined
let emitsTypeDecl: EmitsDeclType | undefined
let emitsTypeDeclRaw: Node | undefined
let emitIdentifier: string | undefined
let hasAwait = false
Expand Down Expand Up @@ -414,7 +416,7 @@ export function compileScript(
propsTypeDecl = resolveQualifiedType(
propsTypeDeclRaw,
node => node.type === 'TSTypeLiteral'
) as TSTypeLiteral | TSInterfaceBody | undefined
) as PropsDeclType | undefined

if (!propsTypeDecl) {
error(
Expand Down Expand Up @@ -541,7 +543,7 @@ export function compileScript(
emitsTypeDecl = resolveQualifiedType(
emitsTypeDeclRaw,
node => node.type === 'TSFunctionType' || node.type === 'TSTypeLiteral'
) as TSFunctionType | TSTypeLiteral | TSInterfaceBody | undefined
) as EmitsDeclType | undefined

if (!emitsTypeDecl) {
error(
Expand Down Expand Up @@ -638,7 +640,7 @@ export function compileScript(
function resolveQualifiedType(
node: Node,
qualifier: (node: Node) => boolean
) {
): (Node & FromNormalScript)| undefined {
sxzz marked this conversation as resolved.
Show resolved Hide resolved
if (qualifier(node)) {
return node
}
Expand All @@ -648,7 +650,8 @@ export function compileScript(
) {
const refName = node.typeName.name
const body = getAstBody()
for (const node of body) {
for (let i = 0; i < body.length; i++) {
const node = body[i]
let qualified = isQualifiedType(
node,
qualifier,
Expand All @@ -661,6 +664,8 @@ export function compileScript(
filterExtendsType(extendsTypes, bodies)
qualified.body = bodies
}
;(qualified as Node & FromNormalScript).__fromNormalScript =
scriptAst && i >= scriptSetupAst.body.length
return qualified
}
}
Expand Down Expand Up @@ -840,8 +845,10 @@ export function compileScript(
}
}

function genSetupPropsType(node: TSTypeLiteral | TSInterfaceBody) {
const scriptSetupSource = scriptSetup!.content
function genSetupPropsType(node: PropsDeclType) {
const scriptSource = node.__fromNormalScript
? script!.content
: scriptSetup!.content
if (hasStaticWithDefaults()) {
// if withDefaults() is used, we need to remove the optional flags
// on props that have default values
Expand All @@ -862,20 +869,19 @@ export function compileScript(
res +=
m.key.name +
(m.type === 'TSMethodSignature' ? '()' : '') +
scriptSetupSource.slice(
scriptSource.slice(
m.typeAnnotation.start!,
m.typeAnnotation.end!
) +
', '
} else {
res +=
scriptSetupSource.slice(m.start!, m.typeAnnotation.end!) + `, `
res += scriptSource.slice(m.start!, m.typeAnnotation.end!) + `, `
}
}
}
return (res.length ? res.slice(0, -2) : res) + ` }`
} else {
return scriptSetupSource.slice(node.start!, node.end!)
return scriptSource.slice(node.start!, node.end!)
}
}

Expand Down Expand Up @@ -1435,7 +1441,8 @@ export function compileScript(
if (destructureElements.length) {
args += `, { ${destructureElements.join(', ')} }`
if (emitsTypeDecl) {
args += `: { emit: (${scriptSetup.content.slice(
const content = emitsTypeDecl.__fromNormalScript ? script!.content : scriptSetup.content
args += `: { emit: (${content.slice(
emitsTypeDecl.start!,
emitsTypeDecl.end!
)}), expose: any, slots: any, attrs: any }`
Expand Down