Reuse safeStringify

This commit is contained in:
Alex Dima 2022-06-23 21:54:47 +02:00
parent 938124008c
commit bbee9d7864
No known key found for this signature in database
GPG key ID: 39563C1504FDD0C9
2 changed files with 5 additions and 24 deletions

View file

@ -11,7 +11,7 @@ export interface IRemoteConsoleLog {
arguments: string;
}
interface IStackArgument {
export interface IStackArgument {
__$stack: string;
}

View file

@ -3,6 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IStackArgument } from 'vs/base/common/console';
import { safeStringify } from 'vs/base/common/objects';
import { MainContext, MainThreadConsoleShape } from 'vs/workbench/api/common/extHost.protocol';
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
@ -70,7 +72,6 @@ const MAX_LENGTH = 100000;
* Prevent circular stringify and convert arguments to real array
*/
function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean): string {
const seen: any[] = [];
const argsArray = [];
// Massage some arguments with special treatment
@ -105,24 +106,12 @@ function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean):
if (includeStack) {
const stack = new Error().stack;
if (stack) {
argsArray.push({ __$stack: stack.split('\n').slice(3).join('\n') });
argsArray.push({ __$stack: stack.split('\n').slice(3).join('\n') } as IStackArgument);
}
}
try {
const res = JSON.stringify(argsArray, function (key, value) {
// Objects get special treatment to prevent circles
if (isObject(value) || Array.isArray(value)) {
if (seen.indexOf(value) !== -1) {
return '[Circular]';
}
seen.push(value);
}
return value;
});
const res = safeStringify(argsArray);
if (res.length > MAX_LENGTH) {
return 'Output omitted for a large object that exceeds the limits';
@ -133,11 +122,3 @@ function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean):
return `Output omitted for an object that cannot be inspected ('${error.toString()}')`;
}
}
function isObject(obj: unknown) {
return typeof obj === 'object'
&& obj !== null
&& !Array.isArray(obj)
&& !(obj instanceof RegExp)
&& !(obj instanceof Date);
}