Skip to content

Commit 930309b

Browse files
authored
Alias types (#11)
Added aliases types. Allow for zero as enum values. Added aliases, enums, and unions to pascal case type name rule.
1 parent 7b4c843 commit 930309b

9 files changed

+228
-20
lines changed

src/ast/definitions.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ export interface TypeSystemDefinition {
3333
}
3434

3535
export class NamespaceDefinition extends AbstractNode {
36-
description?: StringValue;
3736
name: Name;
37+
description?: StringValue;
3838
annotations: Annotation[];
3939

4040
constructor(
4141
loc: Location | undefined,
42-
desc: StringValue | undefined,
4342
name: Name,
43+
desc: StringValue | undefined,
4444
annotations?: Annotation[]
4545
) {
4646
super(Kind.NamespaceDefinition, loc);
@@ -62,6 +62,39 @@ export class NamespaceDefinition extends AbstractNode {
6262
}
6363
}
6464

65+
export class AliasDefinition extends AbstractNode {
66+
name: Name;
67+
description?: StringValue;
68+
type: Type;
69+
annotations: Annotation[];
70+
71+
constructor(
72+
loc: Location | undefined,
73+
name: Name,
74+
desc: StringValue | undefined,
75+
type: Type,
76+
annotations?: Annotation[]
77+
) {
78+
super(Kind.AliasDefinition, loc);
79+
this.name = name;
80+
this.description = desc;
81+
this.type = type;
82+
this.annotations = annotations || [];
83+
}
84+
85+
annotation(
86+
name: string,
87+
callback?: (annotation: Annotation) => void
88+
): Annotation | undefined {
89+
return getAnnotation(name, this.annotations, callback);
90+
}
91+
92+
public accept(context: Context, visitor: Visitor): void {
93+
visitor.visitAlias(context);
94+
visitAnnotations(context, visitor, this.annotations);
95+
}
96+
}
97+
6598
export class ImportDefinition extends AbstractNode {
6699
description?: StringValue;
67100
all: boolean;

src/ast/document.ts

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export class Document extends AbstractNode {
3030
});
3131
visitor.visitDirectivesAfter(context);
3232

33+
visitor.visitAliasesBefore(context);
34+
context.aliases.map((alias) => {
35+
alias.accept(context.clone({ alias: alias }), visitor);
36+
});
37+
visitor.visitAliasesAfter(context);
38+
3339
visitor.visitAllOperationsBefore(context);
3440
context.interface.accept(context, visitor);
3541

src/ast/kinds.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export enum Kind {
2727
// Definitions
2828
NamespaceDefinition = "NamespaceDefinition",
2929
ImportDefinition = "ImportDefinition",
30+
AliasDefinition = "AliasDefinition",
3031
InterfaceDefinition = "InterfaceDefinition",
3132
RoleDefinition = "RoleDefinition",
3233
OperationDefinition = "OperationDefinition",

src/ast/visitor.ts

+96-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
EnumValueDefinition,
1212
DirectiveDefinition,
1313
ImportDefinition,
14+
AliasDefinition,
1415
} from "./definitions";
1516
import { Document } from "./document";
1617
import { Annotation, Name } from "./nodes";
@@ -33,6 +34,9 @@ export class Writer {
3334
export type ObjectMap<T = any> = { [key: string]: T };
3435

3536
interface NamedParameters {
37+
importDef?: ImportDefinition;
38+
directive?: DirectiveDefinition;
39+
alias?: AliasDefinition;
3640
role?: RoleDefinition;
3741
type?: TypeDefinition;
3842
operations?: OperationDefinition[];
@@ -47,8 +51,6 @@ interface NamedParameters {
4751
enumValues?: EnumValueDefinition[];
4852
enumValue?: EnumValueDefinition;
4953
union?: UnionDefinition;
50-
directive?: DirectiveDefinition;
51-
importDef?: ImportDefinition;
5254
annotation?: Annotation;
5355
}
5456

@@ -73,16 +75,21 @@ export class Context {
7375
imports: ImportDefinition[];
7476
directives: DirectiveDefinition[];
7577
directiveMap: Map<string, DirectiveDefinition>;
78+
aliases: AliasDefinition[];
7679
interface: InterfaceDefinition;
7780
roles: RoleDefinition[];
7881
types: TypeDefinition[];
7982
enums: EnumDefinition[];
8083
unions: UnionDefinition[];
81-
allTypes: Map<string, TypeDefinition | EnumDefinition | UnionDefinition>;
84+
allTypes: Map<
85+
string,
86+
TypeDefinition | EnumDefinition | UnionDefinition | AliasDefinition
87+
>;
8288

8389
// Drill-down definitions
8490
importDef?: ImportDefinition;
8591
directive?: DirectiveDefinition;
92+
alias?: AliasDefinition;
8693
role?: RoleDefinition;
8794
type?: TypeDefinition;
8895
operations?: OperationDefinition[];
@@ -112,6 +119,7 @@ export class Context {
112119
this.imports = other.imports;
113120
this.directives = other.directives;
114121
this.directiveMap = other.directiveMap;
122+
this.aliases = other.aliases;
115123
this.interface = other.interface;
116124
this.roles = other.roles;
117125
this.enums = other.enums;
@@ -125,12 +133,13 @@ export class Context {
125133
} else {
126134
this.namespace = new NamespaceDefinition(
127135
undefined,
128-
undefined,
129-
new Name(undefined, "")
136+
new Name(undefined, ""),
137+
undefined
130138
);
131139
this.directives = new Array<DirectiveDefinition>();
132140
this.directiveMap = new Map<string, DirectiveDefinition>();
133141
this.imports = new Array<ImportDefinition>();
142+
this.aliases = new Array<AliasDefinition>();
134143
this.interface = new InterfaceDefinition();
135144
this.roles = new Array<RoleDefinition>();
136145
this.enums = new Array<EnumDefinition>();
@@ -154,6 +163,9 @@ export class Context {
154163
}
155164

156165
clone({
166+
importDef,
167+
directive,
168+
alias,
157169
role,
158170
type,
159171
operations,
@@ -168,14 +180,13 @@ export class Context {
168180
enumValues,
169181
enumValue,
170182
union,
171-
directive,
172-
importDef,
173183
annotation,
174184
}: NamedParameters): Context {
175185
var context = new Context(this.config, undefined, this);
176186

177187
context.importDef = importDef || this.importDef;
178188
context.directive = directive || this.directive;
189+
context.alias = alias || this.alias;
179190
context.role = role || this.role;
180191
context.type = type || this.type;
181192
context.operations = operations || this.operations;
@@ -209,6 +220,11 @@ export class Context {
209220
case Kind.ImportDefinition:
210221
this.imports.push(value as ImportDefinition);
211222
break;
223+
case Kind.AliasDefinition:
224+
const alias = value as AliasDefinition;
225+
this.aliases.push(alias);
226+
this.allTypes.set(alias.name.value, alias);
227+
break;
212228
case Kind.InterfaceDefinition:
213229
this.interface = value as InterfaceDefinition;
214230
break;
@@ -261,6 +277,12 @@ export interface Visitor {
261277
visitDirectiveAfter(context: Context): void;
262278
visitDirectivesAfter(context: Context): void;
263279

280+
visitAliasesBefore(context: Context): void;
281+
visitAliasBefore(context: Context): void;
282+
visitAlias(context: Context): void;
283+
visitAliasAfter(context: Context): void;
284+
visitAliasesAfter(context: Context): void;
285+
264286
visitAllOperationsBefore(context: Context): void;
265287
visitInterfaceBefore(context: Context): void;
266288
visitInterface(context: Context): void;
@@ -419,6 +441,37 @@ export abstract class AbstractVisitor implements Visitor {
419441
this.triggerCallbacks(context, "DirectivesAfter");
420442
}
421443

444+
public visitAliasesBefore(context: Context): void {
445+
this.triggerAliasesBefore(context);
446+
}
447+
public triggerAliasesBefore(context: Context): void {
448+
this.triggerCallbacks(context, "AliasesBefore");
449+
}
450+
public visitAliasBefore(context: Context): void {
451+
this.triggerAliasBefore(context);
452+
}
453+
public triggerAliasBefore(context: Context): void {
454+
this.triggerCallbacks(context, "AliasBefore");
455+
}
456+
public visitAlias(context: Context): void {
457+
this.triggerAlias(context);
458+
}
459+
public triggerAlias(context: Context): void {
460+
this.triggerCallbacks(context, "Alias");
461+
}
462+
public visitAliasAfter(context: Context): void {
463+
this.triggerAliasBefore(context);
464+
}
465+
public triggerAliasAfter(context: Context): void {
466+
this.triggerCallbacks(context, "AliasAfter");
467+
}
468+
public visitAliasesAfter(context: Context): void {
469+
this.triggerAliasesAfter(context);
470+
}
471+
public triggerAliasesAfter(context: Context): void {
472+
this.triggerCallbacks(context, "AliasesAfter");
473+
}
474+
422475
public visitAllOperationsBefore(context: Context): void {
423476
this.triggerAllOperationsBefore(context);
424477
}
@@ -748,6 +801,11 @@ export class MultiVisitor extends AbstractVisitor {
748801
visitor.visitDirectivesBefore(context);
749802
});
750803
}
804+
public visitDirectiveBefore(context: Context): void {
805+
this.visitors.map((visitor) => {
806+
visitor.visitDirectiveBefore(context);
807+
});
808+
}
751809
public visitDirective(context: Context): void {
752810
this.visitors.map((visitor) => {
753811
visitor.visitDirective(context);
@@ -768,12 +826,43 @@ export class MultiVisitor extends AbstractVisitor {
768826
visitor.visitDirectiveParametersAfter(context);
769827
});
770828
}
829+
public visitDirectiveAfter(context: Context): void {
830+
this.visitors.map((visitor) => {
831+
visitor.visitDirectiveAfter(context);
832+
});
833+
}
771834
public visitDirectivesAfter(context: Context): void {
772835
this.visitors.map((visitor) => {
773836
visitor.visitDirectivesAfter(context);
774837
});
775838
}
776839

840+
public visitAliasesBefore(context: Context): void {
841+
this.visitors.map((visitor) => {
842+
visitor.visitAliasesBefore(context);
843+
});
844+
}
845+
public visitAliasBefore(context: Context): void {
846+
this.visitors.map((visitor) => {
847+
visitor.visitAliasBefore(context);
848+
});
849+
}
850+
public visitAlias(context: Context): void {
851+
this.visitors.map((visitor) => {
852+
visitor.visitAlias(context);
853+
});
854+
}
855+
public visitAliasAfter(context: Context): void {
856+
this.visitors.map((visitor) => {
857+
visitor.visitAliasAfter(context);
858+
});
859+
}
860+
public visitAliasesAfter(context: Context): void {
861+
this.visitors.map((visitor) => {
862+
visitor.visitAliasesAfter(context);
863+
});
864+
}
865+
777866
public visitAllOperationsBefore(context: Context): void {
778867
this.visitors.map((visitor) => {
779868
visitor.visitAllOperationsBefore(context);

0 commit comments

Comments
 (0)