fix(compiler-sfc): handle imported types from default exports
close #8355
This commit is contained in:
parent
8b7c04b18f
commit
5aec717a24
|
@ -574,6 +574,47 @@ describe('resolveType', () => {
|
||||||
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
|
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)', () => {
|
test('relative (dynamic import)', () => {
|
||||||
const files = {
|
const files = {
|
||||||
'/foo.ts': `export type P = { foo: string, bar: import('./bar').N }`,
|
'/foo.ts': `export type P = { foo: string, bar: import('./bar').N }`,
|
||||||
|
|
|
@ -1144,6 +1144,18 @@ function recordTypes(
|
||||||
stmt.source.value
|
stmt.source.value
|
||||||
)
|
)
|
||||||
Object.assign(scope.exportedTypes, sourceScope.exportedTypes)
|
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]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1160,13 +1172,14 @@ function recordTypes(
|
||||||
function recordType(
|
function recordType(
|
||||||
node: Node,
|
node: Node,
|
||||||
types: Record<string, Node>,
|
types: Record<string, Node>,
|
||||||
declares: Record<string, Node>
|
declares: Record<string, Node>,
|
||||||
|
overwriteId?: string
|
||||||
) {
|
) {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'TSInterfaceDeclaration':
|
case 'TSInterfaceDeclaration':
|
||||||
case 'TSEnumDeclaration':
|
case 'TSEnumDeclaration':
|
||||||
case 'TSModuleDeclaration': {
|
case 'TSModuleDeclaration': {
|
||||||
const id = getId(node.id)
|
const id = overwriteId || getId(node.id)
|
||||||
let existing = types[id]
|
let existing = types[id]
|
||||||
if (existing) {
|
if (existing) {
|
||||||
if (node.type === 'TSModuleDeclaration') {
|
if (node.type === 'TSModuleDeclaration') {
|
||||||
|
@ -1199,7 +1212,7 @@ function recordType(
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'ClassDeclaration':
|
case 'ClassDeclaration':
|
||||||
types[getId(node.id)] = node
|
types[overwriteId || getId(node.id)] = node
|
||||||
break
|
break
|
||||||
case 'TSTypeAliasDeclaration':
|
case 'TSTypeAliasDeclaration':
|
||||||
types[node.id.name] = node.typeAnnotation
|
types[node.id.name] = node.typeAnnotation
|
||||||
|
|
Loading…
Reference in New Issue