fix(compiler): Only emit metadata for exported enums (#10957)

Closes: #10955
This commit is contained in:
Chuck Jazdzewski 2016-08-22 10:26:22 -07:00 committed by Kara
parent ece7985b8a
commit a7b76826a0
2 changed files with 52 additions and 36 deletions

View File

@ -247,6 +247,7 @@ export class MetadataCollector {
// Otherwise don't record the function.
break;
case ts.SyntaxKind.EnumDeclaration:
if (node.flags & ts.NodeFlags.Export) {
const enumDeclaration = <ts.EnumDeclaration>node;
let enumValueHolder: {[name: string]: MetadataValue} = {};
const enumName = enumDeclaration.name.text;
@ -285,6 +286,7 @@ export class MetadataCollector {
if (!metadata) metadata = {};
metadata[enumName] = enumValueHolder;
}
}
break;
case ts.SyntaxKind.VariableStatement:
const variableStatement = <ts.VariableStatement>node;

View File

@ -25,6 +25,7 @@ describe('Collector', () => {
'exported-enum.ts',
'exported-consts.ts',
'local-symbol-ref.ts',
'private-enum.ts',
're-exports.ts',
'static-field-reference.ts',
'static-method.ts',
@ -339,6 +340,15 @@ describe('Collector', () => {
expect(someEnum).toEqual({A: 0, B: 1, C: 100, D: 101});
});
it('should ignore a non-export enum', () => {
let enumSource = program.getSourceFile('/private-enum.ts');
let metadata = collector.getMetadata(enumSource);
let publicEnum: any = metadata.metadata['PublicEnum'];
let privateEnum: any = metadata.metadata['PrivateEnum'];
expect(publicEnum).toEqual({a: 0, b: 1, c: 2});
expect(privateEnum).toBeUndefined();
});
it('should be able to collect enums initialized from consts', () => {
let enumSource = program.getSourceFile('/exported-enum.ts');
let metadata = collector.getMetadata(enumSource);
@ -838,6 +848,10 @@ const FILES: Directory = {
})
export class SomeComponent {}
`,
'private-enum.ts': `
export enum PublicEnum { a, b, c }
enum PrivateEnum { e, f, g }
`,
'node_modules': {
'angular2': {
'core.d.ts': `