blakeembrey / blakeembrey/javascript-stringify

Add support for getters & setters in objects

未關閉
#26 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
TypeScript
星號
146
分支
16
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

從 src/object.ts 中的物件字串化邏輯開始,並將其與連結的示範 commit 進行比較。追蹤屬性描述元與 lexer 邏輯如何產生物件輸出,然後確認 getter/setter 範例會序列化為 "{_foo:null,foo:null}",且不會破壞一般屬性。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript, typescript
領域
tooling
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。