From 11447aa91ad1fdfad7b5263361b71f8b550adbb4 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Mon, 17 Jun 2024 20:47:41 +0530 Subject: [PATCH 1/5] fix: array and object mappings --- src/utils/converter.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 533c826..c142e86 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -77,6 +77,7 @@ function processAllFilter( type: SyntaxType.PATH, root: currentInputAST.root, pathType: currentInputAST.pathType, + inferredPathType: currentInputAST.inferredPathType, parts: matchedInputParts, returnAsArray: true, } as PathExpression; @@ -121,6 +122,7 @@ function processWildCardSelector( type: SyntaxType.PATH, root: currentInputAST.root, pathType: currentInputAST.pathType, + inferredPathType: currentInputAST.inferredPathType, parts: matchedInputParts, } as PathExpression; } From d6e24af3c73f60a539b2a19c0544e0f4d7e32ece Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Tue, 18 Jun 2024 00:31:41 +0530 Subject: [PATCH 2/5] fix: indentiation for objects --- .husky/pre-commit | 1 + src/reverse_translator.ts | 13 ++++++++++--- src/utils/common.test.ts | 4 ++-- src/utils/common.ts | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 1c43e09..ddd6c03 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -2,5 +2,6 @@ . "$(dirname -- "$0")/_/husky.sh" npm test +npm run lint:fix npm run lint:check npm run lint-staged \ No newline at end of file diff --git a/src/reverse_translator.ts b/src/reverse_translator.ts index 4699539..78cc8a3 100644 --- a/src/reverse_translator.ts +++ b/src/reverse_translator.ts @@ -39,6 +39,8 @@ import { escapeStr } from './utils'; export class JsonTemplateReverseTranslator { private options?: EngineOptions; + private level = 0; + constructor(options?: EngineOptions) { this.options = options; } @@ -429,9 +431,14 @@ export class JsonTemplateReverseTranslator { translateObjectExpression(expr: ObjectExpression): string { const code: string[] = []; - code.push('{\n\t'); - code.push(this.translateExpressions(expr.props, ',\n\t')); - code.push('\n}'); + const prevIndentation = ' '.repeat(this.level); + code.push('{\n'); + this.level++; + const currIndentation = ' '.repeat(this.level); + code.push(currIndentation); + code.push(this.translateExpressions(expr.props, `,\n${currIndentation}`)); + this.level--; + code.push(`\n${prevIndentation}}`); return code.join(''); } diff --git a/src/utils/common.test.ts b/src/utils/common.test.ts index 16d0cd8..5e0b011 100644 --- a/src/utils/common.test.ts +++ b/src/utils/common.test.ts @@ -48,11 +48,11 @@ describe('Common Utils tests', () => { expect(CommonUtils.escapeStr(undefined)).toEqual(''); }); it('should return escaped string for simple string input', () => { - expect(CommonUtils.escapeStr('aabc')).toEqual(`'aabc'`); + expect(CommonUtils.escapeStr('aabc')).toEqual(`"aabc"`); }); it('should return escaped string for string with escape characters', () => { - expect(CommonUtils.escapeStr(`a\nb'c`)).toEqual(`'a\nb\\'c'`); + expect(CommonUtils.escapeStr(`a\nb'"c`)).toEqual(`"a\nb'\\"c"`); }); }); describe('CreateAsyncFunction', () => { diff --git a/src/utils/common.ts b/src/utils/common.ts index 83bf884..68cdfe8 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -49,5 +49,5 @@ export function escapeStr(s?: string): string { if (typeof s !== 'string') { return ''; } - return `'${s.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`; + return `"${s.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; } From 026c3368f1588b7d36ec1d049048c2d83e0977a7 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Tue, 18 Jun 2024 08:41:16 +0530 Subject: [PATCH 3/5] 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(''); } From 3ce28bbb0acacfe6df0b1500a6588365cb814191 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Tue, 18 Jun 2024 08:43:43 +0530 Subject: [PATCH 4/5] fix: indentation for objects --- src/reverse_translator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reverse_translator.ts b/src/reverse_translator.ts index 149fd10..d5ad3a0 100644 --- a/src/reverse_translator.ts +++ b/src/reverse_translator.ts @@ -430,7 +430,7 @@ export class JsonTemplateReverseTranslator { } getIndentation(): string { - return ' '.repeat(this.level); + return ' '.repeat(this.level); } translateObjectExpression(expr: ObjectExpression): string { From 2431bcc1c0f0734fb5d87619810af720a9d43eba Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Tue, 18 Jun 2024 08:45:38 +0530 Subject: [PATCH 5/5] fix: indentation for objects --- src/constants.ts | 1 + src/reverse_translator.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index ba00551..46575cb 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 }; diff --git a/src/reverse_translator.ts b/src/reverse_translator.ts index d5ad3a0..f23f5b0 100644 --- a/src/reverse_translator.ts +++ b/src/reverse_translator.ts @@ -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 { @@ -430,7 +430,7 @@ export class JsonTemplateReverseTranslator { } getIndentation(): string { - return ' '.repeat(this.level); + return ' '.repeat(this.level * INDENTATION_SPACES); } translateObjectExpression(expr: ObjectExpression): string {