diff --git a/src/index.spec.ts b/src/index.spec.ts index e03d411..44e77c7 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -134,6 +134,11 @@ describe("javascript-stringify", () => { "should not quote Object.prototype keys", test({ constructor: 1, toString: 2 }, "{constructor:1,toString:2}"), ); + + it( + "should keep `__proto__` keys as own properties", + testRoundTrip("{['__proto__']:42}"), + ); }); describe("functions", () => { diff --git a/src/quote.ts b/src/quote.ts index 225b158..71f955a 100644 --- a/src/quote.ts +++ b/src/quote.ts @@ -77,6 +77,9 @@ export function isValidVariableName(name: PropertyKey): name is string { * Quote JavaScript key access. */ export function quoteKey(key: PropertyKey, next: Next) { + // `__proto__` as an identifier or string key is the prototype setter rather + // than an own property; emit it as a computed key so it round-trips. + if (key === "__proto__") return `[${next(key)}]`; return isValidVariableName(key) ? key : next(key); }