From 026c3368f1588b7d36ec1d049048c2d83e0977a7 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Tue, 18 Jun 2024 08:41:16 +0530 Subject: [PATCH] fix: indentation for objects --- src/reverse_translator.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/reverse_translator.ts b/src/reverse_translator.ts index 78cc8a3..149fd10 100644 --- a/src/reverse_translator.ts +++ b/src/reverse_translator.ts @@ -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 { @@ -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(''); }