fix(tsc-wrapped): add metadata for type
declarations (#19040)
PR Close #19040
This commit is contained in:
parent
e63cf3b89e
commit
ae52851458
@ -265,12 +265,12 @@ export class MetadataCollector {
|
|||||||
|
|
||||||
const isExportedIdentifier = (identifier: ts.Identifier) => exportMap.has(identifier.text);
|
const isExportedIdentifier = (identifier: ts.Identifier) => exportMap.has(identifier.text);
|
||||||
const isExported =
|
const isExported =
|
||||||
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration |
|
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration | ts.TypeAliasDeclaration |
|
||||||
ts.EnumDeclaration) => isExport(node) || isExportedIdentifier(node.name);
|
ts.EnumDeclaration) => isExport(node) || isExportedIdentifier(node.name);
|
||||||
const exportedIdentifierName = (identifier: ts.Identifier) =>
|
const exportedIdentifierName = (identifier: ts.Identifier) =>
|
||||||
exportMap.get(identifier.text) || identifier.text;
|
exportMap.get(identifier.text) || identifier.text;
|
||||||
const exportedName =
|
const exportedName =
|
||||||
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration |
|
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration | ts.TypeAliasDeclaration |
|
||||||
ts.EnumDeclaration) => exportedIdentifierName(node.name);
|
ts.EnumDeclaration) => exportedIdentifierName(node.name);
|
||||||
|
|
||||||
|
|
||||||
@ -364,7 +364,16 @@ export class MetadataCollector {
|
|||||||
}
|
}
|
||||||
// Otherwise don't record metadata for the class.
|
// Otherwise don't record metadata for the class.
|
||||||
break;
|
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:
|
case ts.SyntaxKind.InterfaceDeclaration:
|
||||||
const interfaceDeclaration = <ts.InterfaceDeclaration>node;
|
const interfaceDeclaration = <ts.InterfaceDeclaration>node;
|
||||||
if (interfaceDeclaration.name && isExported(interfaceDeclaration)) {
|
if (interfaceDeclaration.name && isExported(interfaceDeclaration)) {
|
||||||
@ -372,7 +381,6 @@ export class MetadataCollector {
|
|||||||
metadata[exportedName(interfaceDeclaration)] = {__symbolic: 'interface'};
|
metadata[exportedName(interfaceDeclaration)] = {__symbolic: 'interface'};
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ts.SyntaxKind.FunctionDeclaration:
|
case ts.SyntaxKind.FunctionDeclaration:
|
||||||
// Record functions that return a single value. Record the parameter
|
// Record functions that return a single value. Record the parameter
|
||||||
// names substitution will be performed by the StaticReflector.
|
// names substitution will be performed by the StaticReflector.
|
||||||
|
@ -35,6 +35,7 @@ describe('Collector', () => {
|
|||||||
'exported-functions.ts',
|
'exported-functions.ts',
|
||||||
'exported-enum.ts',
|
'exported-enum.ts',
|
||||||
'exported-consts.ts',
|
'exported-consts.ts',
|
||||||
|
'exported-type.ts',
|
||||||
'local-symbol-ref.ts',
|
'local-symbol-ref.ts',
|
||||||
'local-function-ref.ts',
|
'local-function-ref.ts',
|
||||||
'local-symbol-ref-func.ts',
|
'local-symbol-ref-func.ts',
|
||||||
@ -66,6 +67,13 @@ describe('Collector', () => {
|
|||||||
expect(metadata).toBeUndefined();
|
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', () => {
|
it('should return an interface reference for interfaces', () => {
|
||||||
const sourceFile = program.getSourceFile('app/hero.ts');
|
const sourceFile = program.getSourceFile('app/hero.ts');
|
||||||
const metadata = collector.getMetadata(sourceFile);
|
const metadata = collector.getMetadata(sourceFile);
|
||||||
@ -1234,6 +1242,9 @@ const FILES: Directory = {
|
|||||||
}
|
}
|
||||||
export declare function declaredFn();
|
export declare function declaredFn();
|
||||||
`,
|
`,
|
||||||
|
'exported-type.ts': `
|
||||||
|
export type SomeType = 'a' | 'b';
|
||||||
|
`,
|
||||||
'exported-enum.ts': `
|
'exported-enum.ts': `
|
||||||
import {constValue} from './exported-consts';
|
import {constValue} from './exported-consts';
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user