blakeembrey / blakeembrey/javascript-stringify

Add support for getters & setters in objects

Aperta
#26 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
TypeScript
Stelle
146
Fork
16
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.