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

Commit

Permalink
feat(): Add support for void generic argument, which translates to dy…
Browse files Browse the repository at this point in the history
…namic.
  • Loading branch information
hansl committed Feb 3, 2016
1 parent f03eeb8 commit 0df59c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ts from 'typescript';
import {Transpiler} from './main';

export type ClassLike = ts.ClassDeclaration | ts.InterfaceDeclaration;
export type VisitorFn = (node: ts.Node) => void;

export function ident(n: ts.Node): string {
if (n.kind === ts.SyntaxKind.Identifier) return (<ts.Identifier>n).text;
Expand Down Expand Up @@ -29,9 +30,10 @@ export class TranspilerBase {
if (nodes) this.visitEach(nodes);
}

visitList(nodes: ts.Node[], separator: string = ',') {
visitList(
nodes: ts.Node[], separator: string = ',', handler: VisitorFn = node => this.visit(node)) {
for (var i = 0; i < nodes.length; i++) {
this.visit(nodes[i]);
handler.call(null, nodes[i]);
if (i < nodes.length - 1) this.emit(separator);
}
}
Expand Down Expand Up @@ -76,7 +78,13 @@ export class TranspilerBase {
maybeVisitTypeArguments(n: {typeArguments?: ts.NodeArray<ts.TypeNode>}) {
if (n.typeArguments) {
this.emit('<');
this.visitList(n.typeArguments);
this.visitList(n.typeArguments, ',', (node) => {
if (node.kind == ts.SyntaxKind.VoidKeyword) {
this.emit('dynamic');
} else {
this.visit(node);
}
});
this.emit('>');
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ describe('type arguments', () => {
it('should support use', () => {
expectTranslate('class X extends Y<A, B> { }').to.equal('class X extends Y<A, B> {}');
});
it('should rename void generic arguments to objects', () => {
expectTranslate('var x: X<number, void>;').to.equal('X<num, dynamic> x;');
expectTranslate('class X extends Y<void> { }').to.equal('class X extends Y<dynamic> {}');
});
});

0 comments on commit 0df59c0

Please sign in to comment.