Skip to content

Commit 315fa34

Browse files
committed
fix: update zmodel code generator
- include imports in output - fix indentaions - include comments in output
1 parent 47075c2 commit 315fa34

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

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

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

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

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

101107
@gen(DataSource)
@@ -107,16 +113,17 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}
107113

108114
@gen(ModelImport)
109115
private _generateModelImport(ast: ModelImport) {
110-
return `import '${ast.path}'`;
116+
return `import ${this.quotedStr(ast.path)}`;
111117
}
112118

113119
@gen(Enum)
114120
private _generateEnum(ast: Enum) {
115121
return `enum ${ast.name} {
116-
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ast.attributes.length > 0
122+
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
123+
ast.attributes.length > 0
117124
? '\n\n' + ast.attributes.map((x) => this.indent + this.generate(x)).join('\n')
118125
: ''
119-
}
126+
}
120127
}`;
121128
}
122129

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

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

142151
@gen(ConfigArrayExpr)
@@ -164,15 +173,24 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}
164173

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

170181
@gen(DataModel)
171182
private _generateDataModel(ast: DataModel) {
172-
return `${ast.isView ? 'view' : 'model'} ${ast.name}${
183+
const comments = `${ast.comments.join('\n')}\n`;
184+
185+
return `${ast.comments.length > 0 ? comments : ''}${ast.isView ? 'view' : 'model'} ${ast.name}${
173186
ast.mixins.length > 0 ? ' mixes ' + ast.mixins.map((x) => x.ref?.name).join(', ') : ''
174187
} {
175-
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
188+
${ast.fields
189+
.map((x) => {
190+
const comments = x.comments.map((c) => `${this.indent}${c}`).join('\n');
191+
return (x.comments.length ? `${comments}\n` : '') + this.indent + this.generate(x);
192+
})
193+
.join('\n')}${
176194
ast.attributes.length > 0
177195
? '\n\n' + ast.attributes.map((x) => this.indent + this.generate(x)).join('\n')
178196
: ''
@@ -182,7 +200,11 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
182200

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

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

242264
@gen(NumberLiteral)
@@ -279,7 +301,7 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
279301

280302
@gen(ReferenceArg)
281303
private _generateReferenceArg(ast: ReferenceArg) {
282-
return `${ast.name}:${this.generate(ast.value)}`;
304+
return `${ast.name}: ${this.generate(ast.value)}`;
283305
}
284306

285307
@gen(MemberAccessExpr)

0 commit comments

Comments
 (0)