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.
break;
case ts.SyntaxKind.EnumDeclaration:
const enumDeclaration = <ts.EnumDeclaration>node;
let enumValueHolder: {[name: string]: MetadataValue} = {};
const enumName = enumDeclaration.name.text;
let nextDefaultValue: MetadataValue = 0;
let writtenMembers = 0;
for (const member of enumDeclaration.members) {
let enumValue: MetadataValue;
if (!member.initializer) {
enumValue = nextDefaultValue;
} 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
}
if (node.flags & ts.NodeFlags.Export) {
const enumDeclaration = <ts.EnumDeclaration>node;
let enumValueHolder: {[name: string]: MetadataValue} = {};
const enumName = enumDeclaration.name.text;
let nextDefaultValue: MetadataValue = 0;
let writtenMembers = 0;
for (const member of enumDeclaration.members) {
let enumValue: MetadataValue;
if (!member.initializer) {
enumValue = nextDefaultValue;
} else {
enumValue = evaluator.evaluateNode(member.initializer);
}
} else {
nextDefaultValue = errorSym('Unsuppported enum member name', member.name);
};
}
if (writtenMembers) {
if (!metadata) metadata = {};
metadata[enumName] = enumValueHolder;
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 {
nextDefaultValue = errorSym('Unsuppported enum member name', member.name);
};
}
if (writtenMembers) {
if (!metadata) metadata = {};
metadata[enumName] = enumValueHolder;
}
}
break;
case ts.SyntaxKind.VariableStatement:

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': `