fix(tsc-wrapped): add metadata for type declarations (#19040)

PR Close #19040
This commit is contained in:
Alan Agius 2017-09-04 21:01:01 +02:00 committed by Igor Minar
parent e63cf3b89e
commit ae52851458
2 changed files with 23 additions and 4 deletions

View File

@ -265,12 +265,12 @@ export class MetadataCollector {
const isExportedIdentifier = (identifier: ts.Identifier) => exportMap.has(identifier.text);
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);
const exportedIdentifierName = (identifier: ts.Identifier) =>
exportMap.get(identifier.text) || identifier.text;
const exportedName =
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration |
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration | ts.TypeAliasDeclaration |
ts.EnumDeclaration) => exportedIdentifierName(node.name);
@ -364,7 +364,16 @@ 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)) {
@ -372,7 +381,6 @@ export class MetadataCollector {
metadata[exportedName(interfaceDeclaration)] = {__symbolic: 'interface'};
}
break;
case ts.SyntaxKind.FunctionDeclaration:
// Record functions that return a single value. Record the parameter
// names substitution will be performed by the StaticReflector.

View File

@ -35,6 +35,7 @@ describe('Collector', () => {
'exported-functions.ts',
'exported-enum.ts',
'exported-consts.ts',
'exported-type.ts',
'local-symbol-ref.ts',
'local-function-ref.ts',
'local-symbol-ref-func.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);
@ -1234,6 +1242,9 @@ const FILES: Directory = {
}
export declare function declaredFn();
`,
'exported-type.ts': `
export type SomeType = 'a' | 'b';
`,
'exported-enum.ts': `
import {constValue} from './exported-consts';