blakeembrey / blakeembrey/javascript-stringify

Add support for getters & setters in objects

Open
#26 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
146
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Currently, objects with [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) and [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) are not handled correctly:

```ts
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](https://github.com/blakeembrey/javascript-stringify/blob/master/src/object.ts#L8) that checks for the given descriptors:

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

I made a demo in a forked repo with [this commit](https://github.com/Etheryte/javascript-stringify/commit/f1c0c7b834fc7d0ef2e1f0cfd4dd8273557f4563) 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.