Skip to content

Commit 9c8f114

Browse files
committed
fix(compiler-sfc): support resolve type declaration from normal script
fix(compiler-sfc): support resolve type declaration from normal script fix(compiler-sfc): support resolve type declaration from normal script update test case
1 parent 4a3237a commit 9c8f114

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,30 @@ const props = __props as { foo: string, bar?: number, baz: boolean, qux(): numbe
14451445

14461446

14471447

1448+
return { props }
1449+
}
1450+
1451+
})"
1452+
`;
1453+
1454+
exports[`SFC compile <script setup> with TypeScript withDefaults + normal script 1`] = `
1455+
"import { defineComponent as _defineComponent } from 'vue'
1456+
1457+
interface Props {
1458+
a?: string;
1459+
}
1460+
1461+
export default /*#__PURE__*/_defineComponent({
1462+
props: {
1463+
a: { type: String, required: false, default: \\"a\\" }
1464+
},
1465+
setup(__props: any, { expose }) {
1466+
expose();
1467+
1468+
const props = __props as { a: string }
1469+
1470+
1471+
14481472
return { props }
14491473
}
14501474

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,22 @@ const emit = defineEmits(['a', 'b'])
954954
)
955955
})
956956

957+
test('withDefaults + normal script', () => {
958+
const { content } = compile(`
959+
<script lang="ts">
960+
interface Props {
961+
a?: string;
962+
}
963+
</script>
964+
<script setup lang="ts">
965+
const props = withDefaults(defineProps<Props>(), {
966+
a: "a",
967+
});
968+
</script>
969+
`)
970+
assertCode(content)
971+
})
972+
957973
test('defineEmits w/ type', () => {
958974
const { content } = compile(`
959975
<script setup lang="ts">

packages/compiler-sfc/src/compileScript.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ export function compileScript(
277277
| TSInterfaceBody
278278
| undefined
279279
let emitsTypeDeclRaw: Node | undefined
280+
// propsTypeDecl or emitsTypeDecl decl from normal script
281+
let isTypeDeclFromNormalScript = false
280282
let emitIdentifier: string | undefined
281283
let hasAwait = false
282284
let hasInlinedSsrRenderFn = false
@@ -554,12 +556,20 @@ export function compileScript(
554556
return isQualifiedType(node.declaration)
555557
}
556558
}
557-
const body = scriptAst
558-
? [...scriptSetupAst.body, ...scriptAst.body]
559-
: scriptSetupAst.body
560-
for (const node of body) {
559+
560+
if (scriptAst) {
561+
for (const node of scriptAst.body) {
562+
const qualified = isQualifiedType(node)
563+
if (qualified) {
564+
isTypeDeclFromNormalScript = true
565+
return qualified
566+
}
567+
}
568+
}
569+
for (const node of scriptSetupAst.body) {
561570
const qualified = isQualifiedType(node)
562571
if (qualified) {
572+
isTypeDeclFromNormalScript = false
563573
return qualified
564574
}
565575
}
@@ -740,7 +750,7 @@ export function compileScript(
740750
}
741751

742752
function genSetupPropsType(node: TSTypeLiteral | TSInterfaceBody) {
743-
const scriptSetupSource = scriptSetup!.content
753+
const scriptSource = isTypeDeclFromNormalScript ? script!.content : scriptSetup!.content
744754
if (hasStaticWithDefaults()) {
745755
// if withDefaults() is used, we need to remove the optional flags
746756
// on props that have default values
@@ -761,20 +771,20 @@ export function compileScript(
761771
res +=
762772
m.key.name +
763773
(m.type === 'TSMethodSignature' ? '()' : '') +
764-
scriptSetupSource.slice(
774+
scriptSource.slice(
765775
m.typeAnnotation.start!,
766776
m.typeAnnotation.end!
767777
) +
768778
', '
769779
} else {
770780
res +=
771-
scriptSetupSource.slice(m.start!, m.typeAnnotation.end!) + `, `
781+
scriptSource.slice(m.start!, m.typeAnnotation.end!) + `, `
772782
}
773783
}
774784
}
775785
return (res.length ? res.slice(0, -2) : res) + ` }`
776786
} else {
777-
return scriptSetupSource.slice(node.start!, node.end!)
787+
return scriptSource.slice(node.start!, node.end!)
778788
}
779789
}
780790

0 commit comments

Comments
 (0)