Skip to content

Commit

Permalink
refactor: fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Jan 29, 2025
1 parent fdb21e9 commit e4b826c
Show file tree
Hide file tree
Showing 44 changed files with 117 additions and 142 deletions.
2 changes: 1 addition & 1 deletion scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {cli, connectCoverageReporter} = await setupToolboxTester(
globals: {
'@testing-library/dom': 'DomTestingLibrary',
'@testing-library/react': 'ReactTestingLibrary',
react: 'React',
'react': 'React',
'react-dom': 'ReactDom',
},
}),
Expand Down
10 changes: 5 additions & 5 deletions src/clipboard/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export async function paste(
(typeof clipboardData === 'string'
? getClipboardDataFromString(doc, clipboardData)
: clipboardData) ??
(await readDataTransferFromClipboard(doc).catch(() => {
throw new Error(
'`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.',
)
}))
(await readDataTransferFromClipboard(doc).catch(() => {
throw new Error(
'`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.',
)
}))

this.dispatchUIEvent(target, 'paste', {
clipboardData: dataTransfer,
Expand Down
4 changes: 2 additions & 2 deletions src/convenience/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function tab(
shift === true
? '{Shift>}{Tab}{/Shift}'
: shift === false
? '[/ShiftLeft][/ShiftRight]{Tab}'
: '{Tab}',
? '[/ShiftLeft][/ShiftRight]{Tab}'
: '{Tab}',
)
}
8 changes: 3 additions & 5 deletions src/document/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface UISelection {
focusOffset: number
}

export type UIValueString = String & {[UIValue]: true}
export type UISelectionStart = Number & {[UISelection]: true}
export type UIValueString = string & {[UIValue]: true}
export type UISelectionStart = number & {[UISelection]: true}

export function isUIValue(
value: string | UIValueString,
Expand All @@ -40,7 +40,6 @@ export function setUIValue(

element[UIValue] = value

// eslint-disable-next-line no-new-wrappers
element.value = Object.assign(new String(value), {
[UIValue]: true,
}) as unknown as string
Expand All @@ -52,7 +51,7 @@ export function getUIValue(element: HTMLInputElement | HTMLTextAreaElement) {
: String(element[UIValue])
}

/** Flag the IDL value as clean. This does not change the value.*/
/** Flag the IDL value as clean. This does not change the value. */
export function setUIValueClean(
element: HTMLInputElement | HTMLTextAreaElement,
) {
Expand Down Expand Up @@ -113,7 +112,6 @@ export function setUISelection(
return
}

// eslint-disable-next-line no-new-wrappers
const startObj = Object.assign(new Number(startOffset), {
[UISelection]: true,
}) as unknown as number
Expand Down
4 changes: 2 additions & 2 deletions src/document/copySelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {getUISelection, getUIValue} from './UI'
export function copySelection(target: Element) {
const data: Record<string, string> = hasOwnSelection(target)
? {'text/plain': readSelectedValueFromInput(target)}
: // TODO: We could implement text/html copying of DOM nodes here
{'text/plain': String(target.ownerDocument.getSelection())}
// TODO: We could implement text/html copying of DOM nodes here
: {'text/plain': String(target.ownerDocument.getSelection())}

const dt = createDataTransfer(getWindow(target))
for (const type in data) {
Expand Down
2 changes: 0 additions & 2 deletions src/event/behavior/keydown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-use-before-define */

import {getUIValue, setUISelection, getValueOrTextContent} from '../../document'
import {
getTabDestination,
Expand Down
4 changes: 1 addition & 3 deletions src/event/behavior/keypress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-use-before-define */

import {isContentEditable, isEditable, isElementType} from '../../utils'
import {input} from '../input'
import {behavior} from './registry'
Expand All @@ -10,7 +8,7 @@ behavior.keypress = (event, target, instance) => {
isElementType(target, 'button') ||
(isElementType(target, 'input') &&
ClickInputOnEnter.includes(target.type)) ||
(isElementType(target, 'a') && Boolean(target.href))
(isElementType(target, 'a') && Boolean(target.href))
) {
return () => {
instance.dispatchUIEvent(target, 'click')
Expand Down
2 changes: 0 additions & 2 deletions src/event/behavior/keyup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-use-before-define */

import {isClickableInput} from '../../utils'
import {BehaviorPlugin} from '.'
import {behavior} from './registry'
Expand Down
2 changes: 1 addition & 1 deletion src/event/behavior/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface BehaviorPlugin<Type extends EventType> {
event: DocumentEventMap[Type],
target: Element,
instance: Instance,
): // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
):
void | (() => void)
}

Expand Down
14 changes: 6 additions & 8 deletions src/event/createEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
} from './types'

interface InterfaceMap {
ClipboardEvent: {type: ClipboardEvent; init: ClipboardEventInit}
InputEvent: {type: InputEvent; init: InputEventInit}
MouseEvent: {type: MouseEvent; init: MouseEventInit}
PointerEvent: {type: PointerEvent; init: PointerEventInit}
KeyboardEvent: {type: KeyboardEvent; init: KeyboardEventInit}
FocusEvent: {type: FocusEvent; init: FocusEventInit}
ClipboardEvent: {type: ClipboardEvent, init: ClipboardEventInit}
InputEvent: {type: InputEvent, init: InputEventInit}
MouseEvent: {type: MouseEvent, init: MouseEventInit}
PointerEvent: {type: PointerEvent, init: PointerEventInit}
KeyboardEvent: {type: KeyboardEvent, init: KeyboardEventInit}
FocusEvent: {type: FocusEvent, init: FocusEventInit}
}
type InterfaceNames = typeof eventMap[keyof typeof eventMap]['EventType']
type Interface<k extends InterfaceNames> = k extends keyof InterfaceMap
Expand Down Expand Up @@ -55,7 +55,6 @@ export function createEvent<K extends EventType>(

/* istanbul ignore next */
function getEventConstructors(window: Window & typeof globalThis) {
/* eslint-disable @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-extraneous-class */
const Event = window.Event ?? class Event {}
const AnimationEvent =
window.AnimationEvent ?? class AnimationEvent extends Event {}
Expand All @@ -79,7 +78,6 @@ function getEventConstructors(window: Window & typeof globalThis) {
const PointerEvent =
window.PointerEvent ?? class PointerEvent extends MouseEvent {}
const TouchEvent = window.TouchEvent ?? class TouchEvent extends UIEvent {}
/* eslint-enable @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-extraneous-class */

return {
Event,
Expand Down
4 changes: 2 additions & 2 deletions src/event/selection/moveSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function moveSelection(node: Element, direction: -1 | 1) {
selection.startOffset === selection.endOffset
? selection.focusOffset + direction
: direction < 0
? selection.startOffset
: selection.endOffset,
? selection.startOffset
: selection.endOffset,
})
} else {
const selection = node.ownerDocument.getSelection()
Expand Down
8 changes: 4 additions & 4 deletions src/event/selection/resolveCaretPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ function findNodeAtTextOffset(
offset: number | undefined,
isRoot = true,
): {
node: Node
offset: number
} {
node: Node
offset: number
} {
// When clicking after the content the browser behavior can be complicated:
// 1. If there is textContent after the last element child,
// the cursor is moved there.
Expand Down Expand Up @@ -67,7 +67,7 @@ function findNodeAtTextOffset(
// The pre-commit hooks keeps changing this
// See https://github.com/kentcdodds/kcd-scripts/issues/218
/* istanbul ignore else */
// eslint-disable-next-line no-lonely-if

if (c.nodeType === 3) {
return {
node: c as Node,
Expand Down
16 changes: 8 additions & 8 deletions src/event/selection/setSelectionPerMouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export function setSelectionPerMouseDown({
)

const [start, end] = node
? // As offset is describing a DOMOffset it is non-trivial to determine
// which elements might be considered in the same line of text.
// TODO: support expanding initial range on multiple clicks if node is given
[offset, offset]
// As offset is describing a DOMOffset it is non-trivial to determine
// which elements might be considered in the same line of text.
// TODO: support expanding initial range on multiple clicks if node is given
? [offset, offset]
: getTextRange(text, offset, clickCount)

// TODO: implement modifying selection per shift/ctrl+mouse
Expand Down Expand Up @@ -64,7 +64,7 @@ export function setSelectionPerMouseDown({
try {
range.setStart(startNode, startOffset)
range.setEnd(endNode, endOffset)
} catch (e: unknown) {
} catch {
throw new Error('The given offset is out of bounds.')
}

Expand All @@ -89,8 +89,8 @@ function getTextRange(
if (clickCount % 3 === 2) {
return [
textPos -
(text.substr(0, pos).match(/(\w+|\s+|\W)?$/) as RegExpMatchArray)[0]
.length,
(text.substr(0, pos).match(/(\w+|\s+|\W)?$/) as RegExpMatchArray)[0]
.length,
pos === undefined
? pos
: pos +
Expand All @@ -102,7 +102,7 @@ function getTextRange(
// triple click
return [
textPos -
(text.substr(0, pos).match(/[^\r\n]*$/) as RegExpMatchArray)[0].length,
(text.substr(0, pos).match(/[^\r\n]*$/) as RegExpMatchArray)[0].length,
pos === undefined
? pos
: pos +
Expand Down
24 changes: 12 additions & 12 deletions src/event/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ export interface FixedDocumentEventMap extends DocumentEventMap {
type SpecificEventInit<E extends Event> = E extends InputEvent
? InputEventInit
: E extends ClipboardEvent
? ClipboardEventInit
: E extends KeyboardEvent
? KeyboardEventInit
: E extends PointerEvent
? PointerEventInit
: E extends MouseEvent
? MouseEventInit
: E extends FocusEvent
? FocusEventInit
: E extends UIEvent
? UIEventInit
: EventInit
? ClipboardEventInit
: E extends KeyboardEvent
? KeyboardEventInit
: E extends PointerEvent
? PointerEventInit
: E extends MouseEvent
? MouseEventInit
: E extends FocusEvent
? FocusEventInit
: E extends UIEvent
? UIEventInit
: EventInit

export interface PointerCoords {
x?: number
Expand Down
6 changes: 5 additions & 1 deletion src/event/wrapEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {getConfig} from '@testing-library/dom'

export function wrapEvent<R>(cb: () => R, _element: Element) {
export function wrapEvent<R>(
cb: () => R,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_element: Element,
) {
return getConfig().eventWrapper(cb) as unknown as R
}
4 changes: 2 additions & 2 deletions src/pointer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ async function pointerAction(instance: Instance, action: PointerAction) {
'pointerName' in action && action.pointerName
? action.pointerName
: 'keyDef' in action
? instance.system.pointer.getPointerName(action.keyDef)
: 'mouse'
? instance.system.pointer.getPointerName(action.keyDef)
: 'mouse'

const previousPosition =
instance.system.pointer.getPreviousPosition(pointerName)
Expand Down
8 changes: 4 additions & 4 deletions src/setup/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function setupDirect(
keyboardState,
pointerState,
...options
}: DirectOptions & {keyboardState?: System; pointerState?: System} = {}, // backward-compatibility
}: DirectOptions & {keyboardState?: System, pointerState?: System} = {}, // backward-compatibility
node?: Node,
) {
const config = createConfig(options, defaultOptionsDirect, node)
Expand Down Expand Up @@ -146,9 +146,9 @@ export function createInstance(
config: Config,
system: System = new System(),
): {
instance: Instance
api: UserEvent
} {
instance: Instance
api: UserEvent
} {
const instance = {} as Instance
Object.assign(instance, {
config,
Expand Down
1 change: 0 additions & 1 deletion src/system/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export class KeyboardHost {
return !!this.registry[code]?.unpreventedDefault
}
delete(code: string) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.registry[code]
}
values() {
Expand Down
2 changes: 0 additions & 2 deletions src/system/pointer/buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class Buttons {
getButtons() {
let v = 0
for (const button of Object.keys(this.pressed)) {
// eslint-disable-next-line no-bitwise
v |= 2 ** Number(button)
}
return v
Expand All @@ -32,7 +31,6 @@ export class Buttons {
k => k.name !== keyDef.name,
)
if (this.pressed[button].length === 0) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.pressed[button]
return button
}
Expand Down
2 changes: 1 addition & 1 deletion src/utility/selectOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function selectOptionsBase(
return matchingOption
} else {
throw getConfig().getElementError(
`Value "${String(val)}" not found in options`,
`Value "${String(val as string)}" not found in options`,
select,
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dataTransfer/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function readBlobText(blob: Blob, FileReader: {new (): FileReader}) {
fr.onerror = rej
fr.onabort = rej
fr.onload = () => {
res(String(fr.result))
res(String(fr.result as string))
}
fr.readAsText(blob)
})
Expand Down
7 changes: 3 additions & 4 deletions src/utils/dataTransfer/DataTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class DataTransferItemStub implements DataTransferItem {

class DataTransferItemListStub
extends Array<DataTransferItem>
implements DataTransferItemList
{
implements DataTransferItemList {
add(data: string, type: string): DataTransferItem
add(file: File): DataTransferItem
add(...args: never[]) {
Expand All @@ -69,8 +68,8 @@ function getTypeMatcher(type: string, exact: boolean) {
return exact
? item.type === (isGroup ? group : type)
: isGroup
? item.type.startsWith(`${group}/`)
: item.type === group
? item.type.startsWith(`${group}/`)
: item.type === group
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/dataTransfer/FileList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function createFileList(

// guard for environments without FileList
/* istanbul ignore else */
if (window.FileList as Function | undefined) {
if (window.FileList) {
Object.setPrototypeOf(list, window.FileList.prototype)
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/edit/isContentEditable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//jsdom is not supporting isContentEditable
// jsdom is not supporting isContentEditable
export function isContentEditable(
element: Element,
): element is HTMLElement & {contenteditable: 'true'} {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/edit/isEditable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type GuardedType<T> = T extends (x: any) => x is infer R ? R : never
export function isEditable(
element: Element,
): element is
| GuardedType<typeof isContentEditable>
| (EditableInputOrTextarea & {readOnly: false}) {
| GuardedType<typeof isContentEditable>
| (EditableInputOrTextarea & {readOnly: false}) {
return (
(isEditableInputOrTextArea(element) && !element.readOnly) ||
isContentEditable(element)
Expand Down
Loading

0 comments on commit e4b826c

Please sign in to comment.