Skip to content

Remove JSON.stringify shim #12721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Dec 8, 2016
Merged
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
18 changes: 0 additions & 18 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,24 +863,6 @@ namespace ts {
return result;
}

/**
* Reduce the properties defined on a map-like (but not from its prototype chain).
*
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
* reduceProperties instead as it offers better performance.
*
* @param map The map-like to reduce
* @param callback An aggregation function that is called for each entry in the map
* @param initial The initial value for the reduction.
*/
export function reduceOwnProperties<T, U>(map: MapLike<T>, callback: (aggregate: U, value: T, key: string) => U, initial: U): U {
let result = initial;
for (const key in map) if (hasOwnProperty.call(map, key)) {
result = callback(result, map[key], String(key));
}
return result;
}

/**
* Performs a shallow equality comparison of the contents of two map-likes.
*
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ namespace ts {

encodeLastRecordedSourceMapSpan();

return stringify({
return JSON.stringify({
version: 3,
file: sourceMapData.sourceMapFile,
sourceRoot: sourceMapData.sourceMapSourceRoot,
Expand Down
49 changes: 0 additions & 49 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3189,55 +3189,6 @@ namespace ts {
return output;
}

/**
* Serialize an object graph into a JSON string. This is intended only for use on an acyclic graph
* as the fallback implementation does not check for circular references by default.
*/
export const stringify: (value: any) => string = typeof JSON !== "undefined" && JSON.stringify
? JSON.stringify
: stringifyFallback;

/**
* Serialize an object graph into a JSON string.
*/
function stringifyFallback(value: any): string {
// JSON.stringify returns `undefined` here, instead of the string "undefined".
return value === undefined ? undefined : stringifyValue(value);
}

function stringifyValue(value: any): string {
return typeof value === "string" ? `"${escapeString(value)}"`
: typeof value === "number" ? isFinite(value) ? String(value) : "null"
: typeof value === "boolean" ? value ? "true" : "false"
: typeof value === "object" && value ? isArray(value) ? cycleCheck(stringifyArray, value) : cycleCheck(stringifyObject, value)
: /*fallback*/ "null";
}

function cycleCheck(cb: (value: any) => string, value: any) {
Debug.assert(!value.hasOwnProperty("__cycle"), "Converting circular structure to JSON");
value.__cycle = true;
const result = cb(value);
delete value.__cycle;
return result;
}

function stringifyArray(value: any) {
return `[${reduceLeft(value, stringifyElement, "")}]`;
}

function stringifyElement(memo: string, value: any) {
return (memo ? memo + "," : memo) + stringifyValue(value);
}

function stringifyObject(value: any) {
return `{${reduceOwnProperties(value, stringifyProperty, "")}}`;
}

function stringifyProperty(memo: string, value: any, key: string) {
return value === undefined || typeof value === "function" || key === "__cycle" ? memo
: (memo ? memo + "," : memo) + `"${escapeString(key)}":${stringifyValue(value)}`;
}

const base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

/**
Expand Down