Skip to content

Commit 30399d4

Browse files
authoredFeb 1, 2023
fix(compiler-sfc): support resolving type declaration from normal script (#5831)
fix #5830
1 parent 1fde49c commit 30399d4

File tree

3 files changed

+99
-17
lines changed

3 files changed

+99
-17
lines changed
 

‎packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+42
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,24 @@ export default /*#__PURE__*/_defineComponent({
14461446

14471447

14481448

1449+
return { emit }
1450+
}
1451+
1452+
})"
1453+
`;
1454+
1455+
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type from normal script 1`] = `
1456+
"import { defineComponent as _defineComponent } from 'vue'
1457+
1458+
export interface Emits { (e: 'foo' | 'bar'): void }
1459+
1460+
export default /*#__PURE__*/_defineComponent({
1461+
emits: [\\"foo\\", \\"bar\\"],
1462+
setup(__props, { expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
1463+
expose();
1464+
1465+
1466+
14491467
return { emit }
14501468
}
14511469

@@ -1764,6 +1782,30 @@ return { props, get defaults() { return defaults } }
17641782
})"
17651783
`;
17661784

1785+
exports[`SFC compile <script setup> > with TypeScript > withDefaults (static) + normal script 1`] = `
1786+
"import { defineComponent as _defineComponent } from 'vue'
1787+
1788+
interface Props {
1789+
a?: string;
1790+
}
1791+
1792+
export default /*#__PURE__*/_defineComponent({
1793+
props: {
1794+
a: { type: String, required: false, default: \\"a\\" }
1795+
},
1796+
setup(__props: any, { expose }) {
1797+
expose();
1798+
1799+
const props = __props as { a: string };
1800+
1801+
1802+
1803+
return { props }
1804+
}
1805+
1806+
})"
1807+
`;
1808+
17671809
exports[`SFC compile <script setup> > with TypeScript > withDefaults (static) 1`] = `
17681810
"import { defineComponent as _defineComponent } from 'vue'
17691811

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

+31
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,22 @@ const emit = defineEmits(['a', 'b'])
11211121
})
11221122
})
11231123

1124+
test('withDefaults (static) + normal script', () => {
1125+
const { content } = compile(`
1126+
<script lang="ts">
1127+
interface Props {
1128+
a?: string;
1129+
}
1130+
</script>
1131+
<script setup lang="ts">
1132+
const props = withDefaults(defineProps<Props>(), {
1133+
a: "a",
1134+
});
1135+
</script>
1136+
`)
1137+
assertCode(content)
1138+
})
1139+
11241140
// #7111
11251141
test('withDefaults (static) w/ production mode', () => {
11261142
const { content } = compile(
@@ -1261,6 +1277,21 @@ const emit = defineEmits(['a', 'b'])
12611277
expect(content).toMatch(`emits: ["foo", "bar"]`)
12621278
})
12631279

1280+
1281+
test('defineEmits w/ type from normal script', () => {
1282+
const { content } = compile(`
1283+
<script lang="ts">
1284+
export interface Emits { (e: 'foo' | 'bar'): void }
1285+
</script>
1286+
<script setup lang="ts">
1287+
const emit = defineEmits<Emits>()
1288+
</script>
1289+
`)
1290+
assertCode(content)
1291+
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
1292+
expect(content).toMatch(`emits: ["foo", "bar"]`)
1293+
})
1294+
12641295
test('defineEmits w/ type (type alias)', () => {
12651296
const { content } = compile(`
12661297
<script setup lang="ts">

‎packages/compiler-sfc/src/compileScript.ts

+26-17
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ export interface ImportBinding {
136136
isUsedInTemplate: boolean
137137
}
138138

139+
type FromNormalScript<T> = T & { __fromNormalScript?: boolean | null }
140+
type PropsDeclType = FromNormalScript<TSTypeLiteral | TSInterfaceBody>
141+
type EmitsDeclType = FromNormalScript<
142+
TSFunctionType | TSTypeLiteral | TSInterfaceBody
143+
>
144+
139145
/**
140146
* Compile `<script setup>`
141147
* It requires the whole SFC descriptor because we need to handle and merge
@@ -287,15 +293,11 @@ export function compileScript(
287293
let propsRuntimeDefaults: ObjectExpression | undefined
288294
let propsDestructureDecl: Node | undefined
289295
let propsDestructureRestId: string | undefined
290-
let propsTypeDecl: TSTypeLiteral | TSInterfaceBody | undefined
296+
let propsTypeDecl: PropsDeclType | undefined
291297
let propsTypeDeclRaw: Node | undefined
292298
let propsIdentifier: string | undefined
293299
let emitsRuntimeDecl: Node | undefined
294-
let emitsTypeDecl:
295-
| TSFunctionType
296-
| TSTypeLiteral
297-
| TSInterfaceBody
298-
| undefined
300+
let emitsTypeDecl: EmitsDeclType | undefined
299301
let emitsTypeDeclRaw: Node | undefined
300302
let emitIdentifier: string | undefined
301303
let hasAwait = false
@@ -436,7 +438,7 @@ export function compileScript(
436438
propsTypeDecl = resolveQualifiedType(
437439
propsTypeDeclRaw,
438440
node => node.type === 'TSTypeLiteral'
439-
) as TSTypeLiteral | TSInterfaceBody | undefined
441+
) as PropsDeclType | undefined
440442

441443
if (!propsTypeDecl) {
442444
error(
@@ -567,7 +569,7 @@ export function compileScript(
567569
emitsTypeDecl = resolveQualifiedType(
568570
emitsTypeDeclRaw,
569571
node => node.type === 'TSFunctionType' || node.type === 'TSTypeLiteral'
570-
) as TSFunctionType | TSTypeLiteral | TSInterfaceBody | undefined
572+
) as EmitsDeclType | undefined
571573

572574
if (!emitsTypeDecl) {
573575
error(
@@ -667,7 +669,7 @@ export function compileScript(
667669
function resolveQualifiedType(
668670
node: Node,
669671
qualifier: (node: Node) => boolean
670-
) {
672+
): Node | undefined {
671673
if (qualifier(node)) {
672674
return node
673675
}
@@ -677,7 +679,8 @@ export function compileScript(
677679
) {
678680
const refName = node.typeName.name
679681
const body = getAstBody()
680-
for (const node of body) {
682+
for (let i = 0; i < body.length; i++) {
683+
const node = body[i]
681684
let qualified = isQualifiedType(
682685
node,
683686
qualifier,
@@ -690,6 +693,8 @@ export function compileScript(
690693
filterExtendsType(extendsTypes, bodies)
691694
qualified.body = bodies
692695
}
696+
;(qualified as FromNormalScript<Node>).__fromNormalScript =
697+
scriptAst && i >= scriptSetupAst.body.length
693698
return qualified
694699
}
695700
}
@@ -877,8 +882,10 @@ export function compileScript(
877882
}
878883
}
879884

880-
function genSetupPropsType(node: TSTypeLiteral | TSInterfaceBody) {
881-
const scriptSetupSource = scriptSetup!.content
885+
function genSetupPropsType(node: PropsDeclType) {
886+
const scriptSource = node.__fromNormalScript
887+
? script!.content
888+
: scriptSetup!.content
882889
if (hasStaticWithDefaults()) {
883890
// if withDefaults() is used, we need to remove the optional flags
884891
// on props that have default values
@@ -903,20 +910,19 @@ export function compileScript(
903910
res +=
904911
m.key.name +
905912
(m.type === 'TSMethodSignature' ? '()' : '') +
906-
scriptSetupSource.slice(
913+
scriptSource.slice(
907914
m.typeAnnotation.start!,
908915
m.typeAnnotation.end!
909916
) +
910917
', '
911918
} else {
912-
res +=
913-
scriptSetupSource.slice(m.start!, m.typeAnnotation.end!) + `, `
919+
res += scriptSource.slice(m.start!, m.typeAnnotation.end!) + `, `
914920
}
915921
}
916922
}
917923
return (res.length ? res.slice(0, -2) : res) + ` }`
918924
} else {
919-
return scriptSetupSource.slice(node.start!, node.end!)
925+
return scriptSource.slice(node.start!, node.end!)
920926
}
921927
}
922928

@@ -1480,7 +1486,10 @@ export function compileScript(
14801486
if (destructureElements.length) {
14811487
args += `, { ${destructureElements.join(', ')} }`
14821488
if (emitsTypeDecl) {
1483-
args += `: { emit: (${scriptSetup.content.slice(
1489+
const content = emitsTypeDecl.__fromNormalScript
1490+
? script!.content
1491+
: scriptSetup.content
1492+
args += `: { emit: (${content.slice(
14841493
emitsTypeDecl.start!,
14851494
emitsTypeDecl.end!
14861495
)}), expose: any, slots: any, attrs: any }`

0 commit comments

Comments
 (0)
Please sign in to comment.