Skip to content

Commit

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

* fix: indentiation for objects
  • Loading branch information
koladilip authored Jun 17, 2024
1 parent 52fe549 commit 5245753
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
. "$(dirname -- "$0")/_/husky.sh"

npm test
npm run lint:fix
npm run lint:check
npm run lint-staged
13 changes: 10 additions & 3 deletions src/reverse_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import { escapeStr } from './utils';
export class JsonTemplateReverseTranslator {
private options?: EngineOptions;

private level = 0;

constructor(options?: EngineOptions) {
this.options = options;
}
Expand Down Expand Up @@ -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('');
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\"')}"`;
}
2 changes: 2 additions & 0 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function processAllFilter(
type: SyntaxType.PATH,
root: currentInputAST.root,
pathType: currentInputAST.pathType,
inferredPathType: currentInputAST.inferredPathType,
parts: matchedInputParts,
returnAsArray: true,
} as PathExpression;
Expand Down Expand Up @@ -121,6 +122,7 @@ function processWildCardSelector(
type: SyntaxType.PATH,
root: currentInputAST.root,
pathType: currentInputAST.pathType,
inferredPathType: currentInputAST.inferredPathType,
parts: matchedInputParts,
} as PathExpression;
}
Expand Down

0 comments on commit 5245753

Please sign in to comment.