Skip to content

Commit

Permalink
feat: upgrade eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Oct 14, 2024
1 parent e98c9b1 commit 9048809
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 65 deletions.
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default [
"file-progress/activate": 1,
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/class-methods-use-this": "off",
"@typescript-eslint/max-params": "off"
"@typescript-eslint/max-params": "off",
"@typescript-eslint/no-magic-numbers": "off"
}
}
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"cross-env": "^7.0.3",
"dts-bundle-generator": "^9.5.1",
"eslint": "^8.57.0",
"eslint-config-love": "^64.0.0",
"eslint-config-love": "^87.0.0",
"eslint-plugin-file-progress": "^1.5.0",
"fs-extra": "^11.2.0",
"gh-pages": "^6.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/common/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export default class Action {
private _callbacks: ActionCallback[] = []

subscribe (callback: ActionCallback): void {
const index = this._callbacks.indexOf(callback) ?? -1
const index = this._callbacks.indexOf(callback)
if (index < 0) {
this._callbacks.push(callback)
}
}

unsubscribe (callback?: ActionCallback): void {
if (isFunction(callback)) {
const index = this._callbacks.indexOf(callback) ?? -1
const index = this._callbacks.indexOf(callback)
if (index > -1) {
this._callbacks.splice(index, 1)
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class Canvas {
if (result) {
this._resizeObserver = new ResizeObserver((entries: ResizeObserverEntry[]) => {
const entry = entries.find((entry: ResizeObserverEntry) => entry.target === this._element)
const size = entry?.devicePixelContentBoxSize?.[0]
const size = entry?.devicePixelContentBoxSize[0]
if (isValid(size)) {
this._nextPixelWidth = size.inlineSize
this._nextPixelHeight = size.blockSize
Expand Down Expand Up @@ -136,8 +136,8 @@ export default class Canvas {
}

destroy (): void {
this._resizeObserver?.unobserve(this._element)
this._resizeObserver.unobserve(this._element)
// eslint-disable-next-line @typescript-eslint/no-deprecated
this._mediaQueryList?.removeListener(this._mediaQueryListener)
this._mediaQueryList.removeListener(this._mediaQueryListener)
}
}
41 changes: 27 additions & 14 deletions src/common/SyntheticEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,17 @@ const enum Delay {
PreventFiresTouchEvents = 500,
}

const enum ManhattanDistance {
CancelClick = 5,
// eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
CancelTap = 5,
// eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
DoubleClick = 5,
DoubleTap = 30,
const ManhattanDistance = {
CancelClick: 5,
CancelTap: 5,
DoubleClick: 5,
DoubleTap: 30,
}

const enum MouseEventButton {
Left = 0,
Middle = 1,
Right = 2,
const MouseEventButton = {
Left: 0,
Middle: 1,
Right: 2,
}

export const TOUCH_MIN_RADIUS = 10
Expand Down Expand Up @@ -231,18 +229,21 @@ export default class SyntheticEvent {
this._unsubscribeMouseWheel?.()
this._unsubscribeContextMenu?.()

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundMouseMoveHandler = this._mouseMoveHandler.bind(this)
this._unsubscribeMousemove = () => {
this._target.removeEventListener('mousemove', boundMouseMoveHandler)
}
this._target.addEventListener('mousemove', boundMouseMoveHandler)

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundMouseWheel = this._mouseWheelHandler.bind(this)
this._unsubscribeMouseWheel = () => {
this._target.removeEventListener('wheel', boundMouseWheel)
}
this._target.addEventListener('wheel', boundMouseWheel, { passive: false })

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundContextMenu = this._contextMenuHandler.bind(this)
this._unsubscribeContextMenu = () => {
this._target.removeEventListener('contextmenu', boundContextMenu)
Expand Down Expand Up @@ -353,6 +354,7 @@ export default class SyntheticEvent {
const moveInfo = this._mouseTouchMoveWithDownInfo(this._getCoordinate(touch), this._touchMoveStartCoordinate!)
const { xOffset, yOffset, manhattanDistance } = moveInfo


if (!this._touchMoveExceededManhattanDistance && manhattanDistance < ManhattanDistance.CancelTap) {
return
}
Expand Down Expand Up @@ -392,13 +394,15 @@ export default class SyntheticEvent {
}

private _mouseMoveWithDownHandler (moveEvent: MouseEvent): void {

if (moveEvent.button !== MouseEventButton.Left) {
return
}

const moveInfo = this._mouseTouchMoveWithDownInfo(this._getCoordinate(moveEvent), this._mouseMoveStartCoordinate!)
const { manhattanDistance } = moveInfo


if (manhattanDistance >= ManhattanDistance.CancelClick) {
// if manhattan distance is more that 5 - we should cancel click event
this._cancelClick = true
Expand Down Expand Up @@ -441,6 +445,7 @@ export default class SyntheticEvent {

if (this._tapTimeoutId !== null && this._tapCount > 1) {
const { manhattanDistance } = this._mouseTouchMoveWithDownInfo(this._getCoordinate(dblClickEvent), this._tapCoordinate)

if (manhattanDistance < ManhattanDistance.DoubleTap && !this._cancelTap) {
this._processEvent(this._makeCompatEvent(dblClickEvent), this._handler.doubleTapEvent)
}
Expand All @@ -451,6 +456,7 @@ export default class SyntheticEvent {

if (this._clickTimeoutId !== null && this._clickCount > 1) {
const { manhattanDistance } = this._mouseTouchMoveWithDownInfo(this._getCoordinate(dblClickEvent), this._clickCoordinate)

if (manhattanDistance < ManhattanDistance.DoubleClick && !this._cancelClick) {
this._processEvent(this._makeCompatEvent(dblClickEvent), this._handler.mouseDoubleClickEvent)
}
Expand All @@ -459,7 +465,7 @@ export default class SyntheticEvent {
}
}

// eslint-disable-next-line complexity
private _touchEndHandler (touchEndEvent: TouchEvent): void {
let touch = this._touchWithId(touchEndEvent.changedTouches, this._activeTouchId)
if (touch === null && touchEndEvent.touches.length === 0) {
Expand Down Expand Up @@ -593,7 +599,9 @@ export default class SyntheticEvent {
}

{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundTouchMoveWithDownHandler = this._touchMoveHandler.bind(this)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundTouchEndHandler = this._touchEndHandler.bind(this)

this._unsubscribeRootTouchEvents = () => {
Expand Down Expand Up @@ -643,7 +651,9 @@ export default class SyntheticEvent {
}

{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundMouseMoveWithDownHandler = this._mouseMoveWithDownHandler.bind(this)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const boundMouseUpHandler = this._mouseUpHandler.bind(this)

this._unsubscribeRootMouseEvents = () => {
Expand Down Expand Up @@ -787,7 +797,7 @@ export default class SyntheticEvent {
}

private _startPinch (touches: TouchList): void {
const box = this._target.getBoundingClientRect() ?? { left: 0, top: 0 }
const box = this._target.getBoundingClientRect()
this._startPinchMiddleCoordinate = {
x: ((touches[0].clientX - box.left) + (touches[1].clientX - box.left)) / 2,
y: ((touches[0].clientY - box.top) + (touches[1].clientY - box.top)) / 2
Expand Down Expand Up @@ -851,9 +861,11 @@ export default class SyntheticEvent {
private _firesTouchEvents (e: MouseEvent): boolean {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (isValid(e.sourceCapabilities?.firesTouchEvents)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
return e.sourceCapabilities.firesTouchEvents
}

Expand All @@ -868,7 +880,7 @@ export default class SyntheticEvent {
// TouchEvent has no clientX/Y coordinates:
// We have to use the last Touch instead
const eventLike = touch ?? (event as MouseEvent)
const box = this._target.getBoundingClientRect() ?? { left: 0, top: 0 }
const box = this._target.getBoundingClientRect()
return {
x: eventLike.clientX - box.left,
y: eventLike.clientY - box.top,
Expand Down Expand Up @@ -907,6 +919,7 @@ export default class SyntheticEvent {

private _eventTimeStamp (e: TouchEvent | MouseEvent): number {
// for some reason e.timestamp is always 0 on iPad with magic mouse, so we use performance.now() as a fallback
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return e.timeStamp ?? performance.now()
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let measureCtx: Nullable<CanvasRenderingContext2D> = null
* @returns {number}
*/
export function getPixelRatio (canvas: HTMLCanvasElement): number {
return canvas.ownerDocument?.defaultView?.devicePixelRatio ?? 1
return canvas.ownerDocument.defaultView?.devicePixelRatio ?? 1
}

export function createFont (size?: number, weight?: string | number, family?: string): string {
Expand Down
30 changes: 13 additions & 17 deletions src/common/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,23 @@ export function nice (value: number): number {
nf = 8
}
value = nf * exp10
return exponent >= -20 ? +value.toFixed(exponent < 0 ? -exponent : 0) : value
return +value.toFixed(Math.abs(exponent))
}

/**
* 四舍五入
* Round
* @param value
* @param precision
* @return {number}
*/
export function round (value: number, precision: number): number {
if (precision == null) {
precision = 10
}
precision = Math.min(Math.max(0, precision), 20)
const v = (+value).toFixed(precision)
return +v
export function round (value: number, precision?: number): number {
precision = Math.max(0, precision ?? 0)
const pow = Math.pow(10, precision)
return Math.round(value * pow) / pow
}

/**
* 获取小数位数
* Get precision
* @param value
* @return {number|number}
*/
Expand All @@ -104,10 +101,9 @@ export function getPrecision (value: number): number {
if (eIndex > 0) {
const precision = +str.slice(eIndex + 1)
return precision < 0 ? -precision : 0
} else {
const dotIndex = str.indexOf('.')
return dotIndex < 0 ? 0 : str.length - 1 - dotIndex
}
const dotIndex = str.indexOf('.')
return dotIndex < 0 ? 0 : str.length - 1 - dotIndex
}

export function getMaxMin<D> (dataList: D[], maxKey: keyof D, minKey: keyof D): number[] {
Expand All @@ -116,15 +112,15 @@ export function getMaxMin<D> (dataList: D[], maxKey: keyof D, minKey: keyof D):
let index = 0
while (index < dataLength) {
const data = dataList[index]
maxMin[0] = Math.max((data[maxKey] ?? data) as number, maxMin[0])
maxMin[1] = Math.min((data[minKey] ?? data) as number, maxMin[1])
maxMin[0] = Math.max((data[maxKey] ?? Number.MIN_SAFE_INTEGER) as number, maxMin[0])
maxMin[1] = Math.min((data[minKey] ?? Number.MAX_SAFE_INTEGER) as number, maxMin[1])
++index
}
return maxMin
}

/**
* 10为底的对数函数
* log10
* @param value
* @return {number}
*/
Expand All @@ -133,7 +129,7 @@ export function log10 (value: number): number {
}

/**
* 10的指数函数
* index 10
* @param value
* @return {number}
*/
Expand Down
6 changes: 3 additions & 3 deletions src/component/Axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ export default abstract class AxisImp implements Axis {
scrollZoomEnabled = true
createTicks: AxisCreateTicksCallback

private readonly _parent: DrawPane<Axis>
private readonly _parent: DrawPane

private _range = getDefaultAxisRange()
private _prevRange = getDefaultAxisRange()
private _ticks: AxisTick[] = []

private _autoCalcTickFlag = true

constructor (parent: DrawPane<Axis>) {
constructor (parent: DrawPane) {
this._parent = parent
}

getParent (): DrawPane<Axis> { return this._parent }
getParent (): DrawPane { return this._parent }

buildTicks (force: boolean): boolean {
if (this._autoCalcTickFlag) {
Expand Down
2 changes: 1 addition & 1 deletion src/component/Indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ export type IndicatorConstructor<D = unknown> = new () => IndicatorImp<D>

export type EachFigureCallback<D> = (figure: IndicatorFigure<D>, figureStyles: IndicatorFigureStyle, index: number) => void

// eslint-disable-next-line @typescript-eslint/max-params
export function eachFigures<D = unknown> (
kLineDataList: KLineData[],
indicator: Indicator<D>,
Expand Down Expand Up @@ -382,6 +381,7 @@ export default class IndicatorImp<D = unknown> implements Indicator<D> {
if (!isString(this.name)) {
this.name = name ?? ''
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.shortName = shortName ?? this.shortName ?? this.name
if (isNumber(precision)) {
this.precision = precision
Expand Down
6 changes: 3 additions & 3 deletions src/component/XAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export interface XAxis extends Axis, Required<XAxisTemplate> {
convertTimestampToPixel: (timestamp: number) => number
}

export type XAxisConstructor = new (parent: DrawPane<Axis>) => XAxis
export type XAxisConstructor = new (parent: DrawPane) => XAxis

export default abstract class XAxisImp extends AxisImp implements XAxis {
constructor (parent: DrawPane<Axis>, xAxis: XAxisTemplate) {
constructor (parent: DrawPane, xAxis: XAxisTemplate) {
super(parent)
this.override(xAxis)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ export default abstract class XAxisImp extends AxisImp implements XAxis {

static extend (template: XAxisTemplate): XAxisConstructor {
class Custom extends XAxisImp {
constructor (parent: DrawPane<Axis>) {
constructor (parent: DrawPane) {
super(parent, template)
}
}
Expand Down
Loading

0 comments on commit 9048809

Please sign in to comment.