blakeembrey / blakeembrey/javascript-stringify
Add support for getters & setters in objects
- 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
Research direction
Start with the object stringification logic in src/object.ts and compare it with the linked demo commit. Trace how property descriptors and the lexer logic produce the object output, then verify that the getter/setter example serializes as "{_foo:null,foo:null}" without breaking ordinary properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100