Skip to content

Commit

Permalink
fix: incorrect condition in function call expression (#24)
Browse files Browse the repository at this point in the history
Condition check is applicable when function is called on object.
  • Loading branch information
koladilip authored Dec 19, 2022
1 parent 13a8d32 commit 03d503b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,13 @@ export class JsonTemplateTranslator {
code.push(JsonTemplateTranslator.generateAssignmentCode(result, ctx));
if (expr.object) {
code.push(this.translateExpr(expr.object, result, ctx));
code.push(`if(${JsonTemplateTranslator.returnIsNotEmpty(result)}){`);
}
code.push(`if(${JsonTemplateTranslator.returnIsNotEmpty(result)}){`);
const functionArgsStr = this.translateSpreadableExpressions(expr.args, result, code);
code.push(result, '=', this.getFunctionName(expr, result), '(', functionArgsStr, ');');
code.push('}');
if (expr.object) {
code.push('}');
}
code.push(JsonTemplateTranslator.generateAssignmentCode(dest, result));
this.releaseVars(result);
return code.join('');
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/functions/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export const data: Scenario[] = [
templatePath: 'parent_scope_vars.jt',
output: 90,
},
{
templatePath: 'promise.jt',
input: [1, 2],
output: [1, 2],
},
{
templatePath: 'promise.jt',
input: { a: 1 },
output: { a: 1 },
},
{
output: 80,
},
Expand Down
3 changes: 3 additions & 0 deletions test/scenarios/functions/promise.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
await new Promise(function(resolve) {
setTimeout(lambda resolve(^), 100)
});

0 comments on commit 03d503b

Please sign in to comment.