fix lint errors

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 5745afa622
commit 2ee3926f25
4 changed files with 129 additions and 82 deletions

View File

@@ -132,7 +132,9 @@ function initMoveObserver(
const threshold =
typeof sampling.mousemove === 'number' ? sampling.mousemove : 50;
const callbackThreshold =
typeof sampling.mousemoveCallback === 'number' ? sampling.mousemoveCallback : 500;
typeof sampling.mousemoveCallback === 'number'
? sampling.mousemoveCallback
: 500;
let positions: mousePosition[] = [];
let timeBaseline: number | null;
@@ -261,18 +263,18 @@ function initScrollObserver(
function initViewportResizeObserver(
cb: viewportResizeCallback,
): listenerHandler {
let last_h = -1;
let last_w = -1;
let lastH = -1;
let lastW = -1;
const updateDimension = throttle(() => {
const height = getWindowHeight();
const width = getWindowWidth();
if (last_h !== height || last_w != width) {
if (lastH !== height || lastW !== width) {
cb({
width: Number(width),
height: Number(height),
});
last_h = height;
last_w = width;
lastH = height;
lastW = width;
}
}, 200);
return on('resize', updateDimension, window);
@@ -562,24 +564,30 @@ function initLogObserver(
logOptions: LogRecordOptions,
): listenerHandler {
const logger = logOptions.logger;
if (!logger) return () => {};
if (!logger) {
return () => {};
}
let logCount = 0;
const cancelHandlers: any[] = [];
const cancelHandlers: listenerHandler[] = [];
// add listener to thrown errors
if (logOptions.level!.includes('error')) {
if (window) {
const originalOnError = window.onerror;
// tslint:disable-next-line:no-any
window.onerror = (...args: any[]) => {
originalOnError && originalOnError.apply(this, args);
let stack: Array<string> = [];
if (args[args.length - 1] instanceof Error)
if (originalOnError) {
originalOnError.apply(this, args);
}
let stack: string[] = [];
if (args[args.length - 1] instanceof Error) {
// 0(the second parameter) tells parseStack that every stack in Error is useful
stack = parseStack(args[args.length - 1].stack, 0);
}
const payload = [stringify(args[0], logOptions.stringifyOptions)];
cb({
level: 'error',
trace: stack,
payload: payload,
payload,
});
};
cancelHandlers.push(() => {
@@ -587,8 +595,9 @@ function initLogObserver(
});
}
}
for (const levelType of logOptions.level!)
for (const levelType of logOptions.level!) {
cancelHandlers.push(replace(logger, levelType));
}
return () => {
cancelHandlers.forEach((h) => h());
};
@@ -598,10 +607,13 @@ function initLogObserver(
* @param logger the logger object such as Console
* @param level the name of log function to be replaced
*/
function replace(logger: Logger, level: LogLevel) {
if (!logger[level]) return () => {};
function replace(_logger: Logger, level: LogLevel) {
if (!_logger[level]) {
return () => {};
}
// replace the logger.{level}. return a restore function
return patch(logger, level, (original) => {
return patch(_logger, level, (original) => {
// tslint:disable-next-line:no-any
return (...args: any[]) => {
original.apply(this, args);
try {
@@ -610,13 +622,13 @@ function initLogObserver(
stringify(s, logOptions.stringifyOptions),
);
logCount++;
if (logCount < logOptions.lengthThreshold!)
if (logCount < logOptions.lengthThreshold!) {
cb({
level: level,
level,
trace: stack,
payload: payload,
payload,
});
else if (logCount === logOptions.lengthThreshold)
} else if (logCount === logOptions.lengthThreshold) {
// notify the user
cb({
level: 'warn',
@@ -625,6 +637,7 @@ function initLogObserver(
stringify('The number of log records reached the threshold.'),
],
});
}
} catch (error) {
original('rrweb logger error:', error, ...args);
}
@@ -639,7 +652,7 @@ function initLogObserver(
function parseStack(
stack: string | undefined,
omitDepth: number = 1,
): Array<string> {
): string[] {
let stacks: string[] = [];
if (stack) {
stacks = stack

View File

@@ -1,3 +1,4 @@
// tslint:disable:no-any no-bitwise forin
/**
* this file is used to serialize log message to string
*
@@ -14,18 +15,21 @@ function pathToSelector(node: HTMLElement): string | '' {
return '';
}
var path = '';
let path = '';
while (node.parentElement) {
var name = node.localName;
if (!name) break;
let name = node.localName;
if (!name) {
break;
}
name = name.toLowerCase();
var parent = node.parentElement;
let parent = node.parentElement;
var domSiblings = [];
let domSiblings = [];
if (parent.children && parent.children.length > 0) {
for (var i = 0; i < parent.children.length; i++) {
var sibling = parent.children[i];
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < parent.children.length; i++) {
let sibling = parent.children[i];
if (sibling.localName && sibling.localName.toLowerCase) {
if (sibling.localName.toLowerCase() === name) {
domSiblings.push(sibling);
@@ -56,45 +60,55 @@ export function stringify(
numOfKeysLimit: 50,
};
Object.assign(options, stringifyOptions);
let stack: any[] = [],
keys: any[] = [];
const stack: any[] = [];
const keys: any[] = [];
return JSON.stringify(obj, function (key, value) {
/**
* forked from https://github.com/moll/json-stringify-safe/blob/master/stringify.js
* to deCycle the object
*/
if (stack.length > 0) {
var thisPos = stack.indexOf(this);
const thisPos = stack.indexOf(this);
~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
if (~stack.indexOf(value)) {
if (stack[0] === value) value = '[Circular ~]';
else
if (stack[0] === value) {
value = '[Circular ~]';
} else {
value =
'[Circular ~.' +
keys.slice(0, stack.indexOf(value)).join('.') +
']';
}
}
} else stack.push(value);
} else {
stack.push(value);
}
/* END of the FORK */
if (value === null || value === undefined) return value;
if (value === null || value === undefined) {
return value;
}
if (shouldToString(value)) {
return toString(value);
}
if (value instanceof Event) {
const eventResult: any = {};
for (const key in value) {
const eventValue = (value as any)[key];
if (Array.isArray(eventValue))
eventResult[key] = pathToSelector(
for (const eventKey in value) {
const eventValue = (value as any)[eventKey];
if (Array.isArray(eventValue)) {
eventResult[eventKey] = pathToSelector(
eventValue.length ? eventValue[0] : null,
);
else eventResult[key] = eventValue;
} else {
eventResult[eventKey] = eventValue;
}
}
return eventResult;
} else if (value instanceof Node) {
if (value instanceof HTMLElement) return value ? value.outerHTML : '';
if (value instanceof HTMLElement) {
return value ? value.outerHTML : '';
}
return value.nodeName;
}
return value;
@@ -103,21 +117,24 @@ export function stringify(
/**
* whether we should call toString function of this object
*/
function shouldToString(obj: object): boolean {
function shouldToString(_obj: object): boolean {
if (
typeof obj === 'object' &&
Object.keys(obj).length > options.numOfKeysLimit
)
typeof _obj === 'object' &&
Object.keys(_obj).length > options.numOfKeysLimit
) {
return true;
if (typeof obj === 'function') return true;
}
if (typeof _obj === 'function') {
return true;
}
return false;
}
/**
* limit the toString() result according to option
*/
function toString(obj: object): string {
let str = obj.toString();
function toString(_obj: object): string {
let str = _obj.toString();
if (options.stringLengthLimit && str.length > options.stringLengthLimit) {
str = `${str.slice(0, options.stringLengthLimit)}...`;
}