Skip to content

Commit

Permalink
fix: indentation for objects (#101)
Browse files Browse the repository at this point in the history
* fix: array and object mappings

* fix: indentiation for objects

* fix: indentation for objects

* fix: indentation for objects

* fix: indentation for objects
  • Loading branch information
koladilip authored Jun 18, 2024
1 parent 4fe6873 commit b7ca7be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const BINDINGS_PARAM_KEY = '___b';
export const BINDINGS_CONTEXT_KEY = '___b.context.';
export const RESULT_KEY = '___r';
export const FUNCTION_RESULT_KEY = '___f';
export const INDENTATION_SPACES = 4;
export const EMPTY_EXPR = { type: SyntaxType.EMPTY };
19 changes: 10 additions & 9 deletions src/reverse_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
UnaryExpression,
} from './types';
import { translateLiteral } from './utils/translator';
import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY, EMPTY_EXPR } from './constants';
import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY, EMPTY_EXPR, INDENTATION_SPACES } from './constants';
import { escapeStr } from './utils';

export class JsonTemplateReverseTranslator {
Expand Down 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 * INDENTATION_SPACES);
}

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 b7ca7be

Please sign in to comment.