Skip to content

Commit aa5e576

Browse files
committed
minor tweak to null handling in stringify
1 parent 5c23a5f commit aa5e576

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/compiler/utilities.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,24 +2415,28 @@ namespace ts {
24152415
}
24162416

24172417
export const stringify: (value: any) => string = JSON && JSON.stringify ? JSON.stringify : function stringify(value: any): string {
2418+
return value === undefined ? undefined : stringifyValue(value);
2419+
};
2420+
2421+
function stringifyValue(value: any): string {
24182422
/* tslint:disable:no-null */
2419-
return value == null ? "null"
2423+
return value === null ? "null" // explicit test for `null` as `typeof null` is "object"
24202424
: typeof value === "string" ? `"${escapeString(value)}"`
24212425
: typeof value === "number" ? String(value)
24222426
: typeof value === "boolean" ? value ? "true" : "false"
24232427
: isArray(value) ? `[${reduceLeft(value, stringifyElement, "")}]`
24242428
: typeof value === "object" ? `{${reduceProperties(value, stringifyProperty, "")}}`
2425-
: "null";
2429+
: /*fallback*/ "null";
24262430
/* tslint:enable:no-null */
2427-
};
2431+
}
24282432

24292433
function stringifyElement(memo: string, value: any) {
2430-
return (memo ? memo + "," : memo) + stringify(value);
2434+
return (memo ? memo + "," : memo) + stringifyValue(value);
24312435
}
24322436

24332437
function stringifyProperty(memo: string, value: any, key: string) {
2434-
return value === undefined ? memo
2435-
: (memo ? memo + "," : memo) + `"${escapeString(key)}":${stringify(value)}`;
2438+
return value === undefined || typeof value === "function" ? memo
2439+
: (memo ? memo + "," : memo) + `"${escapeString(key)}":${stringifyValue(value)}`;
24362440
}
24372441

24382442
const base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

0 commit comments

Comments
 (0)