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

Commit 2c35367

Browse files
committed
Merge pull request #31 from chalin/chalin-0318-enum
Support enums.
2 parents 2f11473 + cfa716d commit 2c35367

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: main.ts

+22
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,28 @@ class Translator {
345345
this.visitClassLike(ifDecl);
346346
break;
347347

348+
case ts.SyntaxKind.EnumDeclaration:
349+
var decl = <ts.EnumDeclaration>node;
350+
this.emit('enum');
351+
this.visit(decl.name);
352+
this.emit('{');
353+
// Enums can be empty in TS ...
354+
if (decl.members.length === 0) {
355+
// ... but not in Dart.
356+
this.reportError(node, 'empty enums are not supported');
357+
}
358+
this.visitList(decl.members);
359+
this.emit('}');
360+
break;
361+
362+
case ts.SyntaxKind.EnumMember:
363+
var member = <ts.EnumMember>node;
364+
this.visit(member.name);
365+
if (member.initializer) {
366+
this.reportError(node, 'enum initializers are not supported');
367+
}
368+
break;
369+
348370
case ts.SyntaxKind.HeritageClause:
349371
var heritageClause = <ts.HeritageClause>node;
350372
if (heritageClause.token === ts.SyntaxKind.ExtendsKeyword) {

Diff for: test/DartTest.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="../typings/chai/chai.d.ts"/>
2+
/// <reference path="../typings/mocha/mocha.d.ts"/>
23
/// <reference path="../typings/source-map-support/source-map-support.d.ts"/>
34

45
import sms = require('source-map-support');
@@ -110,6 +111,24 @@ describe('transpile to dart', () => {
110111
});
111112
});
112113

114+
describe('enums', () => {
115+
it('should support basic enum declaration', () => {
116+
expectTranslate('enum Color { Red, Green, Blue }')
117+
.to.equal(' enum Color { Red , Green , Blue }');
118+
});
119+
it('does not support empty enum', () => {
120+
chai.expect(() => translateSource('enum Empty { }'))
121+
.to.throw('empty enums are not supported');
122+
});
123+
it('does not support enum with initializer', () => {
124+
chai.expect(() => translateSource('enum Color { Red = 1, Green, Blue = 4 }'))
125+
.to.throw('enum initializers are not supported');
126+
});
127+
it('should support switch over enum', () => {
128+
expectTranslate('switch(c) { case Color.Red: break; default: break; }')
129+
.to.equal(' switch ( c ) { case Color . Red : break ; default : break ; }');
130+
});
131+
});
113132

114133
describe('functions', () => {
115134
it('supports declarations',

0 commit comments

Comments
 (0)