From cfa716d6ed9961eb733a393d2b2446d730bbc2c2 Mon Sep 17 00:00:00 2001
From: Patrice Chalin <pchalin@gmail.com>
Date: Wed, 18 Mar 2015 15:14:26 -0700
Subject: [PATCH] Support enums

Fixes #10.
---
 main.ts          | 22 ++++++++++++++++++++++
 test/DartTest.ts | 19 +++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/main.ts b/main.ts
index 6bcd4b4..2c0021a 100644
--- a/main.ts
+++ b/main.ts
@@ -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) {
diff --git a/test/DartTest.ts b/test/DartTest.ts
index 4c3be36..edcd22c 100644
--- a/test/DartTest.ts
+++ b/test/DartTest.ts
@@ -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');
@@ -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', () => {
+      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; }')
+          .to.equal(' switch ( c ) { case Color . Red : break ; default : break ; }');
+    });
+  });
 
   describe('functions', () => {
     it('supports declarations',