Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Sep 6, 2021
1 parent 52a386d commit c8af08d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
Expand Up @@ -913,12 +913,13 @@ return { }
exports[`SFC compile <script setup> with TypeScript defineProps w/ extends interface 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
interface Foo { x?: number }
interface Bar extends Foo { y?: number }
interface Props extends Bar {
z: number
y: string
}
interface Bar extends Foo { y?: number }
interface Props extends Bar {
z: number
y: string
}
interface Foo { x?: number }
export default /*#__PURE__*/_defineComponent({
props: {
Expand All @@ -929,7 +930,7 @@ export default /*#__PURE__*/_defineComponent({
setup(__props: any, { expose }) {
expose()
return { }
}
Expand Down
16 changes: 9 additions & 7 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -775,14 +775,16 @@ const emit = defineEmits(['a', 'b'])

test('defineProps w/ extends interface', () => {
const { content, bindings } = compile(`
<script lang="ts">
interface Foo { x?: number }
</script>
<script setup lang="ts">
interface Foo { x?: number }
interface Bar extends Foo { y?: number }
interface Props extends Bar {
z: number
y: string
}
defineProps<Props>()
interface Bar extends Foo { y?: number }
interface Props extends Bar {
z: number
y: string
}
defineProps<Props>()
</script>
`)
assertCode(content)
Expand Down
13 changes: 9 additions & 4 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -440,6 +440,12 @@ export function compileScript(
return true
}

function getAstBody(): Statement[] {
return scriptAst
? [...scriptSetupAst.body, ...scriptAst.body]
: scriptSetupAst.body
}

function resolveExtendsType(
node: Node,
qualifier: (node: Node) => boolean,
Expand All @@ -451,7 +457,8 @@ export function compileScript(
extend.type === 'TSExpressionWithTypeArguments' &&
extend.expression.type === 'Identifier'
) {
for (const node of scriptSetupAst.body) {
const body = getAstBody()
for (const node of body) {
const qualified = isQualifiedType(
node,
qualifier,
Expand Down Expand Up @@ -521,9 +528,7 @@ export function compileScript(
node.typeName.type === 'Identifier'
) {
const refName = node.typeName.name
const body = scriptAst
? [...scriptSetupAst.body, ...scriptAst.body]
: scriptSetupAst.body
const body = getAstBody()
for (const node of body) {
let qualified = isQualifiedType(
node,
Expand Down

0 comments on commit c8af08d

Please sign in to comment.