MCPcopy
hub / github.com/microsoft/vscode / safeToString

Function safeToString

src/bootstrap-fork.ts:21–75  ·  view source on GitHub ↗

* Prevent circular stringify and convert arguments to real array

(args: ArrayLike<unknown>)

Source from the content-addressed store, hash-verified

19 * Prevent circular stringify and convert arguments to real array
20 */
21 function safeToString(args: ArrayLike<unknown>): string {
22 const seen: unknown[] = [];
23 const argsArray: unknown[] = [];
24
25 // Massage some arguments with special treatment
26 if (args.length) {
27 for (let i = 0; i < args.length; i++) {
28 let arg = args[i];
29
30 // Any argument of type 'undefined' needs to be specially treated because
31 // JSON.stringify will simply ignore those. We replace them with the string
32 // 'undefined' which is not 100% right, but good enough to be logged to console
33 if (typeof arg === 'undefined') {
34 arg = 'undefined';
35 }
36
37 // Any argument that is an Error will be changed to be just the error stack/message
38 // itself because currently cannot serialize the error over entirely.
39 else if (arg instanceof Error) {
40 const errorObj = arg;
41 if (errorObj.stack) {
42 arg = errorObj.stack;
43 } else {
44 arg = errorObj.toString();
45 }
46 }
47
48 argsArray.push(arg);
49 }
50 }
51
52 try {
53 const res = JSON.stringify(argsArray, function (key, value: unknown) {
54
55 // Objects get special treatment to prevent circles
56 if (isObject(value) || Array.isArray(value)) {
57 if (seen.indexOf(value) !== -1) {
58 return '[Circular]';
59 }
60
61 seen.push(value);
62 }
63
64 return value;
65 });
66
67 if (res.length > MAX_LENGTH) {
68 return 'Output omitted for a large object that exceeds the limits';
69 }
70
71 return res;
72 } catch (error) {
73 return `Output omitted for an object that cannot be inspected ('${error.toString()}')`;
74 }
75 }
76
77 function safeSend(arg: { type: string; severity: string; arguments: string }): void {
78 try {

Callers 1

wrapConsoleMethodFunction · 0.85

Calls 6

stringifyMethod · 0.80
isArrayMethod · 0.80
isObjectFunction · 0.70
toStringMethod · 0.65
pushMethod · 0.65
indexOfMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…