From ae528514588c235ec0c3466cad596ff83f3f522c Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Mon, 4 Sep 2017 21:01:01 +0200 Subject: [PATCH] fix(tsc-wrapped): add metadata for `type` declarations (#19040) PR Close #19040 --- tools/@angular/tsc-wrapped/src/collector.ts | 16 ++++++++++++---- .../@angular/tsc-wrapped/test/collector.spec.ts | 11 +++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tools/@angular/tsc-wrapped/src/collector.ts b/tools/@angular/tsc-wrapped/src/collector.ts index 7ec3183a99..79a808f80a 100644 --- a/tools/@angular/tsc-wrapped/src/collector.ts +++ b/tools/@angular/tsc-wrapped/src/collector.ts @@ -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 = 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 = 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. diff --git a/tools/@angular/tsc-wrapped/test/collector.spec.ts b/tools/@angular/tsc-wrapped/test/collector.spec.ts index a6ca662bf8..abc813eceb 100644 --- a/tools/@angular/tsc-wrapped/test/collector.spec.ts +++ b/tools/@angular/tsc-wrapped/test/collector.spec.ts @@ -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';