Skip to content

Commit

Permalink
fix(compiler-sfc): support resolve type declaration from normal script
Browse files Browse the repository at this point in the history
fix(compiler-sfc): support resolve type declaration from normal script

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

update test case
  • Loading branch information
edison1105 committed Apr 29, 2022
1 parent 4a3237a commit 9c8f114
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
Expand Up @@ -1445,6 +1445,30 @@ const props = __props as { foo: string, bar?: number, baz: boolean, qux(): numbe



return { props }
}

})"
`;

exports[`SFC compile <script setup> with TypeScript withDefaults + 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 }
}

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

test('withDefaults + 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('defineEmits w/ type', () => {
const { content } = compile(`
<script setup lang="ts">
Expand Down
26 changes: 18 additions & 8 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -277,6 +277,8 @@ export function compileScript(
| TSInterfaceBody
| undefined
let emitsTypeDeclRaw: Node | undefined
// propsTypeDecl or emitsTypeDecl decl from normal script
let isTypeDeclFromNormalScript = false
let emitIdentifier: string | undefined
let hasAwait = false
let hasInlinedSsrRenderFn = false
Expand Down Expand Up @@ -554,12 +556,20 @@ export function compileScript(
return isQualifiedType(node.declaration)
}
}
const body = scriptAst
? [...scriptSetupAst.body, ...scriptAst.body]
: scriptSetupAst.body
for (const node of body) {

if (scriptAst) {
for (const node of scriptAst.body) {
const qualified = isQualifiedType(node)
if (qualified) {
isTypeDeclFromNormalScript = true
return qualified
}
}
}
for (const node of scriptSetupAst.body) {
const qualified = isQualifiedType(node)
if (qualified) {
isTypeDeclFromNormalScript = false
return qualified
}
}
Expand Down Expand Up @@ -740,7 +750,7 @@ export function compileScript(
}

function genSetupPropsType(node: TSTypeLiteral | TSInterfaceBody) {
const scriptSetupSource = scriptSetup!.content
const scriptSource = isTypeDeclFromNormalScript ? 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 @@ -761,20 +771,20 @@ 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!) + `, `
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

0 comments on commit 9c8f114

Please sign in to comment.