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 all 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
18 changes: 17 additions & 1 deletion packages/rrdom-nodejs/src/document-nodejs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { NodeType as RRNodeType } from 'rrweb-snapshot';
import type { NWSAPI } from 'nwsapi';
import type { CSSStyleDeclaration as CSSStyleDeclarationType } from 'cssstyle';
Expand All @@ -14,8 +15,11 @@ import {
IRRDocument,
CSSStyleDeclaration,
} from 'rrdom';
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const nwsapi = require('nwsapi');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const cssom = require('cssom');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const cssstyle = require('cssstyle');

export class RRNode extends BaseRRNode {}
Expand All @@ -37,6 +41,7 @@ export class RRDocument
private _nwsapi: NWSAPI;
get nwsapi() {
if (!this._nwsapi) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
this._nwsapi = nwsapi({
document: (this as unknown) as Document,
DOMException: (null as unknown) as new (
Expand All @@ -53,26 +58,31 @@ export class RRDocument
return this._nwsapi;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get documentElement(): RRElement | null {
return super.documentElement as RRElement | null;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get body(): RRElement | null {
return super.body as RRElement | null;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get head() {
return super.head as RRElement | null;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get implementation(): RRDocument {
return this;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get firstElementChild(): RRElement | null {
return this.documentElement;
Expand Down Expand Up @@ -109,8 +119,11 @@ export class RRDocument
}

createDocument(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_namespace: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_qualifiedName: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_doctype?: DocumentType | null,
) {
return new RRDocument();
Expand Down Expand Up @@ -191,6 +204,7 @@ export class RRElement extends BaseRRElementImpl(RRNode) {
private _style: CSSStyleDeclarationType;
constructor(tagName: string) {
super(tagName);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
this._style = new cssstyle.CSSStyleDeclaration();
const style = this._style;
Object.defineProperty(this.attributes, 'style', {
Expand All @@ -203,6 +217,7 @@ export class RRElement extends BaseRRElementImpl(RRNode) {
});
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get style() {
return (this._style as unknown) as CSSStyleDeclaration;
Expand Down Expand Up @@ -311,7 +326,7 @@ export class RRImageElement extends RRElement {
src: string;
width: number;
height: number;
onload: ((this: GlobalEventHandlers, ev: Event) => any) | null;
onload: ((this: GlobalEventHandlers, ev: Event) => unknown) | null;
}

export class RRMediaElement extends BaseRRMediaElementImpl(RRElement) {}
Expand All @@ -334,6 +349,7 @@ export class RRStyleElement extends RRElement {
for (const child of this.childNodes)
if (child.RRNodeType === RRNodeType.Text)
result += (child as RRText).textContent;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
this._sheet = cssom.parse(result);
}
return this._sheet;
Expand Down
13 changes: 8 additions & 5 deletions packages/rrdom-nodejs/src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { RRDocument, RRNode } from './document-nodejs';
*/
export function polyfillPerformance() {
if (typeof window !== 'undefined' || 'performance' in global) return;
((global as Window & typeof globalThis)
.performance as unknown) = require('perf_hooks').performance;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-var-requires
const performance = require('perf_hooks').performance;
((global as Window & typeof globalThis).performance as unknown) = performance;
}

/**
Expand All @@ -22,11 +23,11 @@ export function polyfillRAF() {
INTERVAL = 1_000 / FPS;
let timeoutHandle: NodeJS.Timeout | null = null,
rafCount = 0,
requests = Object.create(null);
requests = Object.create(null) as Record<string, (time: number) => void>;

function onFrameTimer() {
const currentRequests = requests;
requests = Object.create(null);
requests = Object.create(null) as Record<string, (time: number) => void>;
timeoutHandle = null;
Object.keys(currentRequests).forEach(function (id) {
const request = currentRequests[id];
Expand Down Expand Up @@ -63,7 +64,9 @@ export function polyfillRAF() {
*/
export function polyfillEvent() {
if (typeof Event !== 'undefined') return;
(global.Event as unknown) = function () {};
(global.Event as unknown) = function () {
//
};
}

/**
Expand Down
9 changes: 3 additions & 6 deletions packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export function diff(
const newMediaRRElement = newRRElement as RRMediaElement;
if (newMediaRRElement.paused !== undefined)
newMediaRRElement.paused
? oldMediaElement.pause()
: oldMediaElement.play();
? void oldMediaElement.pause()
: void oldMediaElement.play();
if (newMediaRRElement.muted !== undefined)
oldMediaElement.muted = newMediaRRElement.muted;
if (newMediaRRElement.volume !== undefined)
Expand Down Expand Up @@ -383,10 +383,7 @@ export function createOrGetNode(
let tagName = (rrNode as IRRElement).tagName.toLowerCase();
tagName = SVGTagMap[tagName] || tagName;
if (sn && 'isSVG' in sn && sn?.isSVG) {
node = document.createElementNS(
NAMESPACES['svg'],
(rrNode as IRRElement).tagName.toLowerCase(),
);
node = document.createElementNS(NAMESPACES['svg'], tagName);
} else node = document.createElement((rrNode as IRRElement).tagName);
break;
}
Expand Down
19 changes: 15 additions & 4 deletions packages/rrdom/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export interface IRRCDATASection extends IRRNode {
}

type ConstrainedConstructor<T = Record<string, unknown>> = new (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
) => T;

Expand All @@ -138,7 +139,8 @@ export class BaseRRNode implements IRRNode {
public readonly nodeName: string;
public readonly RRNodeType: RRNodeType;

constructor(...args: any[]) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
constructor(..._args: any[]) {
//
}

Expand Down Expand Up @@ -166,19 +168,22 @@ export class BaseRRNode implements IRRNode {
return false;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public appendChild(_newChild: IRRNode): IRRNode {
throw new Error(
`RRDomException: Failed to execute 'appendChild' on 'RRNode': This RRNode type does not support this method.`,
);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public insertBefore(_newChild: IRRNode, _refChild: IRRNode | null): IRRNode {
throw new Error(
`RRDomException: Failed to execute 'insertBefore' on 'RRNode': This RRNode type does not support this method.`,
);
}

public removeChild(node: IRRNode): IRRNode {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public removeChild(_node: IRRNode): IRRNode {
throw new Error(
`RRDomException: Failed to execute 'removeChild' on 'RRNode': This RRNode type does not support this method.`,
);
Expand Down Expand Up @@ -306,8 +311,8 @@ export function BaseRRDocumentImpl<
/**
* Adhoc implementation for setting xhtml namespace in rebuilt.ts (rrweb-snapshot).
* There are two lines used this function:
* 1. doc.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">')
* 2. doc.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">')
* 1. doc.write('\<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""\>')
* 2. doc.write('\<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ""\>')
*/
public write(content: string) {
let publicId;
Expand All @@ -329,8 +334,11 @@ export function BaseRRDocumentImpl<
}

createDocument(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_namespace: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_qualifiedName: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_doctype?: DocumentType | null,
): IRRDocument {
return new BaseRRDocument();
Expand Down Expand Up @@ -542,12 +550,14 @@ export function BaseRRElementImpl<
return node;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public attachShadow(_init: ShadowRootInit): IRRElement {
const shadowRoot = this.ownerDocument.createElement('SHADOWROOT');
this.shadowRoot = shadowRoot;
return shadowRoot;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public dispatchEvent(_event: Event) {
return true;
}
Expand All @@ -570,6 +580,7 @@ export function BaseRRMediaElementImpl<
public volume?: number;
public paused?: boolean;
public muted?: boolean;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
attachShadow(_init: ShadowRootInit): IRRElement {
throw new Error(
`RRDomException: Failed to execute 'attachShadow' on 'RRElement': This RRElement does not support attachShadow`,
Expand Down
55 changes: 44 additions & 11 deletions packages/rrdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
export class RRDocument extends BaseRRDocumentImpl(RRNode) {
// In the rrweb replayer, there are some unserialized nodes like the element that stores the injected style rules.
// These unserialized nodes may interfere the execution of the diff algorithm.
// The id of serialized node is larger than 0. So this value ​​less than 0 is used as id for these unserialized nodes.
// The id of serialized node is larger than 0. So this value less than 0 is used as id for these unserialized nodes.
private _unserializedId = -1;

/**
Expand All @@ -57,8 +57,11 @@ export class RRDocument extends BaseRRDocumentImpl(RRNode) {
}

createDocument(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_namespace: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_qualifiedName: string | null,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_doctype?: DocumentType | null,
) {
return new RRDocument();
Expand Down Expand Up @@ -201,9 +204,9 @@ function getValidTagName(element: HTMLElement): string {

/**
* Build a RRNode from a real Node.
* @param node the real Node
* @param rrdom the RRDocument
* @param domMirror the NodeMirror that records the real document tree
* @param node - the real Node
* @param rrdom - the RRDocument
* @param domMirror - the NodeMirror that records the real document tree
* @returns the built RRNode
*/
export function buildFromNode(
Expand All @@ -225,15 +228,16 @@ export function buildFromNode(
| 'CSS1Compat';
}
break;
case NodeType.DOCUMENT_TYPE_NODE:
case NodeType.DOCUMENT_TYPE_NODE: {
const documentType = node as DocumentType;
rrNode = rrdom.createDocumentType(
documentType.name,
documentType.publicId,
documentType.systemId,
);
break;
case NodeType.ELEMENT_NODE:
}
case NodeType.ELEMENT_NODE: {
const elementNode = node as HTMLElement;
const tagName = getValidTagName(elementNode);
rrNode = rrdom.createElement(tagName);
Expand All @@ -248,6 +252,7 @@ export function buildFromNode(
* Because if these values are changed later, the mutation will be applied through the batched input events on its RRElement after the diff algorithm is executed.
*/
break;
}
case NodeType.TEXT_NODE:
rrNode = rrdom.createTextNode((node as Text).textContent || '');
break;
Expand Down Expand Up @@ -280,9 +285,9 @@ export function buildFromNode(

/**
* Build a RRDocument from a real document tree.
* @param dom the real document tree
* @param domMirror the NodeMirror that records the real document tree
* @param rrdom the rrdom object to be constructed
* @param dom - the real document tree
* @param domMirror - the NodeMirror that records the real document tree
* @param rrdom - the rrdom object to be constructed
* @returns the build rrdom
*/
export function buildFromDom(
Expand Down Expand Up @@ -390,7 +395,7 @@ export class Mirror implements IMirror<RRNode> {

/**
* Get a default serializedNodeWithId value for a RRNode.
* @param id the serialized id to assign
* @param id - the serialized id to assign
*/
export function getDefaultSN(node: IRRNode, id: number): serializedNodeWithId {
switch (node.RRNodeType) {
Expand All @@ -400,7 +405,7 @@ export function getDefaultSN(node: IRRNode, id: number): serializedNodeWithId {
type: node.RRNodeType,
childNodes: [],
};
case RRNodeType.DocumentType:
case RRNodeType.DocumentType: {
const doctype = node as IRRDocumentType;
return {
id,
Expand All @@ -409,6 +414,7 @@ export function getDefaultSN(node: IRRNode, id: number): serializedNodeWithId {
publicId: doctype.publicId,
systemId: doctype.systemId,
};
}
case RRNodeType.Element:
return {
id,
Expand Down Expand Up @@ -438,6 +444,33 @@ export function getDefaultSN(node: IRRNode, id: number): serializedNodeWithId {
}
}

/**
* Print the RRDom as a string.
* @param rootNode - the root node of the RRDom tree
* @param mirror - a rrweb or rrdom Mirror
* @returns printed string
*/
export function printRRDom(rootNode: IRRNode, mirror: IMirror<IRRNode>) {
return walk(rootNode, mirror, '');
}
function walk(node: IRRNode, mirror: IMirror<IRRNode>, blankSpace: string) {
let printText = `${blankSpace}${mirror.getId(node)} ${node.toString()}\n`;
if (node.RRNodeType === RRNodeType.Element) {
const element = node as IRRElement;
if (element.shadowRoot)
printText += walk(element.shadowRoot, mirror, blankSpace + ' ');
}
for (const child of node.childNodes)
printText += walk(child, mirror, blankSpace + ' ');
if (node.nodeName === 'IFRAME')
printText += walk(
(node as RRIFrameElement).contentDocument,
mirror,
blankSpace + ' ',
);
return printText;
}

export { RRNode };

export {
Expand Down
2 changes: 1 addition & 1 deletion packages/rrdom/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const camelizeRE = /-([a-z])/g;
const CUSTOM_PROPERTY_REGEX = /^--[a-zA-Z0-9-]+$/;
export const camelize = (str: string): string => {
if (CUSTOM_PROPERTY_REGEX.test(str)) return str;
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
return str.replace(camelizeRE, (_, c: string) => (c ? c.toUpperCase() : ''));
};

/**
Expand Down
Loading