Skip to content

Commit d0b5ddf

Browse files
authored
Misc Fixes (#9)
* Fixed required annotation error message * Making a return type optional (assumes void) and fixing validation rule for void returns * Disable camel case directive name rule * Fix descriptions over namespaces, imports, directives, interfaces and roles * Make parameters parenthesis options for directive definitions * Added parseType * Switch on kind improvement * Added annotation callback * novisit annotation for types and enums * Operation returns void validation fix * Required field from type in valid annotation check
1 parent 7483b41 commit d0b5ddf

10 files changed

+140
-75
lines changed

src/ast/definitions.ts

+55-21
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ export class NamespaceDefinition extends AbstractNode {
4949
this.annotations = annotations || [];
5050
}
5151

52-
directive(name: string): Annotation | undefined {
53-
return getAnnotation(name, this.annotations);
52+
annotation(
53+
name: string,
54+
callback?: (annotation: Annotation) => void
55+
): Annotation | undefined {
56+
return getAnnotation(name, this.annotations, callback);
5457
}
5558

5659
public accept(context: Context, visitor: Visitor): void {
@@ -82,8 +85,11 @@ export class ImportDefinition extends AbstractNode {
8285
this.annotations = annotations || [];
8386
}
8487

85-
directive(name: string): Annotation | undefined {
86-
return getAnnotation(name, this.annotations);
88+
annotation(
89+
name: string,
90+
callback?: (annotation: Annotation) => void
91+
): Annotation | undefined {
92+
return getAnnotation(name, this.annotations, callback);
8793
}
8894

8995
public accept(context: Context, visitor: Visitor): void {
@@ -115,8 +121,11 @@ export class TypeDefinition extends AbstractNode {
115121
this.fields = fields;
116122
}
117123

118-
annotation(name: string): Annotation | undefined {
119-
return getAnnotation(name, this.annotations);
124+
annotation(
125+
name: string,
126+
callback?: (annotation: Annotation) => void
127+
): Annotation | undefined {
128+
return getAnnotation(name, this.annotations, callback);
120129
}
121130

122131
public accept(context: Context, visitor: Visitor): void {
@@ -183,8 +192,11 @@ export class OperationDefinition extends AbstractNode {
183192
return mp;
184193
}
185194

186-
annotaton(name: string): Annotation | undefined {
187-
return getAnnotation(name, this.annotations);
195+
annotation(
196+
name: string,
197+
callback?: (annotation: Annotation) => void
198+
): Annotation | undefined {
199+
return getAnnotation(name, this.annotations, callback);
188200
}
189201

190202
public accept(context: Context, visitor: Visitor): void {
@@ -230,8 +242,11 @@ export abstract class ValuedDefinition extends AbstractNode {
230242
this.annotations = annotations;
231243
}
232244

233-
directive(name: string): Annotation | undefined {
234-
return getAnnotation(name, this.annotations);
245+
annotation(
246+
name: string,
247+
callback?: (annotation: Annotation) => void
248+
): Annotation | undefined {
249+
return getAnnotation(name, this.annotations, callback);
235250
}
236251
}
237252

@@ -300,8 +315,11 @@ export class InterfaceDefinition extends AbstractNode implements Definition {
300315
this.annotations = annotations || [];
301316
}
302317

303-
directive(name: string): Annotation | undefined {
304-
return getAnnotation(name, this.annotations);
318+
annotation(
319+
name: string,
320+
callback?: (annotation: Annotation) => void
321+
): Annotation | undefined {
322+
return getAnnotation(name, this.annotations, callback);
305323
}
306324

307325
public accept(context: Context, visitor: Visitor): void {
@@ -340,8 +358,11 @@ export class RoleDefinition extends AbstractNode implements Definition {
340358
this.annotations = annotations || [];
341359
}
342360

343-
directive(name: string): Annotation | undefined {
344-
return getAnnotation(name, this.annotations);
361+
annotation(
362+
name: string,
363+
callback?: (annotation: Annotation) => void
364+
): Annotation | undefined {
365+
return getAnnotation(name, this.annotations, callback);
345366
}
346367

347368
public accept(context: Context, visitor: Visitor): void {
@@ -380,8 +401,11 @@ export class UnionDefinition extends AbstractNode implements Definition {
380401
this.types = types;
381402
}
382403

383-
directive(name: string): Annotation | undefined {
384-
return getAnnotation(name, this.annotations);
404+
annotation(
405+
name: string,
406+
callback?: (annotation: Annotation) => void
407+
): Annotation | undefined {
408+
return getAnnotation(name, this.annotations, callback);
385409
}
386410

387411
public accept(context: Context, visitor: Visitor): void {
@@ -410,8 +434,11 @@ export class EnumDefinition extends AbstractNode implements Definition {
410434
this.values = values;
411435
}
412436

413-
directive(name: string): Annotation | undefined {
414-
return getAnnotation(name, this.annotations);
437+
annotation(
438+
name: string,
439+
callback?: (annotation: Annotation) => void
440+
): Annotation | undefined {
441+
return getAnnotation(name, this.annotations, callback);
415442
}
416443

417444
public accept(context: Context, visitor: Visitor): void {
@@ -452,8 +479,11 @@ export class EnumValueDefinition extends AbstractNode implements Definition {
452479
this.annotations = annotations;
453480
}
454481

455-
directive(name: string): Annotation | undefined {
456-
return getAnnotation(name, this.annotations);
482+
annotation(
483+
name: string,
484+
callback?: (annotation: Annotation) => void
485+
): Annotation | undefined {
486+
return getAnnotation(name, this.annotations, callback);
457487
}
458488

459489
public accept(context: Context, visitor: Visitor): void {
@@ -533,13 +563,17 @@ function visitAnnotations(
533563

534564
function getAnnotation(
535565
name: string,
536-
annotations?: Annotation[]
566+
annotations?: Annotation[],
567+
callback?: (annotation: Annotation) => void
537568
): Annotation | undefined {
538569
if (annotations == undefined) {
539570
return undefined;
540571
}
541572
for (let a of annotations!) {
542573
if (a.name.value === name) {
574+
if (callback != undefined) {
575+
callback(a);
576+
}
543577
return a;
544578
}
545579
}

src/ast/document.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export class Document extends AbstractNode {
4242

4343
visitor.visitTypesBefore(context);
4444
context.types.map((type) => {
45-
type.accept(context.clone({ type: type }), visitor);
45+
if (!type.annotation("novisit")) {
46+
type.accept(context.clone({ type: type }), visitor);
47+
}
4648
});
4749
visitor.visitTypesAfter(context);
4850

@@ -54,7 +56,9 @@ export class Document extends AbstractNode {
5456

5557
visitor.visitEnumsBefore(context);
5658
context.enums.map((enumDef) => {
57-
enumDef.accept(context.clone({ enumDef: enumDef }), visitor);
59+
if (!enumDef.annotation("novisit")) {
60+
enumDef.accept(context.clone({ enumDef: enumDef }), visitor);
61+
}
5862
});
5963
visitor.visitEnumsAfter(context);
6064

src/ast/nodes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export class Annotation extends AbstractNode {
5757
this.arguments = args || [];
5858
}
5959

60-
toObject(): { [k: string]: any } {
60+
convert<T>(): T {
6161
let obj: { [k: string]: any } = {};
6262
this.arguments.map((arg) => {
6363
obj[arg.name.value] = arg.value.getValue();
6464
});
65-
return obj;
65+
return obj as T;
6666
}
6767

6868
public accept(context: Context, visitor: Visitor): void {

src/ast/visitor.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -197,36 +197,36 @@ export class Context {
197197

198198
private parseDocument(): void {
199199
this.document!.definitions.forEach((value) => {
200-
switch (true) {
201-
case value.isKind(Kind.NamespaceDefinition):
200+
switch (value.getKind()) {
201+
case Kind.NamespaceDefinition:
202202
this.namespace = value as NamespaceDefinition;
203203
break;
204-
case value.isKind(Kind.DirectiveDefinition):
204+
case Kind.DirectiveDefinition:
205205
const directive = value as DirectiveDefinition;
206206
this.directives.push(directive);
207207
this.directiveMap.set(directive.name.value, directive);
208208
break;
209-
case value.isKind(Kind.ImportDefinition):
209+
case Kind.ImportDefinition:
210210
this.imports.push(value as ImportDefinition);
211211
break;
212-
case value.isKind(Kind.InterfaceDefinition):
212+
case Kind.InterfaceDefinition:
213213
this.interface = value as InterfaceDefinition;
214214
break;
215-
case value.isKind(Kind.RoleDefinition):
215+
case Kind.RoleDefinition:
216216
const role = value as RoleDefinition;
217217
this.roles.push(role);
218218
break;
219-
case value.isKind(Kind.TypeDefinition):
219+
case Kind.TypeDefinition:
220220
const type = value as TypeDefinition;
221221
this.types.push(type);
222222
this.allTypes.set(type.name.value, type);
223223
break;
224-
case value.isKind(Kind.EnumDefinition):
224+
case Kind.EnumDefinition:
225225
const enumDef = value as EnumDefinition;
226226
this.enums.push(enumDef);
227227
this.allTypes.set(enumDef.name.value, enumDef);
228228
break;
229-
case value.isKind(Kind.UnionDefinition):
229+
case Kind.UnionDefinition:
230230
const union = value as UnionDefinition;
231231
this.unions.push(union);
232232
this.allTypes.set(union.name.value, union);

0 commit comments

Comments
 (0)