Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rschnekenbu committed Apr 25, 2023
1 parent f49f69e commit 783274a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 82 deletions.
49 changes: 48 additions & 1 deletion packages/core/src/common/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { isObject } from './types';
import { isObject, isUndefined } from './types';

export function deepClone<T>(obj: T): T {
if (!isObject(obj)) {
Expand Down Expand Up @@ -70,3 +70,50 @@ export function notEmpty<T>(arg: T | undefined | null): arg is T {
export function isEmpty(arg: Object): boolean {
return Object.keys(arg).length === 0 && arg.constructor === Object;
}

// copied and modified from https://github.com/microsoft/vscode/blob/1.76.0/src/vs/base/common/objects.ts#L45-L83
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function cloneAndChange(obj: any, changer: (orig: any) => any, seen: Set<any>): any {
// impossible to clone an undefined or null object
// eslint-disable-next-line no-null/no-null
if (isUndefined(obj) || obj === null) {
return obj;
}

const changed = changer(obj);
if (!isUndefined(changed)) {
return changed;
}

if (Array.isArray(obj)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const r1: any[] = [];
for (const e of obj) {
r1.push(cloneAndChange(e, changer, seen));
}
return r1;
}

if (isObject(obj)) {
if (seen.has(obj)) {
throw new Error('Cannot clone recursive data-structure');
}
seen.add(obj);
const r2 = {};
for (const i2 in obj) {
if (_hasOwnProperty.call(obj, i2)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(r2 as any)[i2] = cloneAndChange(obj[i2], changer, seen);
}
}
seen.delete(obj);
return r2;
}

return obj;
}
1 change: 0 additions & 1 deletion packages/core/src/common/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ interface TelemetrySender {
}

interface TelemetryLoggerOptions {

}
1 change: 0 additions & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,6 @@ export interface TabsMain {
}

export interface TelemetryMain {

}

export interface TelemetryExt {
Expand Down
84 changes: 5 additions & 79 deletions packages/plugin-ext/src/plugin/telemetry-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

import { Event, Emitter } from '@theia/core/lib/common/event';
import { cloneAndChange } from '@theia/core';
import { mixin } from '../common/types';
import { TelemetryTrustedValue, TelemetryLoggerOptions } from './types-impl';

export class TelemetryExtImpl {
Expand Down Expand Up @@ -174,6 +177,7 @@ function mixInCommonPropsAndCleanData(data: Record<string, any>, additionalPrope
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

/**
* Cleans a given stack of possible paths
* @param stack The stack to sanitize
Expand Down Expand Up @@ -214,7 +218,7 @@ function anonymizeFilePaths(stack: string, cleanupPatterns: RegExp[]): string {
// Check to see if the any cleanupIndexes partially overlap with this match
const overlappingRange = cleanUpIndexes.some(([start, end]) => result.index < end && start < fileRegex.lastIndex);

// anoynimize user file paths that do not need to be retained or cleaned up.
// anonymize user file paths that do not need to be retained or cleaned up.
if (!nodeModulesRegex.test(result[0]) && !overlappingRange) {
updatedStack += stack.substring(lastIndex, result.index) + '<REDACTED: user-file-path>';
lastIndex = fileRegex.lastIndex;
Expand Down Expand Up @@ -293,81 +297,3 @@ export function cleanData(data: Record<string, any>, cleanUpPatterns: RegExp[]):
}, new Set());
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function cloneAndChange(obj: any, changer: (orig: any) => any, seen: Set<any>): any {
// impossible to clone a null or undefined object
if (isUndefined(obj) || obj === null) {
return obj;
}

const changed = changer(obj);
if (!isUndefined(changed)) {
return changed;
}

if (Array.isArray(obj)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const r1: any[] = [];
for (const e of obj) {
r1.push(cloneAndChange(e, changer, seen));
}
return r1;
}

if (isObject(obj)) {
if (seen.has(obj)) {
throw new Error('Cannot clone recursive data-structure');
}
seen.add(obj);
const r2 = {};
for (const i2 in obj) {
if (Object.prototype.hasOwnProperty.call(obj, i2)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(r2 as any)[i2] = cloneAndChange(obj[i2], changer, seen);
}
}
seen.delete(obj);
return r2;
}

return obj;
}

/**
* Copies all properties of source into destination. The optional parameter "overwrite" allows to control
* if existing properties on the destination should be overwritten or not. Defaults to true (overwrite).
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function mixin(destination: any, source: any, overwrite: boolean = true): any {
if (!isObject(destination)) {
return source;
}

if (isObject(source)) {
Object.keys(source).forEach(key => {
if (key in destination) {
if (overwrite) {
if (isObject(destination[key]) && isObject(source[key])) {
mixin(destination[key], source[key], overwrite);
} else {
destination[key] = source[key];
}
}
} else {
destination[key] = source[key];
}
});
}
return destination;
}

type UnknownObject<T extends object> = Record<string | number | symbol, unknown> & { [K in keyof T]: unknown };

export function isObject<T extends object>(value: unknown): value is UnknownObject<T> {
// eslint-disable-next-line no-null/no-null
return typeof value === 'object' && value !== null;
}

export function isUndefined(value: unknown): value is undefined {
return typeof value === 'undefined';
}

0 comments on commit 783274a

Please sign in to comment.