Skip to content

Commit

Permalink
fix: stringifiy obj expression (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored Nov 6, 2023
1 parent 5d934d0 commit 0792582
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tame-hats-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rekajs/parser': patch
---

Fix stringify obj expressions
16 changes: 16 additions & 0 deletions packages/parser/src/tests/stringifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,20 @@ describe('Stringifier', () => {
)
).toEqual(`left + (isRequired ? "*" : "")`);
});
it('should be able to stringify nested objects correctly', () => {
expect(
Stringifier.toString(
t.assignment({
left: t.identifier({ name: 'obj' }),
operator: '=',
right: t.objectExpression({
properties: {
foo: t.literal({ value: 1 }),
bar: t.literal({ value: 0 }),
},
}),
})
)
).toEqual(`obj = {\n "foo": 1,\n "bar": 0\n}`);
});
});
6 changes: 5 additions & 1 deletion packages/parser/src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Writer {
}

arr[i - 1] += current;
arr.length--;
arr.splice(i, 1);
}

return arr;
Expand Down Expand Up @@ -132,6 +132,10 @@ export class Writer {
for (let i = 0; i < arr.length; i++) {
const c = arr[i];

if (c.length === 0) {
continue;
}

if (Array.isArray(c)) {
const prev = depth;
depth = depth + 1;
Expand Down

1 comment on commit 0792582

@vercel
Copy link

@vercel vercel bot commented on 0792582 Nov 6, 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.