Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: indentation for objects #101

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading