Skip to content
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

refactor: eliminate eslint errors #920

Merged
merged 6 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ module.exports = {
plugins: ['@typescript-eslint', 'eslint-plugin-tsdoc'],
rules: {
'tsdoc/syntax': 'warn',
'@typescript-eslint/unbound-method': 'off',
YunFeng0817 marked this conversation as resolved.
Show resolved Hide resolved
'@typescript-eslint/no-unsafe-member-access': 'off',
YunFeng0817 marked this conversation as resolved.
Show resolved Hide resolved
'@typescript-eslint/restrict-template-expressions': 'off',
},
};
2 changes: 1 addition & 1 deletion packages/rrweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@xstate/fsm": "^1.4.0",
"base64-arraybuffer": "^1.0.1",
"fflate": "^0.4.4",
"mitt": "^1.1.3",
"mitt": "^3.0.0",
"rrdom": "^0.1.2",
"rrweb-snapshot": "^1.1.14"
}
Expand Down
21 changes: 5 additions & 16 deletions packages/rrweb/src/plugins/console/record/error-stack-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,9 @@ export class StackFrame {
toString() {
const lineNumber = this.lineNumber || '';
const columnNumber = this.columnNumber || '';
if (this.functionName) {
return (
this.functionName +
' (' +
this.fileName +
':' +
lineNumber +
':' +
columnNumber +
')'
);
}
return this.fileName + ':' + lineNumber + ':' + columnNumber;
if (this.functionName)
return `${this.functionName} (${this.fileName}:${lineNumber}:${columnNumber})`;
return `${this.fileName}:${lineNumber}:${columnNumber}`;
}
}

Expand All @@ -55,18 +45,17 @@ const SAFARI_NATIVE_CODE_REGEXP = /^(eval@)?(\[native code])?$/;
export const ErrorStackParser = {
/**
* Given an Error object, extract the most information from it.
*
* @param {Error} error object
* @return {Array} of StackFrames
*/
parse: function (error: Error): StackFrame[] {
// https://github.com/rrweb-io/rrweb/issues/782
if (!error) {
return [];
}
if (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
typeof error.stacktrace !== 'undefined' ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
typeof error['opera#sourceloc'] !== 'undefined'
) {
Expand Down
116 changes: 55 additions & 61 deletions packages/rrweb/src/plugins/console/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,6 @@ export type LogData = {

type logCallback = (p: LogData) => void;

export type LogLevel =
| 'assert'
| 'clear'
| 'count'
| 'countReset'
| 'debug'
| 'dir'
| 'dirxml'
| 'error'
| 'group'
| 'groupCollapsed'
| 'groupEnd'
| 'info'
| 'log'
| 'table'
| 'time'
| 'timeEnd'
| 'timeLog'
| 'trace'
| 'warn';

/* fork from interface Console */
// all kinds of console functions
export type Logger = {
Expand All @@ -104,13 +83,24 @@ export type Logger = {
warn?: typeof console.warn;
};

export type LogLevel = keyof Logger;

function initLogObserver(
cb: logCallback,
win: IWindow, // top window or in an iframe
logOptions: LogRecordOptions,
options: LogRecordOptions,
): listenerHandler {
const logOptions = (options
? Object.assign({}, defaultLogOptions, options)
: defaultLogOptions) as {
level: LogLevel[];
lengthThreshold: number;
stringifyOptions?: StringifyOptions;
logger: Logger | 'console';
};
const loggerType = logOptions.logger;
if (!loggerType) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
}
let logger: Logger;
Expand All @@ -122,10 +112,11 @@ function initLogObserver(
let logCount = 0;
const cancelHandlers: listenerHandler[] = [];
// add listener to thrown errors
if (logOptions.level!.includes('error')) {
if (logOptions.level.includes('error')) {
if (window) {
const errorHandler = (event: ErrorEvent) => {
const { message, error } = event;
const message = event.message,
error = event.error as Error;
const trace: string[] = ErrorStackParser.parse(
error,
).map((stackFrame: StackFrame) => stackFrame.toString());
Expand All @@ -142,7 +133,7 @@ function initLogObserver(
});
}
}
for (const levelType of logOptions.level!) {
for (const levelType of logOptions.level) {
cancelHandlers.push(replace(logger, levelType));
}
return () => {
Expand All @@ -151,46 +142,51 @@ function initLogObserver(

/**
* replace the original console function and record logs
* @param logger the logger object such as Console
* @param level the name of log function to be replaced
* @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]) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
}
// replace the logger.{level}. return a restore function
return patch(_logger, level, (original) => {
return (...args: Array<unknown>) => {
original.apply(this, args);
try {
const trace = ErrorStackParser.parse(new Error())
.map((stackFrame: StackFrame) => stackFrame.toString())
.splice(1); // splice(1) to omit the hijacked log function
const payload = args.map((s) =>
stringify(s, logOptions.stringifyOptions),
);
logCount++;
if (logCount < logOptions.lengthThreshold!) {
cb({
level,
trace,
payload,
});
} else if (logCount === logOptions.lengthThreshold) {
// notify the user
cb({
level: 'warn',
trace: [],
payload: [
stringify('The number of log records reached the threshold.'),
],
});
return patch(
_logger,
level,
(original: (...args: Array<unknown>) => void) => {
return (...args: Array<unknown>) => {
original.apply(this, args);
try {
const trace = ErrorStackParser.parse(new Error())
.map((stackFrame: StackFrame) => stackFrame.toString())
.splice(1); // splice(1) to omit the hijacked log function
const payload = args.map((s) =>
stringify(s, logOptions.stringifyOptions),
);
logCount++;
if (logCount < logOptions.lengthThreshold) {
cb({
level,
trace,
payload,
});
} else if (logCount === logOptions.lengthThreshold) {
// notify the user
cb({
level: 'warn',
trace: [],
payload: [
stringify('The number of log records reached the threshold.'),
],
});
}
} catch (error) {
original('rrweb logger error:', error, ...args);
}
} catch (error) {
original('rrweb logger error:', error, ...args);
}
};
});
};
},
);
}
}

Expand All @@ -201,7 +197,5 @@ export const getRecordConsolePlugin: (
) => RecordPlugin = (options) => ({
name: PLUGIN_NAME,
observer: initLogObserver,
options: options
? Object.assign({}, defaultLogOptions, options)
: defaultLogOptions,
options: options,
});
Loading