Skip to content

Commit

Permalink
fix(compiler-sfc): handle imported types from default exports
Browse files Browse the repository at this point in the history
close #8355
  • Loading branch information
yyx990803 committed May 18, 2023
1 parent 8b7c04b commit 5aec717
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,47 @@ describe('resolveType', () => {
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('relative (default export)', () => {
const files = {
'/foo.ts': `export default interface P { foo: string }`,
'/bar.ts': `type X = { bar: string }; export default X`
}
const { props, deps } = resolve(
`
import P from './foo'
import X from './bar'
defineProps<P & X>()
`,
files
)
expect(props).toStrictEqual({
foo: ['String'],
bar: ['String']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('relative (default re-export)', () => {
const files = {
'/bar.ts': `export { default } from './foo'`,
'/foo.ts': `export default interface P { foo: string }; export interface PP { bar: number }`,
'/baz.ts': `export { PP as default } from './foo'`
}
const { props, deps } = resolve(
`
import P from './bar'
import PP from './baz'
defineProps<P & PP>()
`,
files
)
expect(props).toStrictEqual({
foo: ['String'],
bar: ['Number']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('relative (dynamic import)', () => {
const files = {
'/foo.ts': `export type P = { foo: string, bar: import('./bar').N }`,
Expand Down
19 changes: 16 additions & 3 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,18 @@ function recordTypes(
stmt.source.value
)
Object.assign(scope.exportedTypes, sourceScope.exportedTypes)
} else if (stmt.type === 'ExportDefaultDeclaration' && stmt.declaration) {
if (stmt.declaration.type !== 'Identifier') {
recordType(stmt.declaration, types, declares, 'default')
recordType(
stmt.declaration,
exportedTypes,
exportedDeclares,
'default'
)
} else if (types[stmt.declaration.name]) {
exportedTypes['default'] = types[stmt.declaration.name]
}
}
}
}
Expand All @@ -1160,13 +1172,14 @@ function recordTypes(
function recordType(
node: Node,
types: Record<string, Node>,
declares: Record<string, Node>
declares: Record<string, Node>,
overwriteId?: string
) {
switch (node.type) {
case 'TSInterfaceDeclaration':
case 'TSEnumDeclaration':
case 'TSModuleDeclaration': {
const id = getId(node.id)
const id = overwriteId || getId(node.id)
let existing = types[id]
if (existing) {
if (node.type === 'TSModuleDeclaration') {
Expand Down Expand Up @@ -1199,7 +1212,7 @@ function recordType(
break
}
case 'ClassDeclaration':
types[getId(node.id)] = node
types[overwriteId || getId(node.id)] = node
break
case 'TSTypeAliasDeclaration':
types[node.id.name] = node.typeAnnotation
Expand Down

0 comments on commit 5aec717

Please sign in to comment.