Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Support enums #31

Merged
merged 1 commit into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,28 @@ class Translator {
this.visitClassLike(ifDecl);
break;

case ts.SyntaxKind.EnumDeclaration:
var decl = <ts.EnumDeclaration>node;
this.emit('enum');
this.visit(decl.name);
this.emit('{');
// Enums can be empty in TS ...
if (decl.members.length === 0) {
// ... but not in Dart.
this.reportError(node, 'empty enums are not supported');
}
this.visitList(decl.members);
this.emit('}');
break;

case ts.SyntaxKind.EnumMember:
var member = <ts.EnumMember>node;
this.visit(member.name);
if (member.initializer) {
this.reportError(node, 'enum initializers are not supported');
}
break;

case ts.SyntaxKind.HeritageClause:
var heritageClause = <ts.HeritageClause>node;
if (heritageClause.token === ts.SyntaxKind.ExtendsKeyword) {
Expand Down
19 changes: 19 additions & 0 deletions test/DartTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="../typings/chai/chai.d.ts"/>
/// <reference path="../typings/mocha/mocha.d.ts"/>
/// <reference path="../typings/source-map-support/source-map-support.d.ts"/>

import sms = require('source-map-support');
Expand Down Expand Up @@ -107,6 +108,24 @@ describe('transpile to dart', () => {
});
});

describe('enums', () => {
it('should support basic enum declaration', () => {
expectTranslate('enum Color { Red, Green, Blue }')
.to.equal(' enum Color { Red , Green , Blue }');
});
it('does not support empty enum', () => {
chai.expect(() => translateSource('enum Empty { }'))
.to.throw('empty enums are not supported');
});
it('does not support enum with initializer', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for const enums?

microsoft/TypeScript#1029

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we address that in a separate issue / PR? Support or not of const enums is IMHO debatable (in particular since there is no equivalent feature in Dart.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'm also good with just failing for const enums for the time being.

chai.expect(() => translateSource('enum Color { Red = 1, Green, Blue = 4 }'))
.to.throw('enum initializers are not supported');
});
it('should support switch over enum', () => {
expectTranslate('switch(c) { case Color.Red: break; default: break; }')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my information, this is not different in any kind from a plain switch over anything else, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. (This test just made me feel better that a case label could be written as, e.g., Color.Red. I didn't know for sure until I wrote the test.)

.to.equal(' switch ( c ) { case Color . Red : break ; default : break ; }');
});
});

describe('functions', () => {
it('supports declarations',
Expand Down