1- import { BaseVisitor , Context , Writer } from "@apexlang/core/model" ;
1+ import { BaseVisitor , Context , Writer } from "@apexlang/core/model" ;
22
33export class EnumVisitor extends BaseVisitor {
4- constructor ( writer : Writer ) {
5- super ( writer ) ;
6- }
4+ constructor ( writer : Writer ) {
5+ super ( writer ) ;
6+ }
77
8- visitEnumBefore ( context : Context ) {
9- const { enum : enumNode } = context ;
10- enumNode . description && this . write ( `// ${ enumNode . description } \n` ) ;
11- super . visitEnumBefore ( context ) ;
12- }
8+ visitEnumBefore ( context : Context ) {
9+ const { enum : enumNode } = context ;
10+ enumNode . description && this . write ( `// ${ enumNode . description } \n` ) ;
11+ super . visitEnumBefore ( context ) ;
12+ }
1313
14- visitEnum ( context : Context ) {
15- const { enum : enumNode } = context ;
16- this . write ( `enum ${ enumNode . name } {` ) ;
17- this . write ( `\n` ) ;
18- super . visitEnum ( context ) ;
19- }
14+ visitEnum ( context : Context ) {
15+ const { enum : enumNode } = context ;
16+ this . write ( `enum ${ enumNode . name } {` ) ;
17+ this . write ( `\n` ) ;
18+ super . visitEnum ( context ) ;
19+ }
2020
21- visitEnumValuesBefore ( context : Context ) {
22- // const {enum: enumNode} = context;
23- // enumNode.description && this.write(`// ${enumNode.description}\n`);
24- const { enumValues} = context ;
25- enumValues . map ( ( enumValue , index ) => {
26- if ( index > 0 ) {
27- enumValue . description && this . write ( `,${ enumValue . description } ` ) ;
28- } else {
29- enumValue . description && this . write ( `\t // ${ enumValue . description } ` ) ;
30- }
31- } ) ;
32- this . write ( `\n` ) ;
33- // enumValue.node.description?.getValue() && this.write(`// ${enumValue.node.description.getValue()}`);
34- super . visitEnumValuesBefore ( context ) ;
35- }
21+ visitEnumValuesBefore ( context : Context ) {
22+ // const {enum: enumNode} = context;
23+ // enumNode.description && this.write(`// ${enumNode.description}\n`);
24+ const { enumValues } = context ;
25+ enumValues . map ( ( enumValue , index ) => {
26+ if ( index > 0 ) {
27+ enumValue . description && this . write ( `,${ enumValue . description } ` ) ;
28+ } else {
29+ enumValue . description && this . write ( `\t // ${ enumValue . description } ` ) ;
30+ }
31+ } ) ;
32+ this . write ( `\n` ) ;
33+ // enumValue.node.description?.getValue() && this.write(`// ${enumValue.node.description.getValue()}`);
34+ super . visitEnumValuesBefore ( context ) ;
35+ }
3636
37- visitEnumValue ( context : Context ) {
38- const { enumValue} = context ;
39- if ( enumValue . index > 0 ) {
40- this . write ( `,${ enumValue . name } ` ) ;
41- } else {
42- this . write ( `\t ${ enumValue . name } ` ) ;
43- }
44- // this.write(`${enumValue.name} = ${enumValue.index} as "${enumValue.display}"`);
45- super . visitEnumValue ( context ) ;
37+ visitEnumValue ( context : Context ) {
38+ const { enum : enumNode , enumValue, enumValues } = context ;
39+ if ( enumValues . length - 1 === enumValue . index ) {
40+ this . write ( `,\n` ) ;
41+ this . write ( `\t ${ enumValue . name } (${ enumValue . index } );` ) ;
42+ this . write ( `\n` ) ;
43+ this . write ( `\n` ) ;
44+ this . write ( `\t private final int value;` ) ;
45+ this . write ( `\n` ) ;
46+ this . write ( `\t ${ enumNode . name } (int value) {` ) ;
47+ this . write ( `\n` ) ;
48+ this . write ( `\t\t this.value = value;` ) ;
49+ this . write ( `\n` ) ;
50+ this . write ( `\t }` ) ;
51+ } else if ( enumValue . index > 0 ) {
52+ this . write ( `,\n` ) ;
53+ this . write ( `\t ${ enumValue . name } (${ enumValue . index } )` ) ;
54+ } else {
55+ this . write ( `\t ${ enumValue . name } (${ enumValue . index } )` ) ;
4656 }
57+ // this.write(`${enumValue.name} = ${enumValue.index} as "${enumValue.display}"`);
58+ super . visitEnumValue ( context ) ;
59+ }
4760
48- visitEnumAfter ( context : Context ) {
49- this . write ( `\n}` ) ;
50- super . visitEnumAfter ( context ) ;
51- }
52- }
61+ visitEnumAfter ( context : Context ) {
62+ this . write ( `\n}` ) ;
63+ super . visitEnumAfter ( context ) ;
64+ }
65+ }
0 commit comments