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

Commit 3f7e8f7

Browse files
committed
Add support for typescript enums.
Enums can be typechecked now and also minified.
1 parent 1682c0b commit 3f7e8f7

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/sickle.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class Annotator {
150150
this.visitTypeAlias(<ts.TypeAliasDeclaration>node);
151151
this.writeNode(node);
152152
break;
153+
case ts.SyntaxKind.EnumDeclaration:
154+
this.visitEnum(<ts.EnumDeclaration>node);
155+
break;
153156
default:
154157
this.writeNode(node);
155158
break;
@@ -263,6 +266,24 @@ class Annotator {
263266
this.emit(': void;\n');
264267
}
265268

269+
private visitEnum(node: ts.EnumDeclaration) {
270+
this.emit('/** @typedef {number} */\n');
271+
this.writeNode(node);
272+
this.emit('\n');
273+
let i = 0;
274+
for (let member of node.members) {
275+
this.emit(`/** @type {${node.name.getText()}} */\n`);
276+
this.emit(`(<any>${node.name.getText()}).${member.name.getText()} = `);
277+
if (member.initializer) {
278+
this.visit(member.initializer);
279+
} else {
280+
this.emit(String(i));
281+
i++;
282+
}
283+
this.emit(';\n');
284+
}
285+
}
286+
266287
private writeNode(node: ts.Node, skipComments: boolean = false) {
267288
if (node.getChildCount() == 0) {
268289
// Directly write complete tokens.

test_files/enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
enum FooEnum {XYZ, PI = 3.14159}

test_files/es6/enum.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @typedef {number} */
2+
var FooEnum;
3+
(function (FooEnum) {
4+
FooEnum[FooEnum["XYZ"] = 0] = "XYZ";
5+
FooEnum[FooEnum["PI"] = 3.14159] = "PI";
6+
})(FooEnum || (FooEnum = {}));
7+
/** @type {FooEnum} */
8+
FooEnum.XYZ = 0;
9+
/** @type {FooEnum} */
10+
FooEnum.PI = 3.14159;

test_files/sickle/enum.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @typedef {number} */
2+
enum FooEnum {XYZ, PI = 3.14159}
3+
/** @type {FooEnum} */
4+
(<any>FooEnum).XYZ = 0;
5+
/** @type {FooEnum} */
6+
(<any>FooEnum).PI = 3.14159;

0 commit comments

Comments
 (0)