Skip to content

Commit 4ac2bbf

Browse files
committed
Fixes and support for unions.
1 parent a05c64d commit 4ac2bbf

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

TypeScriptDefinitionToolkit.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export module Defs {
2727
CLASS_DECLARATION = 16,
2828
AMBIENT_VARIABLE = 17,
2929
ENUM = 18,
30-
ENUM_MEMBER = 19
30+
ENUM_MEMBER = 19,
31+
UNION_TYPE = 20
3132
}
3233

3334
export interface Base {
@@ -97,6 +98,10 @@ export module Defs {
9798
members: PrimaryType[];
9899
}
99100

101+
export interface UnionType extends PrimaryType {
102+
members: PrimaryType[];
103+
}
104+
100105
// FIXME add an array type??
101106
// -- end types.
102107

@@ -282,6 +287,11 @@ export function toString(obj: Defs.Base, level: number=0, indent: string = "
282287
return "[" + tuple.members.map( (t) => toString(t, level+1, indent) ).join(", ") + "]";
283288
break;
284289

290+
case Defs.Type.UNION_TYPE:
291+
let union = <Defs.UnionType> obj;
292+
return union.members.map( (m) => toString(m) ).join("|");
293+
break;
294+
285295
case Defs.Type.EXPORT_ASSIGNMENT:
286296
let exportAssign = <Defs.ExportAssignment> obj;
287297
return dent + "export = " + exportAssign.name + ";\n";

test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ export function testTypeAlias(test: nodeunit.Test): void {
298298
roundTrip(test, `type foo = bar;`);
299299
}
300300

301+
export function testTypeAlias2(test: nodeunit.Test): void {
302+
roundTrip(test, `type strings = string|string[];`);
303+
}
304+
301305
export function testImportDeclaration(test: nodeunit.Test): void {
302306
roundTrip(test, `import foo = bar;`);
303307
}

typescript_definition.pegjs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var CLASS_DECLARATION = 16;
2121
var AMBIENT_VARIABLE = 17;
2222
var ENUM = 18;
2323
var ENUM_MEMBER = 19;
24+
var UNION_TYPE = 20;
2425

2526
}
2627

@@ -320,8 +321,8 @@ type
320321
/ constructor_type
321322

322323
primary_or_union_type
323-
= primary_type
324-
/ union_type
324+
= union_type
325+
/ primary_type
325326

326327
primary_type
327328
= name:parenthesized_type _ array:array_square !(_ ARROW) /* A parenthesized looks very similar to the start of function signature */
@@ -466,7 +467,17 @@ tuple_element_types
466467
}
467468

468469
union_type
469-
= primary_type PIPE primary_or_union_type
470+
= head:primary_type PIPE rest:primary_or_union_type
471+
{
472+
var members = [];
473+
members.push(head);
474+
if (rest.type === UNION_TYPE) {
475+
members = members.concat(rest.members);
476+
} else {
477+
members.push(rest);
478+
}
479+
return {type: UNION_TYPE, members: members };
480+
}
470481

471482
function_type
472483
= typeParameters:type_parameters? _ LBRACKET _ parameterList:parameter_list? _ RBRACKET _ ARROW _ type:type

0 commit comments

Comments
 (0)