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,43 +247,45 @@ export class MetadataCollector {
// Otherwise don't record the function. // Otherwise don't record the function.
break; break;
case ts.SyntaxKind.EnumDeclaration: case ts.SyntaxKind.EnumDeclaration:
const enumDeclaration = <ts.EnumDeclaration>node; if (node.flags & ts.NodeFlags.Export) {
let enumValueHolder: {[name: string]: MetadataValue} = {}; const enumDeclaration = <ts.EnumDeclaration>node;
const enumName = enumDeclaration.name.text; let enumValueHolder: {[name: string]: MetadataValue} = {};
let nextDefaultValue: MetadataValue = 0; const enumName = enumDeclaration.name.text;
let writtenMembers = 0; let nextDefaultValue: MetadataValue = 0;
for (const member of enumDeclaration.members) { let writtenMembers = 0;
let enumValue: MetadataValue; for (const member of enumDeclaration.members) {
if (!member.initializer) { let enumValue: MetadataValue;
enumValue = nextDefaultValue; if (!member.initializer) {
} else { enumValue = nextDefaultValue;
enumValue = evaluator.evaluateNode(member.initializer); } else {
} enumValue = evaluator.evaluateNode(member.initializer);
let name: string = undefined;
if (member.name.kind == ts.SyntaxKind.Identifier) {
const identifier = <ts.Identifier>member.name;
name = identifier.text;
enumValueHolder[name] = enumValue;
writtenMembers++;
}
if (typeof enumValue === 'number') {
nextDefaultValue = enumValue + 1;
} else if (name) {
nextDefaultValue = {
__symbolic: 'binary',
operator: '+',
left: {
__symbolic: 'select',
expression: {__symbolic: 'reference', name: enumName}, name
}
} }
} else { let name: string = undefined;
nextDefaultValue = errorSym('Unsuppported enum member name', member.name); if (member.name.kind == ts.SyntaxKind.Identifier) {
}; const identifier = <ts.Identifier>member.name;
} name = identifier.text;
if (writtenMembers) { enumValueHolder[name] = enumValue;
if (!metadata) metadata = {}; writtenMembers++;
metadata[enumName] = enumValueHolder; }
if (typeof enumValue === 'number') {
nextDefaultValue = enumValue + 1;
} else if (name) {
nextDefaultValue = {
__symbolic: 'binary',
operator: '+',
left: {
__symbolic: 'select',
expression: {__symbolic: 'reference', name: enumName}, name
}
}
} else {
nextDefaultValue = errorSym('Unsuppported enum member name', member.name);
};
}
if (writtenMembers) {
if (!metadata) metadata = {};
metadata[enumName] = enumValueHolder;
}
} }
break; break;
case ts.SyntaxKind.VariableStatement: case ts.SyntaxKind.VariableStatement:

View File

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