Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
3 changes: 3 additions & 0 deletions src/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down