Skip to content

Commit 3b90098

Browse files
tayeinteractjs-ci
authored andcommitted
fix: resolve some strictNullChecks issues
# Conflicts: # packages/@interactjs/arrange/arrange.spec.ts # packages/@interactjs/modifiers/spring/spring.spec.ts # packages/@interactjs/modifiers/transform/transform.spec.ts # packages/@interactjs/multi-target/plugin.ts # packages/@interactjs/snappers/edgeTarget.spec.ts
1 parent f6dd349 commit 3b90098

File tree

19 files changed

+264
-206
lines changed

19 files changed

+264
-206
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
"@typescript-eslint/parser": "^5.27.1",
4242
"@vitejs/plugin-vue": "^2.3.3",
4343
"@vue/babel-plugin-jsx": "^1.1.1",
44-
"@vue/compiler-sfc": "^3.2.37",
45-
"@vue/reactivity": "^3.2.37",
46-
"@vue/runtime-dom": "^3.2.37",
47-
"@vue/test-utils": "^2.0.0",
44+
"@vue/compiler-sfc": "^3.3.4",
45+
"@vue/reactivity": "^3.3.4",
46+
"@vue/runtime-dom": "^3.3.4",
47+
"@vue/test-utils": "^2.4.1",
4848
"babel-helper-vue-jsx-merge-props": "^2.0.3",
4949
"babel-plugin-syntax-jsx": "^6.18.0",
5050
"babelify": "^10.0.0",
@@ -97,7 +97,7 @@
9797
"typescript": "^4.7.3",
9898
"vijest": "^0.0.1",
9999
"vite": "^4.1.4",
100-
"vue": "^3.2.37",
100+
"vue": "^3.3.4",
101101
"yargs": "^17.5.1"
102102
},
103103
"engines": {

packages/@interactjs/actions/drop/drop.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('actions/drop', () => {
7575
})
7676

7777
const onActionsDropStart = jest.fn((arg: { interaction: Interaction }) => {
78-
const activeDrops = [...arg.interaction.dropState.activeDrops]
78+
const activeDrops = [...arg.interaction.dropState!.activeDrops]
7979
// actions/drop:start is fired with all activeDrops
8080
expect(activeDrops.map((activeDrop) => activeDrop.element)).toEqual([dropEl3])
8181
})
@@ -92,7 +92,7 @@ describe('actions/drop', () => {
9292
expect(onActionsDropStart).toHaveBeenCalledTimes(1)
9393

9494
// rejected dropzones are removed from activeDrops,
95-
expect(interaction.dropState.activeDrops.map((d) => d.element)).toEqual([dropEl3])
95+
expect(interaction.dropState!.activeDrops.map((d) => d.element)).toEqual([dropEl3])
9696

9797
// rejected dropzones are deactivated,
9898
expect(onDeactivate).toHaveBeenNthCalledWith(1, expect.objectContaining({ target: dropEl1 }))

packages/@interactjs/actions/drop/plugin.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ function install (scope: Scope) {
243243

244244
return interact
245245
}
246-
return scope.dynamicDrop
246+
return scope.dynamicDrop!
247247
}
248248

249249
extend(actions.phaselessTypes, {
@@ -331,30 +331,28 @@ function getActiveDrops (scope: Scope, dragElement: Element) {
331331
}
332332

333333
function getDrop (
334-
{ dropState, interactable: draggable, element: dragElement }: Partial<Interaction>,
334+
{ dropState, interactable: draggable, element: dragElement }: Interaction,
335335
dragEvent,
336336
pointerEvent,
337337
) {
338-
const validDrops = []
338+
const validDrops: Element[] = []
339339

340340
// collect all dropzones and their elements which qualify for a drop
341-
for (const { dropzone, element: dropzoneElement, rect } of dropState.activeDrops) {
342-
validDrops.push(
343-
dropzone.dropCheck(dragEvent, pointerEvent, draggable, dragElement, dropzoneElement, rect)
344-
? dropzoneElement
345-
: null,
346-
)
341+
for (const { dropzone, element: dropzoneElement, rect } of dropState!.activeDrops) {
342+
if (dropzone.dropCheck(dragEvent, pointerEvent, draggable!, dragElement!, dropzoneElement, rect)) {
343+
validDrops.push(dropzoneElement)
344+
}
347345
}
348346

349347
// get the most appropriate dropzone based on DOM depth and order
350348
const dropIndex = domUtils.indexOfDeepestElement(validDrops)
351349

352-
return dropState.activeDrops[dropIndex] || null
350+
return dropState!.activeDrops[dropIndex] || null
353351
}
354352

355353
function getDropEvents (interaction: Interaction, _pointerEvent, dragEvent: DragEvent) {
356-
const { dropState } = interaction
357-
const dropEvents = {
354+
const dropState = interaction.dropState!
355+
const dropEvents: Record<string, DropEvent | null> = {
358356
enter: null,
359357
leave: null,
360358
activate: null,
@@ -366,14 +364,14 @@ function getDropEvents (interaction: Interaction, _pointerEvent, dragEvent: Drag
366364
if (dragEvent.type === 'dragstart') {
367365
dropEvents.activate = new DropEvent(dropState, dragEvent, 'dropactivate')
368366

369-
dropEvents.activate.target = null
370-
dropEvents.activate.dropzone = null
367+
dropEvents.activate.target = null as never
368+
dropEvents.activate.dropzone = null as never
371369
}
372370
if (dragEvent.type === 'dragend') {
373371
dropEvents.deactivate = new DropEvent(dropState, dragEvent, 'dropdeactivate')
374372

375-
dropEvents.deactivate.target = null
376-
dropEvents.deactivate.dropzone = null
373+
dropEvents.deactivate.target = null as never
374+
dropEvents.deactivate.dropzone = null as never
377375
}
378376

379377
if (dropState.rejected) {
@@ -406,7 +404,6 @@ function getDropEvents (interaction: Interaction, _pointerEvent, dragEvent: Drag
406404
if (dragEvent.type === 'dragmove' && dropState.cur.dropzone) {
407405
dropEvents.move = new DropEvent(dropState, dragEvent, 'dropmove')
408406

409-
dropEvents.move.dragmove = dragEvent
410407
dragEvent.dropzone = dropState.cur.dropzone
411408
}
412409

@@ -418,7 +415,7 @@ Record<'leave' | 'enter' | 'move' | 'drop' | 'activate' | 'deactivate', DropEven
418415
>
419416

420417
function fireDropEvents (interaction: Interaction, events: FiredDropEvents) {
421-
const { dropState } = interaction
418+
const dropState = interaction.dropState!
422419
const { activeDrops, cur, prev } = dropState
423420

424421
if (events.leave) {
@@ -447,10 +444,10 @@ function onEventCreated ({ interaction, iEvent, event }: DoPhaseArg<'drag', Even
447444
return
448445
}
449446

450-
const { dropState } = interaction
447+
const dropState = interaction.dropState!
451448

452449
if (scope.dynamicDrop) {
453-
dropState.activeDrops = getActiveDrops(scope, interaction.element)
450+
dropState.activeDrops = getActiveDrops(scope, interaction.element!)
454451
}
455452

456453
const dragEvent = iEvent
@@ -490,7 +487,9 @@ function dropzoneMethod (interactable: Interactable, options?: DropzoneOptions |
490487
return acc
491488
}, {})
492489

493-
interactable.off(interactable.options.drop.listeners)
490+
const prevListeners = interactable.options.drop.listeners
491+
prevListeners && interactable.off(prevListeners)
492+
494493
interactable.on(corrected)
495494
interactable.options.drop.listeners = corrected
496495
}
@@ -646,12 +645,12 @@ const drop: Plugin = {
646645
return
647646
}
648647

649-
const { dropState } = interaction
648+
const dropState = interaction.dropState!
650649

651650
// reset active dropzones
652-
dropState.activeDrops = null
653-
dropState.events = null
654-
dropState.activeDrops = getActiveDrops(scope, interaction.element)
651+
dropState.activeDrops = []
652+
dropState.events = {}
653+
dropState.activeDrops = getActiveDrops(scope, interaction.element!)
655654
dropState.events = getDropEvents(interaction, event, dragEvent)
656655

657656
if (dropState.events.activate) {
@@ -670,10 +669,11 @@ const drop: Plugin = {
670669
return
671670
}
672671

673-
fireDropEvents(interaction, interaction.dropState.events)
672+
const dropState = interaction.dropState!
673+
fireDropEvents(interaction, dropState.events)
674674

675675
scope.fire('actions/drop:move', { interaction, dragEvent })
676-
interaction.dropState.events = {}
676+
dropState.events = {}
677677
},
678678

679679
'interactions:action-end': (arg: DoPhaseArg<'drag', EventPhase>, scope) => {
@@ -684,7 +684,7 @@ const drop: Plugin = {
684684
const { interaction, iEvent: dragEvent } = arg
685685

686686
onEventCreated(arg, scope)
687-
fireDropEvents(interaction, interaction.dropState.events)
687+
fireDropEvents(interaction, interaction.dropState!.events)
688688
scope.fire('actions/drop:end', { interaction, dragEvent })
689689
},
690690

@@ -696,12 +696,12 @@ const drop: Plugin = {
696696
const { dropState } = interaction
697697

698698
if (dropState) {
699-
dropState.activeDrops = null
700-
dropState.events = null
701-
dropState.cur.dropzone = null
702-
dropState.cur.element = null
703-
dropState.prev.dropzone = null
704-
dropState.prev.element = null
699+
dropState.activeDrops = null as never
700+
dropState.events = null as never
701+
dropState.cur.dropzone = null as never
702+
dropState.cur.element = null as never
703+
dropState.prev.dropzone = null as never
704+
dropState.prev.element = null as never
705705
dropState.rejected = false
706706
}
707707
},
@@ -712,7 +712,7 @@ const drop: Plugin = {
712712
fireDropEvents,
713713
defaults: {
714714
enabled: false,
715-
accept: null,
715+
accept: null as never,
716716
overlap: 'pointer',
717717
} as DropzoneOptions,
718718
}

packages/@interactjs/core/Interactable.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export class Interactable implements Partial<Eventable> {
4848
readonly _doc: Document
4949
readonly _scopeEvents: Scope['events']
5050

51-
/** @internal */ _rectChecker?: typeof Interactable.prototype.getRect
52-
5351
/** */
5452
constructor (
5553
target: Target,
@@ -84,7 +82,7 @@ export class Interactable implements Partial<Eventable> {
8482
return this
8583
}
8684

87-
updatePerActionListeners (actionName: ActionName, prev: Listeners, cur: Listeners) {
85+
updatePerActionListeners (actionName: ActionName, prev: Listeners | undefined, cur: Listeners | undefined) {
8886
if (is.array(prev) || is.object(prev)) {
8987
this.off(actionName, prev)
9088
}
@@ -168,10 +166,8 @@ export class Interactable implements Partial<Eventable> {
168166
rectChecker(checker: (element: Element) => any): this
169167
rectChecker (checker?: (element: Element) => any) {
170168
if (is.func(checker)) {
171-
this._rectChecker = checker
172-
173169
this.getRect = (element) => {
174-
const rect = extend({}, this._rectChecker(element))
170+
const rect = extend({}, checker.apply(this, element))
175171

176172
if (!(('width' in rect) as unknown)) {
177173
rect.width = rect.right - rect.left
@@ -185,8 +181,7 @@ export class Interactable implements Partial<Eventable> {
185181
}
186182

187183
if (checker === null) {
188-
delete this.getRect
189-
delete this._rectChecker
184+
delete (this as Partial<typeof this>).getRect
190185

191186
return this
192187
}
@@ -268,7 +263,7 @@ export class Interactable implements Partial<Eventable> {
268263
)
269264
}
270265

271-
testAllow (this: Interactable, allowFrom: IgnoreValue, targetNode: Node, element: Node) {
266+
testAllow (this: Interactable, allowFrom: IgnoreValue | undefined, targetNode: Node, element: Node) {
272267
if (!allowFrom) {
273268
return true
274269
}
@@ -286,7 +281,7 @@ export class Interactable implements Partial<Eventable> {
286281
return false
287282
}
288283

289-
testIgnore (this: Interactable, ignoreFrom: IgnoreValue, targetNode: Node, element: Node) {
284+
testIgnore (this: Interactable, ignoreFrom: IgnoreValue | undefined, targetNode: Node, element: Node) {
290285
if (!ignoreFrom || !is.element(element)) {
291286
return false
292287
}

packages/@interactjs/core/InteractableSet.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export class InteractableSet {
4545
const targetIndex = arr.findIndex(targetMappings, (m) => m.context === context)
4646
if (targetMappings[targetIndex]) {
4747
// Destroying mappingInfo's context and interactable
48-
targetMappings[targetIndex].context = null
49-
targetMappings[targetIndex].interactable = null
48+
targetMappings[targetIndex].context = null as never
49+
targetMappings[targetIndex].interactable = null as never
5050
}
5151
targetMappings.splice(targetIndex, 1)
5252
},
@@ -108,9 +108,9 @@ export class InteractableSet {
108108
return found && found.interactable
109109
}
110110

111-
forEachMatch<T> (node: Node, callback: (interactable: Interactable) => T) {
111+
forEachMatch<T> (node: Node, callback: (interactable: Interactable) => T): T | void {
112112
for (const interactable of this.list) {
113-
let ret: void | T
113+
let ret: T
114114

115115
if (
116116
(is.string(interactable.target)

packages/@interactjs/core/Interaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class Interaction<T extends ActionName | null = ActionName> {
155155
_interacting = false
156156
_ending = false
157157
_stopped = true
158-
_proxy: InteractionProxy<T> | null = null
158+
_proxy: InteractionProxy<T>
159159

160160
simulation = null
161161

packages/@interactjs/core/scope.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class Scope {
5656
isInitialized = false
5757
listenerMaps: Array<{
5858
map: ListenerMap
59-
id: string
59+
id?: string
6060
}> = []
6161

6262
browser = browser
@@ -150,7 +150,8 @@ export class Scope {
150150
}
151151

152152
pluginIsInstalled (plugin: Plugin) {
153-
return this._plugins.map[plugin.id] || this._plugins.list.indexOf(plugin) !== -1
153+
const { id } = plugin
154+
return id ? !!this._plugins.map[id] : this._plugins.list.indexOf(plugin) !== -1
154155
}
155156

156157
usePlugin (plugin: Plugin, options?: { [key: string]: any }) {
@@ -183,7 +184,7 @@ export class Scope {
183184
for (; index < len; index++) {
184185
const otherId = this.listenerMaps[index].id
185186

186-
if (before[otherId] || before[pluginIdRoot(otherId)]) {
187+
if (otherId && (before[otherId] || before[pluginIdRoot(otherId)])) {
187188
break
188189
}
189190
}

packages/@interactjs/core/tests/_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function testEnv<T extends Target = HTMLElement> ({
122122
return {
123123
scope,
124124
interaction,
125-
target,
125+
target: target as T extends undefined ? HTMLElement : T,
126126
interactable,
127127
coords,
128128
event,

packages/@interactjs/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export type CursorChecker = (
102102
export interface ActionMethod<T> {
103103
(this: Interactable): T
104104
// eslint-disable-next-line no-undef
105-
(this: Interactable, options: Partial<OrBoolean<T>> | boolean): typeof this
105+
(this: Interactable, options?: Partial<OrBoolean<T>> | boolean): typeof this
106106
}
107107

108108
export interface OptionMethod<T> {

packages/@interactjs/dev-tools/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const checks: Check[] = [
9393
{
9494
name: CheckName.touchAction,
9595
perform ({ element }) {
96-
return !parentHasStyle(element, 'touchAction', /pan-|pinch|none/)
96+
return !!element && !parentHasStyle(element, 'touchAction', /pan-|pinch|none/)
9797
},
9898
getInfo ({ element }) {
9999
return [element, links.touchAction]
@@ -122,7 +122,7 @@ const checks: Check[] = [
122122
name: CheckName.noListeners,
123123
perform (interaction) {
124124
const actionName = interaction.prepared.name
125-
const moveListeners = interaction.interactable.events.types[`${actionName}move`] || []
125+
const moveListeners = interaction.interactable?.events.types[`${actionName}move`] || []
126126

127127
return !moveListeners.length
128128
},

packages/@interactjs/modifiers/snap/edges.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ test('modifiers/snap/edges', () => {
3232
}
3333

3434
arg.state = { options }
35-
snapEdges.start(arg as any)
36-
snapEdges.set(arg as any)
35+
snapEdges.start!(arg as any)
36+
snapEdges.set!(arg as any)
3737

3838
// modified coords are correct
3939
expect(arg.coords).toEqual({ x: target0.left, y: target0.top })
@@ -42,8 +42,8 @@ test('modifiers/snap/edges', () => {
4242
arg.edges = { bottom: true, right: true }
4343

4444
arg.state = { options }
45-
snapEdges.start(arg as any)
46-
snapEdges.set(arg as any)
45+
snapEdges.start!(arg as any)
46+
snapEdges.set!(arg as any)
4747

4848
// modified coord are correct
4949
expect(arg.coords).toEqual({ x: target0.right, y: target0.bottom })

0 commit comments

Comments
 (0)