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: add prefix for wildcard prop values #68

Merged
merged 9 commits into from
Jun 10, 2024
17 changes: 13 additions & 4 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,19 @@ export class JsonTemplateTranslator {
return code.join('');
}

private static translateObjectWildcardValue(expr: ObjectWildcardValueExpression) {
return `${VARS_PREFIX}${expr.value}`;
}

private translateObjectWildcardValueExpr(
expr: ObjectWildcardValueExpression,
dest: string,
_ctx: string,
): string {
return JsonTemplateTranslator.generateAssignmentCode(dest, expr.value);
return JsonTemplateTranslator.generateAssignmentCode(
dest,
JsonTemplateTranslator.translateObjectWildcardValue(expr),
);
}

private translateObjectWildcardProp(
Expand All @@ -557,13 +564,15 @@ export class JsonTemplateTranslator {
vars: string[] = [],
): string {
const code: string[] = [];
const keyExpr = expr.key as ObjectWildcardValueExpression;
const keyVar = JsonTemplateTranslator.translateObjectWildcardValue(
expr.key as ObjectWildcardValueExpression,
);
code.push(JsonTemplateTranslator.generateAssignmentCode(dest, '{}'));
const valueVar = this.acquireVar();
vars.push(valueVar);
code.push(`for(let [key, value] of Object.entries(${ctx})){`);
code.push(`for(let [${VARS_PREFIX}key, ${VARS_PREFIX}value] of Object.entries(${ctx})){`);
code.push(this.translateExpr(expr.value, valueVar, ctx));
code.push(`${dest}[${keyExpr.value}] = ${valueVar};`);
code.push(`${dest}[${keyVar}] = ${valueVar};`);
koladilip marked this conversation as resolved.
Show resolved Hide resolved
code.push('}');
return code.join('');
}
Expand Down
Loading