blakeembrey/javascript-stringify

Add support for getters & setters in objects

Etheryte opened this issue · 0 comments

Currently, objects with getters and setters are not handled correctly:

const input = {
    _foo: null,
    get foo() {
        return this._foo;
    },
    set foo(value) {
        this._foo = value;
    }
};

const output = "{_foo:null,foo:null}";

This problem can be solved by adding an extra case to the object stringification logic that checks for the given descriptors:

const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor && (descriptor.get || descriptor.set)) { ... }

I made a demo in a forked repo with this commit demonstrating the approach.
Sadly, I didn't have time to figure out all of the lexer logic on the go so it isn't clean enough for a flat out PR.