Skip to content

Commit a7af852

Browse files
committed
fix: update zmodel code generator
- include imports in output - fix indentaions - include comments in output
1 parent 76fecae commit a7af852

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

packages/language/src/zmodel-code-generator.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ function gen(name: string) {
7171
*/
7272
export class ZModelCodeGenerator {
7373
private readonly options: ZModelCodeOptions;
74-
74+
private readonly quote: string;
7575
constructor(options?: Partial<ZModelCodeOptions>) {
7676
this.options = {
7777
binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1,
7878
unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0,
7979
indent: options?.indent ?? 4,
8080
quote: options?.quote ?? 'single',
8181
};
82+
this.quote = this.options.quote === 'double' ? '"' : "'";
8283
}
8384

8485
/**
@@ -92,9 +93,14 @@ export class ZModelCodeGenerator {
9293
return handler.value.call(this, ast);
9394
}
9495

96+
private quotedStr(val: string): string {
97+
const trimmedVal = val.replace(new RegExp(`${this.quote}`, 'g'), `\\${this.quote}`);
98+
return `${this.quote}${trimmedVal}${this.quote}`;
99+
}
100+
95101
@gen(Model)
96102
private _generateModel(ast: Model) {
97-
return ast.declarations.map((d) => this.generate(d)).join('\n\n');
103+
return `${ast.imports.map((d) => this.generate(d)).join('\n')}\n\n${ast.declarations.map((d) => this.generate(d)).join('\n\n')}`;
98104
}
99105

100106
@gen(DataSource)
@@ -106,16 +112,17 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}
106112

107113
@gen(ModelImport)
108114
private _generateModelImport(ast: ModelImport) {
109-
return `import '${ast.path}'`;
115+
return `import ${this.quotedStr(ast.path)}`;
110116
}
111117

112118
@gen(Enum)
113119
private _generateEnum(ast: Enum) {
114120
return `enum ${ast.name} {
115-
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ast.attributes.length > 0
121+
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
122+
ast.attributes.length > 0
116123
? '\n\n' + ast.attributes.map((x) => this.indent + this.generate(x)).join('\n')
117124
: ''
118-
}
125+
}
119126
}`;
120127
}
121128

@@ -135,7 +142,9 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}
135142

136143
@gen(ConfigField)
137144
private _generateConfigField(ast: ConfigField) {
138-
return `${ast.name} = ${this.generate(ast.value)}`;
145+
const longestName = Math.max(...ast.$container.fields.map((x) => x.name.length));
146+
const padding = ' '.repeat(longestName - ast.name.length + 1);
147+
return `${ast.name}${padding}= ${this.generate(ast.value)}`;
139148
}
140149

141150
@gen(ConfigArrayExpr)
@@ -163,15 +172,24 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}
163172

164173
@gen(PluginField)
165174
private _generatePluginField(ast: PluginField) {
166-
return `${ast.name} = ${this.generate(ast.value)}`;
175+
const longestName = Math.max(...ast.$container.fields.map((x) => x.name.length));
176+
const padding = ' '.repeat(longestName - ast.name.length + 1);
177+
return `${ast.name}${padding}= ${this.generate(ast.value)}`;
167178
}
168179

169180
@gen(DataModel)
170181
private _generateDataModel(ast: DataModel) {
171-
return `${ast.isView ? 'view' : 'model'} ${ast.name}${
182+
const comments = `${ast.comments.join('\n')}\n`;
183+
184+
return `${ast.comments.length > 0 ? comments : ''}${ast.isView ? 'view' : 'model'} ${ast.name}${
172185
ast.mixins.length > 0 ? ' mixes ' + ast.mixins.map((x) => x.$refText).join(', ') : ''
173186
} {
174-
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
187+
${ast.fields
188+
.map((x) => {
189+
const comments = x.comments.map((c) => `${this.indent}${c}`).join('\n');
190+
return (x.comments.length ? `${comments}\n` : '') + this.indent + this.generate(x);
191+
})
192+
.join('\n')}${
175193
ast.attributes.length > 0
176194
? '\n\n' + ast.attributes.map((x) => this.indent + this.generate(x)).join('\n')
177195
: ''
@@ -181,7 +199,11 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
181199

182200
@gen(DataField)
183201
private _generateDataField(ast: DataField) {
184-
return `${ast.name} ${this.fieldType(ast.type)}${
202+
const longestFieldName = Math.max(...ast.$container.fields.map((f) => f.name.length));
203+
const longestType = Math.max(...ast.$container.fields.map((f) => this.fieldType(f.type).length));
204+
const paddingLeft = longestFieldName - ast.name.length;
205+
const paddingRight = ast.attributes.length > 0 ? longestType - this.fieldType(ast.type).length : 0;
206+
return `${ast.name}${' '.repeat(paddingLeft)} ${this.fieldType(ast.type)}${' '.repeat(paddingRight)}${
185207
ast.attributes.length > 0 ? ' ' + ast.attributes.map((x) => this.generate(x)).join(' ') : ''
186208
}`;
187209
}
@@ -235,7 +257,7 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
235257

236258
@gen(StringLiteral)
237259
private _generateLiteralExpr(ast: LiteralExpr) {
238-
return this.options.quote === 'single' ? `'${ast.value}'` : `"${ast.value}"`;
260+
return this.quotedStr(ast.value as string);
239261
}
240262

241263
@gen(NumberLiteral)
@@ -278,7 +300,7 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
278300

279301
@gen(ReferenceArg)
280302
private _generateReferenceArg(ast: ReferenceArg) {
281-
return `${ast.name}:${this.generate(ast.value)}`;
303+
return `${ast.name}: ${this.generate(ast.value)}`;
282304
}
283305

284306
@gen(MemberAccessExpr)

0 commit comments

Comments
 (0)