Skip to content

Commit e19871f

Browse files
committed
(lint/types): fix or ignore remaining lint type errors
- use TS 3.0 defaultProps support so that clearOnResize doesn't have to be optional - ignore consistent-type-assertions and no-non-null-assertion in places where those hacks were intentionally used for simplicity's sake - fix some strict-boolean-expressions by checking for types explicitly or using nullish coalescing - nullish coalescing on canvasProps isn't breaking because it already had PropTypes.object defined - nullish coalescing on DPR isn't breaking because it should be a number (or undefined), and shouldn't be 0 - and even if it were 0, it would've been set to 1 before anyway bc of the Math.max - ignore several strict-boolean-expressions in _resizeCanvas because checking for undefined specifically is a breaking change - 0, '', null, etc falsey width/height was still resized previously, so if we check for undefined instead, it will stop resizing falseys - 0, '', null, etc don't make a lot of sense to set a width/height, if these were used it may have been accidental or for some temporary hack - accidental would be a bug, so will consider a fix, but fixing that bug would still be breaking :/ :/
1 parent fb1a42b commit e19871f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/index.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import trimCanvas from 'trim-canvas'
55

66
export interface SignatureCanvasProps extends SignaturePad.SignaturePadOptions {
77
canvasProps?: React.CanvasHTMLAttributes<HTMLCanvasElement>
8-
clearOnResize?: boolean
8+
clearOnResize: boolean
99
}
1010

1111
export class SignatureCanvas extends Component<SignatureCanvasProps> {
@@ -25,16 +25,18 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
2525
clearOnResize: PropTypes.bool
2626
}
2727

28-
static defaultProps = {
28+
static defaultProps: Pick<SignatureCanvasProps, 'clearOnResize'> = {
2929
clearOnResize: true
3030
}
3131

3232
// this is some hack-ish init and casting to avoid `| null` everywhere :/
33+
/* eslint-disable @typescript-eslint/consistent-type-assertions */
3334
_sigPad: SignaturePad = {} as SignaturePad
3435
_canvas: HTMLCanvasElement = {} as HTMLCanvasElement
36+
/* eslint-enable @typescript-eslint/consistent-type-assertions */
3537

3638
private readonly setRef = (ref: HTMLCanvasElement | null): void => {
37-
if (ref) {
39+
if (ref !== null) {
3840
this._canvas = ref
3941
}
4042
}
@@ -70,6 +72,7 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
7072
const copy = document.createElement('canvas')
7173
copy.width = this._canvas.width
7274
copy.height = this._canvas.height
75+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7376
copy.getContext('2d')!.drawImage(this._canvas, 0, 0)
7477
// then trim it
7578
return trimCanvas(copy)
@@ -87,10 +90,13 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
8790
this._resizeCanvas()
8891
}
8992

93+
// a few eslint disables of strict-boolean-expressions here because changing
94+
// these lines would be breaking -- 0s, '', null etc are falsey
9095
_resizeCanvas = (): void => {
91-
const canvasProps = this.props.canvasProps || {}
96+
const canvasProps = this.props.canvasProps ?? {}
9297
const { width, height } = canvasProps
9398
// don't resize if the canvas has fixed width and height
99+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
94100
if (width && height) {
95101
return
96102
}
@@ -99,14 +105,17 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
99105
/* When zoomed out to less than 100%, for some very strange reason,
100106
some browsers report devicePixelRatio as less than 1
101107
and only part of the canvas is cleared then. */
102-
const ratio = Math.max(window.devicePixelRatio || 1, 1)
108+
const ratio = Math.max(window.devicePixelRatio ?? 1, 1)
103109

110+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
104111
if (!width) {
105112
canvas.width = canvas.offsetWidth * ratio
106113
}
114+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
107115
if (!height) {
108116
canvas.height = canvas.offsetHeight * ratio
109117
}
118+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
110119
canvas.getContext('2d')!.scale(ratio, ratio)
111120
this.clear()
112121
}

0 commit comments

Comments
 (0)