diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..de4d1f00 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +dist +node_modules diff --git a/.eslintrc b/.eslintrc index 24fb6165..be78317d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,5 @@ { + "root": true, "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": 2020, @@ -9,5 +10,38 @@ "prettier/@typescript-eslint", "plugin:prettier/recommended" ], - "rules": {} + "rules": { + "@typescript-eslint/ban-types": [ + "off" + ], + "@typescript-eslint/no-empty-function": [ + "off" + ], + "@typescript-eslint/no-this-alias": [ + "error", + { + "allowDestructuring": true, // Allow `const { props, state } = this`; false by default + "allowedNames": [ + "self", + "context" + ] // Allow `const self = this`; `[]` by default + } + ], + "@typescript-eslint/no-explicit-any": [ + "off" + // "error", + // { + // "ignoreRestArgs": true + // } + ], + "prefer-rest-params": [ + "off" + ], + "@typescript-eslint/no-non-null-assertion": [ + "off" + ], + "@typescript-eslint/explicit-module-boundary-types": [ + "off" + ] + } } diff --git a/package-lock.json b/package-lock.json index e90e41b0..467e4cc9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10019,9 +10019,9 @@ } }, "typescript": { - "version": "4.0.0-beta", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.0-beta.tgz", - "integrity": "sha512-d3s/CogGtB2uPZ2Z8ts6eoUxxyB9PH3R27/UrzvpthuOvpCg4FWWnBbBiqJ0K4eu6eTlgmLiqQkh2dquReJweA==", + "version": "3.9.7", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", + "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", "dev": true }, "ua-parser-js": { diff --git a/package.json b/package.json index 624d7b33..57f3866a 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "doc": "npx typedoc", "embed": "cp packages/timecat/dist/timecatjs.min.js ../TimeCatChrome/src/assets/", "count": "find ./packages -type f -not -path **/*.spec.ts -not -path **/global.* -not -path './node_modules/*' -not -path '**/dist/*' -name '*.ts' | xargs wc -l", - "lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix" + "lint": "eslint 'packages/**/*.{js,ts}' --quiet --fix" }, "devDependencies": { "@microsoft/api-extractor": "^7.8.9", @@ -46,7 +46,6 @@ "@types/node": "^13.9.2", "@types/pako": "^1.0.1", "@types/smoothscroll-polyfill": "^0.3.1", - "@types/socket.io-client": "^1.4.32", "@zerollup/ts-transform-paths": "^1.7.17", "diff": "^4.0.2", "execa": "^4.0.0", @@ -65,11 +64,16 @@ "rollup-plugin-typescript2": "^0.26.0", "rollup-plugin-visualizer": "^4.0.4", "smoothscroll-polyfill": "^0.4.4", - "socket.io": "^2.3.0", "ts-jest": "^26.1.0", "ttypescript": "^1.5.10", "typedoc": "^0.17.7", - "typescript": "^4.0.0-beta" + "typescript": "^3.9.7", + "@typescript-eslint/eslint-plugin": "^3.9.0", + "@typescript-eslint/parser": "^3.9.0", + "eslint": "^7.6.0", + "eslint-config-prettier": "^6.11.0", + "eslint-plugin-prettier": "^3.1.4", + "prettier": "^2.0.5" }, "repository": { "type": "git", @@ -82,12 +86,5 @@ "files": [ "lib" ], - "dependencies": { - "@typescript-eslint/eslint-plugin": "^3.9.0", - "@typescript-eslint/parser": "^3.9.0", - "eslint": "^7.6.0", - "eslint-config-prettier": "^6.11.0", - "eslint-plugin-prettier": "^3.1.4", - "prettier": "^2.0.5" - } + "dependencies": {} } diff --git a/packages/player/src/dom.ts b/packages/player/src/dom.ts index 21430dfa..2aa3b7a2 100644 --- a/packages/player/src/dom.ts +++ b/packages/player/src/dom.ts @@ -97,7 +97,7 @@ export async function updateDom(this: PlayerComponent, Record: RecordData) { case RecordType.SCROLL: { const { top, left, id } = data as ScrollWatcherData - let target = id ? (nodeStore.getNode(id) as HTMLElement) : this.c.sandBoxDoc.documentElement + const target = id ? (nodeStore.getNode(id) as HTMLElement) : this.c.sandBoxDoc.documentElement const curTop = target.scrollTop @@ -274,7 +274,7 @@ export async function updateDom(this: PlayerComponent, Record: RecordData) { if (src) { const image = new Image() image.src = src - image.onload = function(this: HTMLImageElement) { + image.onload = function (this: HTMLImageElement) { ctx.drawImage(this, 0, 0) } } else { diff --git a/packages/player/src/keyboard.ts b/packages/player/src/keyboard.ts index 6eb6955d..4446b329 100644 --- a/packages/player/src/keyboard.ts +++ b/packages/player/src/keyboard.ts @@ -35,7 +35,7 @@ export class KeyboardComponent { this.detectWindowIsActive() } - dispatchPlay(speed: number = 0) { + dispatchPlay(speed = 0) { reduxStore.dispatch({ type: PlayerTypes.SPEED, data: { diff --git a/packages/player/src/player.ts b/packages/player/src/player.ts index b7cba853..6ed86f34 100644 --- a/packages/player/src/player.ts +++ b/packages/player/src/player.ts @@ -90,13 +90,7 @@ export class PlayerComponent { } if (this.audioData.src) { - this.audioBlobUrl = - location.href - .split('/') - .slice(0, -1) - .join('/') + - '/' + - this.audioData.src + this.audioBlobUrl = location.href.split('/').slice(0, -1).join('/') + '/' + this.audioData.src } else { const bufferStrList = this.audioData.bufferStrList if (!bufferStrList.length) { diff --git a/packages/record/src/audio/audio-recorder.ts b/packages/record/src/audio/audio-recorder.ts index c234056b..63f47d4d 100644 --- a/packages/record/src/audio/audio-recorder.ts +++ b/packages/record/src/audio/audio-recorder.ts @@ -37,7 +37,7 @@ export class Recorder { this.mediaNode = this.audioContext.createMediaStreamSource(this.mediaStream) - let createScript = this.audioContext.createScriptProcessor + const createScript = this.audioContext.createScriptProcessor this.processNode = createScript.call(this.audioContext, 4096, this.opts.channelCount, this.opts.channelCount) diff --git a/packages/record/src/watchers/canvas.ts b/packages/record/src/watchers/canvas.ts index b5f305b2..91ad41b5 100644 --- a/packages/record/src/watchers/canvas.ts +++ b/packages/record/src/watchers/canvas.ts @@ -40,7 +40,7 @@ export class CanvasWatcher extends Watcher { const id = self.getNodeId(this.canvas)! return typeof method === 'function' - ? function() { + ? function () { const args = [...arguments] if (name === 'drawImage') { args[0] = id @@ -51,7 +51,7 @@ export class CanvasWatcher extends Watcher { } : null }, - set: function(value: any) { + set: function (value: any) { const id = self.getNodeId(this.canvas)! self.aggregateDataEmitter(id, name, value) @@ -82,7 +82,7 @@ export class CanvasWatcher extends Watcher { const blockInstances = [CanvasGradient] - return function(this: any, id: number, prop: Prop, args: any) { + return function (this: any, id: number, prop: Prop, args: any) { const context = this function emitData(id: number) { diff --git a/packages/record/src/watchers/dom.ts b/packages/record/src/watchers/dom.ts index 21fbeda8..0db165c3 100644 --- a/packages/record/src/watchers/dom.ts +++ b/packages/record/src/watchers/dom.ts @@ -128,7 +128,7 @@ export class DOMWatcher extends Watcher { const isParentSVG = parentVn && parentVn.extra.isSVG - let vn = addedSiblingMap.get(node)! + const vn = addedSiblingMap.get(node)! if (isParentSVG && isVNode(vn)) { ;(vn as VNode).extra.isSVG = true diff --git a/packages/record/src/watchers/form-element.ts b/packages/record/src/watchers/form-element.ts index a6c8a274..a655adef 100644 --- a/packages/record/src/watchers/form-element.ts +++ b/packages/record/src/watchers/form-element.ts @@ -37,9 +37,9 @@ export class FormElementWatcher extends Watcher { const inputType = target.getAttribute('type') || 'text' let key = 'value' - let value: any = target.value || '' + const value: any = target.value || '' let newValue: any = '' - let patches: ReturnType = [] + const patches: ReturnType = [] if (inputType === 'checkbox' || inputType === 'radio') { if (eventType === 'input') { @@ -115,7 +115,7 @@ export class FormElementWatcher extends Watcher { const [target, key] = item const original = context.Object.getOwnPropertyDescriptor(target, key) context.Object.defineProperty(target, key, { - set: function(value: string | boolean) { + set: function (value: string | boolean) { setTimeout(() => { handleEvent.call(this, key, value) }) diff --git a/packages/record/src/watchers/location.ts b/packages/record/src/watchers/location.ts index fa0ed80f..baf57a54 100644 --- a/packages/record/src/watchers/location.ts +++ b/packages/record/src/watchers/location.ts @@ -27,7 +27,7 @@ export class LocationWatcher extends Watcher { kidnapLocation(type: 'pushState' | 'replaceState') { const original = this.context.history[type] - return function(this: any) { + return function (this: any) { const e = new Event(type) e.arguments = arguments this.context.dispatchEvent(e) diff --git a/packages/share/src/index.ts b/packages/share/src/index.ts index 325c48d9..aa3b30fe 100644 --- a/packages/share/src/index.ts +++ b/packages/share/src/index.ts @@ -118,7 +118,7 @@ export interface DOMRecord { data: DOMWatcherData time: string } -export interface DOMWatcherData extends DOMUpdateDataType {} +export type DOMWatcherData = DOMUpdateDataType export interface AttributesUpdateData { id: number diff --git a/packages/utils/src/redux/create-store.ts b/packages/utils/src/redux/create-store.ts index ea116a57..c05e34f4 100644 --- a/packages/utils/src/redux/create-store.ts +++ b/packages/utils/src/redux/create-store.ts @@ -57,13 +57,13 @@ export function createStore(reducer: Reducer, initState: States = {}) { function getState(): StateMap function getState(name?: T): StateMap[T] - + function getState(name?: T) { const s = state as StateMap if (name) { return s[name] as StateMap[T] } - return s + return s } function getTypeInTopics(type: TopicName): TopicName | undefined { @@ -72,7 +72,7 @@ export function createStore(reducer: Reducer, initState: States = {}) { progress: Object.keys(ProgressTypes) } - for (let [key, enums] of Object.entries(topics)) { + for (const [key, enums] of Object.entries(topics)) { if (enums.includes(type)) { return key as TopicName } diff --git a/packages/utils/src/redux/reducers/player.ts b/packages/utils/src/redux/reducers/player.ts index 99e2d97a..80bd5aac 100644 --- a/packages/utils/src/redux/reducers/player.ts +++ b/packages/utils/src/redux/reducers/player.ts @@ -1,4 +1,4 @@ -let initState = { +const initState = { speed: 0 } diff --git a/packages/utils/src/tools/dom.ts b/packages/utils/src/tools/dom.ts index 5ecdd7e6..2c2b3834 100644 --- a/packages/utils/src/tools/dom.ts +++ b/packages/utils/src/tools/dom.ts @@ -76,7 +76,7 @@ export function completeAttrHref(str: string, node?: Element) { const doc = node.getRootNode() as Document const context = doc.defaultView as Window - let { href, path } = context?.__ReplayLocation__ || {} + const { href, path } = context?.__ReplayLocation__ || {} if (path && href) { const relationHref = new URL(path, href) diff --git a/packages/utils/src/tools/output.ts b/packages/utils/src/tools/output.ts index b282582e..1ed16727 100644 --- a/packages/utils/src/tools/output.ts +++ b/packages/utils/src/tools/output.ts @@ -112,7 +112,7 @@ async function initOptions(html: Document, exportOptions: ExportOptions) { async function injectScripts(html: Document, scripts?: ScriptItem[]) { if (scripts) { - for (let scriptItem of scripts) { + for (const scriptItem of scripts) { const { src, name } = scriptItem let scriptContent = src const script = document.createElement('script') @@ -193,7 +193,7 @@ async function injectData(html: Document, exportOptions: ExportOptions) { const jsonStrData = JSON.stringify(extractedData) const zipArray = pako.gzip(jsonStrData) - let outputStr: string = '' + let outputStr = '' for (let i = 0; i < zipArray.length; i++) { let num = zipArray[i] diff --git a/packages/utils/src/tools/scroll.ts b/packages/utils/src/tools/scroll.ts index 933e8144..56cb777a 100644 --- a/packages/utils/src/tools/scroll.ts +++ b/packages/utils/src/tools/scroll.ts @@ -22,7 +22,7 @@ export function disableScrolling(target: Document) { 'test' as any, () => {}, Object.defineProperty({}, 'passive', { - get: function() { + get: function () { supportsPassive = true } }) diff --git a/packages/utils/src/tools/tool.ts b/packages/utils/src/tools/tool.ts index 2b1870a8..0b070b76 100644 --- a/packages/utils/src/tools/tool.ts +++ b/packages/utils/src/tools/tool.ts @@ -37,10 +37,10 @@ export function objectEquals(x: any, y: any): boolean { // recursive object equality check const p = Object.keys(x) return ( - Object.keys(y).every(function(i) { + Object.keys(y).every(function (i) { return p.indexOf(i) !== -1 }) && - p.every(function(i) { + p.every(function (i) { return objectEquals(x[i], y[i]) }) ) @@ -57,13 +57,13 @@ export function throttle( let timeout: any = null let previous = 0 - const later = function() { + const later = function () { previous = options.leading === false ? 0 : Date.now() timeout = null result = func.apply(context, args) if (!timeout) context = args = null } - return function(this: any) { + return function (this: any) { const now = Date.now() if (!previous && options.leading === false) previous = now const remaining = wait - (now - previous) @@ -99,10 +99,10 @@ export function debounce( ): (this: ThisParameterType, ...args: Parameters) => void { let timeoutId: ReturnType | undefined - return function(this: ThisParameterType, ...args: Parameters) { + return function (this: ThisParameterType, ...args: Parameters) { const context = this - const doLater = function() { + const doLater = function () { timeoutId = undefined if (!options.isImmediate) { func.apply(context, args) diff --git a/packages/utils/src/tools/transform.ts b/packages/utils/src/tools/transform.ts index 48cdd996..e8341bff 100644 --- a/packages/utils/src/tools/transform.ts +++ b/packages/utils/src/tools/transform.ts @@ -1,5 +1,4 @@ import { RecorderOptions } from '@timecat/share' -import { download, isDev } from './common' function encodePCM(bufferData: Float32Array, opts: RecorderOptions) { const { sampleBits } = opts @@ -10,14 +9,14 @@ function encodePCM(bufferData: Float32Array, opts: RecorderOptions) { if (sampleBits === 8) { for (let i = 0; i < bufferData.length; i++, offset++) { - let s = Math.max(-1, Math.min(1, bufferData[i])) + const s = Math.max(-1, Math.min(1, bufferData[i])) let val = s < 0 ? s * 128 : s * 127 val = +val + 128 data.setInt8(offset, val) } } else { for (let i = 0; i < bufferData.length; i++, offset += 2) { - let s = Math.max(-1, Math.min(1, bufferData[i])) + const s = Math.max(-1, Math.min(1, bufferData[i])) data.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, isLittleEndian) } } @@ -29,7 +28,7 @@ export function encodeWAV(data: Float32Array[], opts: RecorderOptions) { const PMC = encodePCM(mergeArray(data), opts) const arrayBuffer = createWavFile(PMC, opts) - let blob = new Blob([arrayBuffer], { + const blob = new Blob([arrayBuffer], { type: 'audio/wav' }) @@ -37,9 +36,9 @@ export function encodeWAV(data: Float32Array[], opts: RecorderOptions) { } function mergeArray(list: Float32Array[]) { - let length = list.length * list[0].length - let data = new Float32Array(length), - offset = 0 + const length = list.length * list[0].length + const data = new Float32Array(length) + let offset = 0 for (let i = 0; i < list.length; i++) { data.set(list[i], offset) offset += list[i].length @@ -49,7 +48,7 @@ function mergeArray(list: Float32Array[]) { function createWavFile(audioData: DataView, { channelCount, sampleBits, sampleRate }: RecorderOptions) { const WAV_HEAD_SIZE = 44 - let buffer = new ArrayBuffer(WAV_HEAD_SIZE + audioData.byteLength) + const buffer = new ArrayBuffer(WAV_HEAD_SIZE + audioData.byteLength) const isLittleEndian = true // control buffer const view = new DataView(buffer) @@ -85,7 +84,7 @@ function createWavFile(audioData: DataView, { channelCount, sampleBits, sampleRa // view.setUint32(40, audioData.length * 2, isLittleEndian) // write PCM - let length = audioData.byteLength + const length = audioData.byteLength let offset = 44 // let volume = 1 for (let i = 0; i < length; i++) { diff --git a/packages/virtual-dom/src/deserialize.ts b/packages/virtual-dom/src/deserialize.ts index ef1c87a2..1916eaac 100644 --- a/packages/virtual-dom/src/deserialize.ts +++ b/packages/virtual-dom/src/deserialize.ts @@ -8,13 +8,13 @@ export function convertVNode(vNode: VNode | VSNode | null, parent?: VNode): Elem } const vs = vNode as VSNode if (vNode.type === Node.COMMENT_NODE) { - return createCommentNode(vs) + return createCommentNode(vs) as Element } if (vNode.type === Node.TEXT_NODE) { if (parent && parent.tag === 'style') { vs.value = completeCssHref(vs.value, parent) } - return createText(vs) + return createText(vs) as Element } const vn = vNode as VNode const output = createNode(vn) @@ -44,7 +44,7 @@ function travel(vNode: VNode, node: Element): void { function createProps(vNode: VNode, node: Element): void { const { props } = vNode.extra if (props) { - for (let [key, val] of Object.entries(props)) { + for (const [key, val] of Object.entries(props)) { if (key === 'scroll') { const { left, top } = val as { top: number; left: number } // TODO @@ -165,16 +165,14 @@ function transformTagName(tag: string) { function createText(vs: VSNode) { const { value, id } = vs - let output: Element | Node - output = document.createTextNode(value) + const output: Node = document.createTextNode(value) nodeStore.updateNode(id, output) - return output as Element + return output } function createCommentNode(vs: VSNode) { const { value, id } = vs - let output: Element | Node - output = document.createComment(value) + const output: Node = document.createComment(value) nodeStore.updateNode(id, output) - return output as Element + return output } diff --git a/packages/virtual-dom/src/dom.ts b/packages/virtual-dom/src/dom.ts index 745be0df..c08e9f73 100644 --- a/packages/virtual-dom/src/dom.ts +++ b/packages/virtual-dom/src/dom.ts @@ -11,8 +11,7 @@ export function setAttribute(node: HTMLElement, name: string, value: string | bo for (const [k, v] of Object.entries(value)) { if (k[0] === '-') { node.style.setProperty(k, v as any) - } - else { + } else { node.style[k as any] = v as any } } diff --git a/packages/virtual-dom/src/serialize.ts b/packages/virtual-dom/src/serialize.ts index edfbd11f..83d5db78 100644 --- a/packages/virtual-dom/src/serialize.ts +++ b/packages/virtual-dom/src/serialize.ts @@ -73,7 +73,7 @@ function getExtra(node: Element, isSVG?: boolean) { } const extraAttr = (attr: Attr) => { - let { name, value } = attr + const { name, value } = attr if (name === 'href' || name === 'src') { if (value.startsWith('#/')) { return []