diff --git a/.changeset/tame-hats-sort.md b/.changeset/tame-hats-sort.md new file mode 100644 index 0000000..1c45bd8 --- /dev/null +++ b/.changeset/tame-hats-sort.md @@ -0,0 +1,5 @@ +--- +'@rekajs/parser': patch +--- + +Fix stringify obj expressions diff --git a/packages/parser/src/tests/stringifier.test.ts b/packages/parser/src/tests/stringifier.test.ts index 4907330..10399b7 100644 --- a/packages/parser/src/tests/stringifier.test.ts +++ b/packages/parser/src/tests/stringifier.test.ts @@ -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}`); + }); }); diff --git a/packages/parser/src/writer.ts b/packages/parser/src/writer.ts index 05b0d9f..fc5ed26 100644 --- a/packages/parser/src/writer.ts +++ b/packages/parser/src/writer.ts @@ -44,7 +44,7 @@ export class Writer { } arr[i - 1] += current; - arr.length--; + arr.splice(i, 1); } return arr; @@ -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;