fix(tsc-wrapped): add metadata for `type` declarations (#18704)
Closes #18675 test(tsc-wrapped): fix collector tests refactor(tsc-wrapped): change `__symbolic` to `interface` for `TypeAliasDeclaration` tsc-wrapped: reword test PR Close #18704
This commit is contained in:
parent
7bfd850493
commit
6e3498ca8e
|
@ -287,13 +287,14 @@ export class MetadataCollector {
|
|||
const isExportedIdentifier = (identifier?: ts.Identifier) =>
|
||||
identifier && exportMap.has(identifier.text);
|
||||
const isExported =
|
||||
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration |
|
||||
ts.EnumDeclaration) => isExport(node) || isExportedIdentifier(node.name);
|
||||
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.TypeAliasDeclaration |
|
||||
ts.InterfaceDeclaration | ts.EnumDeclaration) =>
|
||||
isExport(node) || isExportedIdentifier(node.name);
|
||||
const exportedIdentifierName = (identifier?: ts.Identifier) =>
|
||||
identifier && (exportMap.get(identifier.text) || identifier.text);
|
||||
const exportedName =
|
||||
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration |
|
||||
ts.EnumDeclaration) => exportedIdentifierName(node.name);
|
||||
ts.TypeAliasDeclaration | ts.EnumDeclaration) => exportedIdentifierName(node.name);
|
||||
|
||||
|
||||
// Predeclare classes and functions
|
||||
|
@ -390,6 +391,17 @@ export class MetadataCollector {
|
|||
// Otherwise don't record metadata for the class.
|
||||
break;
|
||||
|
||||
case ts.SyntaxKind.TypeAliasDeclaration:
|
||||
const typeDeclaration = <ts.TypeAliasDeclaration>node;
|
||||
if (typeDeclaration.name && isExported(typeDeclaration)) {
|
||||
const name = exportedName(typeDeclaration);
|
||||
if (name) {
|
||||
if (!metadata) metadata = {};
|
||||
metadata[name] = {__symbolic: 'interface'};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ts.SyntaxKind.InterfaceDeclaration:
|
||||
const interfaceDeclaration = <ts.InterfaceDeclaration>node;
|
||||
if (interfaceDeclaration.name && isExported(interfaceDeclaration)) {
|
||||
|
|
|
@ -34,6 +34,7 @@ describe('Collector', () => {
|
|||
'exported-classes.ts',
|
||||
'exported-functions.ts',
|
||||
'exported-enum.ts',
|
||||
'exported-type.ts',
|
||||
'exported-consts.ts',
|
||||
'local-symbol-ref.ts',
|
||||
'local-function-ref.ts',
|
||||
|
@ -66,6 +67,13 @@ describe('Collector', () => {
|
|||
expect(metadata).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return an interface reference for types', () => {
|
||||
const sourceFile = program.getSourceFile('/exported-type.ts');
|
||||
const metadata = collector.getMetadata(sourceFile);
|
||||
expect(metadata).toEqual(
|
||||
{__symbolic: 'module', version: 3, metadata: {SomeType: {__symbolic: 'interface'}}});
|
||||
});
|
||||
|
||||
it('should return an interface reference for interfaces', () => {
|
||||
const sourceFile = program.getSourceFile('app/hero.ts');
|
||||
const metadata = collector.getMetadata(sourceFile);
|
||||
|
@ -1272,6 +1280,9 @@ const FILES: Directory = {
|
|||
}
|
||||
export declare function declaredFn();
|
||||
`,
|
||||
'exported-type.ts': `
|
||||
export type SomeType = 'a' | 'b';
|
||||
`,
|
||||
'exported-enum.ts': `
|
||||
import {constValue} from './exported-consts';
|
||||
|
||||
|
|
Loading…
Reference in New Issue