Skip to content

Commit

Permalink
fix: call expr arguments definition (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored Oct 27, 2023
1 parent 723a9d3 commit 6e72de6
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .changeset/nasty-rivers-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rekajs/parser': patch
'@rekajs/types': patch
'@rekajs/core': patch
---

Fix call expression arguments definition
10 changes: 2 additions & 8 deletions packages/core/src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,9 @@ export const computeExpression = (
if (expr instanceof t.CallExpression) {
const fn = env.getByIdentifier(expr.identifier);

const params = Object.keys(expr.params).reduce(
(accum, key) => ({
...accum,
[key]: computeExpression(expr.params[key], reka, env),
}),
{}
);
const args = expr.arguments.map((arg) => computeExpression(arg, reka, env));

return fn(params);
return fn(...args);
}

if (expr instanceof t.IfStatement) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export class Resolver {
if (expr instanceof t.CallExpression) {
this.resolveExpr(expr.identifier, scope);

Object.keys(expr.params).forEach((param) => {
this.resolveExpr(expr.params[param], scope);
expr.arguments.forEach((arg) => {
this.resolveExpr(arg, scope);
});
}

Expand Down
16 changes: 1 addition & 15 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,9 @@ const jsToReka = <T extends t.ASTNode = t.ASTNode>(
case 'CallExpression': {
const identifier = _convert(node.callee) as t.Identifier;

let params: Record<string, t.Expression> = {};

const arg0 = node.arguments[0];

if (arg0) {
const objExpr = _convert(arg0);
invariant(
objExpr instanceof t.ObjectExpression,
'Invalid options argument'
);

params = objExpr.properties;
}

return t.callExpression({
identifier,
params,
arguments: node.arguments.map((arg) => _convert(arg)),
});
}
case 'IfStatement': {
Expand Down
18 changes: 2 additions & 16 deletions packages/parser/src/stringifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,14 @@ class _Stringifier {
this.stringify(node.identifier);
this.writer.write('(');

const paramKeys = Object.keys(node.params);

if (paramKeys.length > 0) {
this.writer.write('{');
}

paramKeys.forEach((param, i, arr) => {
this.writer.write(
`${param}: ${this.writer.withTemp(() =>
this.stringify(node.params[param])
)}`
);
node.arguments.forEach((arg, i, arr) => {
this.stringify(arg);

if (i !== arr.length - 1) {
this.writer.write(', ');
}
});

if (paramKeys.length > 0) {
this.writer.write('}');
}

this.writer.write(')');
},
ConditionalExpression: (node) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/generated/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ Schema.register('Func', Func);
type CallExpressionParameters = {
meta?: Record<string, any>;
identifier: Identifier;
params?: Record<string, Expression>;
arguments?: Expression[];
};

export class CallExpression extends Expression {
declare identifier: Identifier;
declare params: Record<string, Expression>;
declare arguments: Expression[];
constructor(value: CallExpressionParameters) {
super('CallExpression', value);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/types.definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Schema.define('CallExpression', {
extends: 'Expression',
fields: (t) => ({
identifier: t.node('Identifier'),
params: t.defaultValue(t.map(t.node('Expression')), {}),
arguments: t.defaultValue(t.array(t.node('Expression')), []),
}),
});

Expand Down
3 changes: 1 addition & 2 deletions site/constants/encoded-dummy-program.ts

Large diffs are not rendered by default.

1 comment on commit 6e72de6

@vercel
Copy link

@vercel vercel bot commented on 6e72de6 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

reka – ./

reka-prevwong.vercel.app
reka-git-main-prevwong.vercel.app
rekajs.vercel.app
reka.js.org

Please sign in to comment.