refactor(tsc-wrapped): collect all exported functions and classes and bump metadata version from 1 to 2

This is needed to resolve symbols without `.d.ts` files.
This bumps the version of the metadata from 1 to 2.
This adds logic into `ng_host.ts` to automatically upgrade
version 1 to version 2 metadata by adding the exported symbols
from the `.d.ts` file.
This commit is contained in:
Tobias Bosch
2016-11-17 09:52:38 -08:00
committed by Chuck Jazdzewski
parent 2fe6fb1163
commit 39a71eb0ec
9 changed files with 169 additions and 92 deletions

View File

@ -256,9 +256,11 @@ export class MetadataCollector {
if (classDeclaration.name) {
const className = classDeclaration.name.text;
if (node.flags & ts.NodeFlags.Export) {
if (!metadata) metadata = {};
if (classDeclaration.decorators) {
if (!metadata) metadata = {};
metadata[className] = classMetadataOf(classDeclaration);
} else {
metadata[className] = {__symbolic: 'class'};
}
}
}
@ -269,10 +271,14 @@ export class MetadataCollector {
// names substitution will be performed by the StaticReflector.
const functionDeclaration = <ts.FunctionDeclaration>node;
if (node.flags & ts.NodeFlags.Export) {
if (!metadata) metadata = {};
const maybeFunc = maybeGetSimpleFunction(functionDeclaration);
if (maybeFunc) {
if (!metadata) metadata = {};
metadata[maybeFunc.name] = recordEntry(maybeFunc.func, node);
} else if (functionDeclaration.name.kind == ts.SyntaxKind.Identifier) {
const nameNode = <ts.Identifier>functionDeclaration.name;
const functionName = nameNode.text;
metadata[functionName] = {__symbolic: 'function'};
}
}
break;

View File

@ -15,7 +15,7 @@
// semantics of the file in an array. For example, when generating a version 2 file, if version 1
// can accurately represent the metadata, generate both version 1 and version 2 in an array.
export const VERSION = 1;
export const VERSION = 2;
export type MetadataEntry = ClassMetadata | FunctionMetadata | MetadataValue;

View File

@ -29,6 +29,7 @@ describe('Collector', () => {
'/unsupported-1.ts',
'/unsupported-2.ts',
'import-star.ts',
'exported-classes.ts',
'exported-functions.ts',
'exported-enum.ts',
'exported-consts.ts',
@ -62,7 +63,7 @@ describe('Collector', () => {
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
version: 2,
metadata: {
HeroDetailComponent: {
__symbolic: 'class',
@ -103,7 +104,7 @@ describe('Collector', () => {
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
version: 2,
metadata: {
AppComponent: {
__symbolic: 'class',
@ -157,7 +158,7 @@ describe('Collector', () => {
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
version: 2,
metadata: {
HEROES: [
{'id': 11, 'name': 'Mr. Nice'}, {'id': 12, 'name': 'Narco'},
@ -170,13 +171,6 @@ describe('Collector', () => {
});
});
it('should return undefined for modules that have no metadata', () => {
const sourceFile = program.getSourceFile('/app/error-cases.ts');
expect(sourceFile).toBeTruthy(sourceFile);
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toBeUndefined();
});
let casesFile: ts.SourceFile;
let casesMetadata: ModuleMetadata;
@ -238,7 +232,7 @@ describe('Collector', () => {
const metadata = collector.getMetadata(unsupported1);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
version: 2,
metadata: {
a: {__symbolic: 'error', message: 'Destructuring not supported', line: 1, character: 16},
b: {__symbolic: 'error', message: 'Destructuring not supported', line: 1, character: 19},
@ -275,12 +269,26 @@ describe('Collector', () => {
]);
});
it('should record all exported classes', () => {
const sourceFile = program.getSourceFile('/exported-classes.ts');
const metadata = collector.getMetadata(sourceFile);
expect(metadata).toEqual({
__symbolic: 'module',
version: 2,
metadata: {
SimpleClass: {__symbolic: 'class'},
AbstractClass: {__symbolic: 'class'},
DeclaredClass: {__symbolic: 'class'}
}
});
});
it('should be able to record functions', () => {
const exportedFunctions = program.getSourceFile('/exported-functions.ts');
const metadata = collector.getMetadata(exportedFunctions);
expect(metadata).toEqual({
__symbolic: 'module',
version: 1,
version: 2,
metadata: {
one: {
__symbolic: 'function',
@ -328,7 +336,9 @@ describe('Collector', () => {
}
}
}
}
},
complexFn: {__symbolic: 'function'},
declaredFn: {__symbolic: 'function'}
}
});
});
@ -829,6 +839,11 @@ const FILES: Directory = {
constructor(private f: common.NgFor) {}
}
`,
'exported-classes.ts': `
export class SimpleClass {}
export abstract class AbstractClass {}
export declare class DeclaredClass {}
`,
'exported-functions.ts': `
export function one(a: string, b: string, c: string) {
return {a: a, b: b, c: c};
@ -842,6 +857,14 @@ const FILES: Directory = {
export function supportsState(): boolean {
return !!window.history.pushState;
}
export function complexFn(x: any): boolean {
if (x) {
return true;
} else {
return false;
}
}
export declare function declaredFn();
`,
'exported-enum.ts': `
import {constValue} from './exported-consts';