Skip to content

Commit

Permalink
fix: indentation for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 18, 2024
1 parent 97078b4 commit 026c336
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/reverse_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ export class JsonTemplateReverseTranslator {
return `throw ${this.translateExpression(expr.value)}`;
}

translateExpressions(exprs: Expression[], sep: string): string {
return exprs.map((expr) => this.translateExpression(expr)).join(sep);
translateExpressions(exprs: Expression[], sep: string, prefix: string = ''): string {
return exprs.map((expr) => `${prefix}${this.translateExpression(expr)}`).join(sep);
}

translateLambdaFunctionExpression(expr: FunctionExpression): string {
Expand Down Expand Up @@ -429,16 +429,17 @@ export class JsonTemplateReverseTranslator {
return `...${this.translateExpression(expr.value)}`;
}

getIndentation(): string {
return ' '.repeat(this.level);
}

translateObjectExpression(expr: ObjectExpression): string {
const code: string[] = [];
const prevIndentation = ' '.repeat(this.level);
code.push('{\n');
code.push('{');
this.level++;
const currIndentation = ' '.repeat(this.level);
code.push(currIndentation);
code.push(this.translateExpressions(expr.props, `,\n${currIndentation}`));
code.push(this.translateExpressions(expr.props, ',', `\n${this.getIndentation()}`));
this.level--;
code.push(`\n${prevIndentation}}`);
code.push(`\n${this.getIndentation()}}`);
return code.join('');
}

Expand Down

0 comments on commit 026c336

Please sign in to comment.