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:
Alan Agius 2017-08-15 16:39:54 +02:00 committed by Miško Hevery
parent 7bfd850493
commit 6e3498ca8e
2 changed files with 28 additions and 5 deletions

View File

@ -287,13 +287,14 @@ export class MetadataCollector {
const isExportedIdentifier = (identifier?: ts.Identifier) => const isExportedIdentifier = (identifier?: ts.Identifier) =>
identifier && exportMap.has(identifier.text); identifier && exportMap.has(identifier.text);
const isExported = const isExported =
(node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration | (node: ts.FunctionDeclaration | ts.ClassDeclaration | ts.TypeAliasDeclaration |
ts.EnumDeclaration) => isExport(node) || isExportedIdentifier(node.name); ts.InterfaceDeclaration | ts.EnumDeclaration) =>
isExport(node) || isExportedIdentifier(node.name);
const exportedIdentifierName = (identifier?: ts.Identifier) => const exportedIdentifierName = (identifier?: ts.Identifier) =>
identifier && (exportMap.get(identifier.text) || identifier.text); identifier && (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.EnumDeclaration) => exportedIdentifierName(node.name); ts.TypeAliasDeclaration | ts.EnumDeclaration) => exportedIdentifierName(node.name);
// Predeclare classes and functions // Predeclare classes and functions
@ -390,6 +391,17 @@ 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)) {

View File

@ -34,6 +34,7 @@ describe('Collector', () => {
'exported-classes.ts', 'exported-classes.ts',
'exported-functions.ts', 'exported-functions.ts',
'exported-enum.ts', 'exported-enum.ts',
'exported-type.ts',
'exported-consts.ts', 'exported-consts.ts',
'local-symbol-ref.ts', 'local-symbol-ref.ts',
'local-function-ref.ts', 'local-function-ref.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);
@ -945,7 +953,7 @@ describe('Collector', () => {
it('should be able to substitute a lambda', () => { it('should be able to substitute a lambda', () => {
const source = createSource(` const source = createSource(`
const b = 1; const b = 1;
export const a = () => b; export const a = () => b;
`); `);
const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => { const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => {
if (node.kind === ts.SyntaxKind.ArrowFunction) { if (node.kind === ts.SyntaxKind.ArrowFunction) {
@ -965,7 +973,7 @@ describe('Collector', () => {
}); });
const source = createSource(` const source = createSource(`
const b = 1; const b = 1;
export const a = () => b; export const a = () => b;
`); `);
const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => { const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => {
if (node.kind === ts.SyntaxKind.ArrowFunction) { if (node.kind === ts.SyntaxKind.ArrowFunction) {
@ -1272,6 +1280,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';