Skip to content

Commit

Permalink
Merge pull request #242 from vim-denops/exprstr-fix-tojson
Browse files Browse the repository at this point in the history
🐛 💥 Correct `toJSON` to match the specifications of `JSON.stringify`
  • Loading branch information
lambdalisue committed May 7, 2024
2 parents 341fa18 + 8d2fd8f commit 83978e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
8 changes: 6 additions & 2 deletions helper/expr_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export type ExprString = string & {
};

type Jsonable = {
toJSON(key: string | number | undefined): string;
/**
* Returns a JSON value that can be specified to {@link JSON.stringify}.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior|toJSON() behavior}
*/
toJSON(key: string | number | undefined): unknown;
};

// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -125,7 +129,7 @@ function isIgnoreRecordValue(x: unknown): boolean {
*/
export function vimStringify(value: unknown, key?: string | number): string {
if (isJsonable(value)) {
return vimStringify(JSON.parse(value.toJSON(key)));
return vimStringify(value.toJSON(key));
}
if (isExprString(value)) {
// Return Vim's expr-string
Expand Down
23 changes: 17 additions & 6 deletions helper/expr_string_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Deno.test({
fn: () => {
const actual = vimStringify({
foo: 42,
toJSON: () => JSON.stringify([123, "bar"]),
toJSON: () => [123, "bar"],
});
assertEquals(actual, "[123,'bar']");
},
Expand All @@ -171,12 +171,20 @@ Deno.test({
const actual = vimStringify(Object.assign(
() => {},
{
toJSON: () => JSON.stringify([123, "bar"]),
toJSON: () => [123, "bar"],
},
));
assertEquals(actual, "[123,'bar']");
},
});
await t.step({
name: "stringify Date that has native `toJSON()` method.",
fn: () => {
// NOTE: `Date.prototype.toJSON` returns a string representing date in the same ISO format as `Date.prototype.toISOString()`.
const actual = vimStringify(new Date("2007-08-31T12:34:56.000Z"));
assertEquals(actual, "'2007-08-31T12:34:56.000Z'");
},
});
await t.step({
name: "raises TypeError if specified BigInt.",
fn: () => {
Expand All @@ -197,7 +205,7 @@ Deno.test({
});
try {
const actual = vimStringify(92382417n);
assertEquals(actual, "92382417");
assertEquals(actual, "'92382417'");
} finally {
// deno-lint-ignore no-explicit-any
delete (BigInt.prototype as any).toJSON;
Expand All @@ -216,8 +224,9 @@ Deno.test({
Symbol("foo"),
"bar",
{
toJSON: () => JSON.stringify([123, "baz"]),
toJSON: () => [123, "baz"],
},
new Date("2007-08-31T12:34:56.000Z"),
{
k0: null,
k1: undefined,
Expand All @@ -232,7 +241,7 @@ Deno.test({
]);
assertEquals(
actual,
`[v:null,v:null,42,v:true,v:null,v:null,'bar',[123,'baz'],{'k0':v:null,'k2':[{'key':234,'expr':"\\U0001F680"}]}]`,
`[v:null,v:null,42,v:true,v:null,v:null,'bar',[123,'baz'],'2007-08-31T12:34:56.000Z',{'k0':v:null,'k2':[{'key':234,'expr':"\\U0001F680"}]}]`,
);
},
});
Expand Down Expand Up @@ -419,8 +428,9 @@ test({
Symbol("foo"),
"bar",
{
toJSON: () => JSON.stringify([123, "baz"]),
toJSON: () => [123, "baz"],
},
new Date("2007-08-31T12:34:56.000Z"),
{
k0: null,
k1: undefined,
Expand Down Expand Up @@ -448,6 +458,7 @@ test({
123,
"baz",
],
"2007-08-31T12:34:56.000Z",
{
k0: null,
k2: [
Expand Down

0 comments on commit 83978e8

Please sign in to comment.