Skip to content

Commit de7abd0

Browse files
authored
fix: Remove unsafe non-null assertions (#9598)
1 parent f6f5fcc commit de7abd0

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

packages/blockly/core/toolbox/toolbox.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
// Former goog.module ID: Blockly.Toolbox
1313

14-
// Unused import preserved for side-effects. Remove if unneeded.
1514
import {BlockSvg} from '../block_svg.js';
1615
import * as browserEvents from '../browser_events.js';
1716
import * as common from '../common.js';
@@ -192,7 +191,7 @@ export class Toolbox
192191
aria.setRole(this.contentsDiv_, aria.Role.TREE);
193192
container.appendChild(this.contentsDiv_);
194193

195-
svg.parentNode!.insertBefore(container, svg);
194+
svg.parentNode?.insertBefore(container, svg);
196195

197196
this.attachEvents_(container, this.contentsDiv_);
198197
return container;
@@ -281,7 +280,7 @@ export class Toolbox
281280
const itemId = (targetElement as Element).getAttribute('id');
282281
if (itemId) {
283282
const item = this.getToolboxItemById(itemId);
284-
if (item!.isSelectable()) {
283+
if (item?.isSelectable()) {
285284
this.setSelectedItem(item);
286285
(item as ISelectableToolboxItem).onClick(e);
287286
}
@@ -396,7 +395,7 @@ export class Toolbox
396395
const toolboxItemDef = toolboxDef[i];
397396
this.createToolboxItem(toolboxItemDef, fragment);
398397
}
399-
this.contentsDiv_!.appendChild(fragment);
398+
this.contentsDiv_?.appendChild(fragment);
400399
}
401400

402401
/**
@@ -435,9 +434,7 @@ export class Toolbox
435434
}
436435
// Adds the ID to the HTML element that can receive a click.
437436
// This is used in onClick_ to find the toolboxItem that was clicked.
438-
if (toolboxItem.getClickTarget()) {
439-
toolboxItem.getClickTarget()!.setAttribute('id', toolboxItem.getId());
440-
}
437+
toolboxItem.getClickTarget()?.setAttribute('id', toolboxItem.getId());
441438
}
442439
}
443440

@@ -722,7 +719,7 @@ export class Toolbox
722719
this.width_ = toolboxDiv.offsetWidth;
723720
this.height_ = workspaceMetrics.viewHeight;
724721
}
725-
this.flyout!.position();
722+
this.flyout?.position();
726723
}
727724

728725
/**
@@ -731,10 +728,11 @@ export class Toolbox
731728
* @internal
732729
*/
733730
handleToolboxItemResize() {
731+
if (!this.HtmlDiv) return;
734732
// Reposition the workspace so that (0,0) is in the correct position
735733
// relative to the new absolute edge (ie toolbox edge).
736734
const workspace = this.workspace_;
737-
const rect = this.HtmlDiv!.getBoundingClientRect();
735+
const rect = this.HtmlDiv.getBoundingClientRect();
738736
const flyout = this.getFlyout();
739737
const newX =
740738
this.toolboxPosition === toolbox.Position.LEFT
@@ -786,7 +784,7 @@ export class Toolbox
786784
this.selectedItem_.isSelectable() &&
787785
this.selectedItem_.getContents().length
788786
) {
789-
this.flyout!.show(this.selectedItem_.getContents());
787+
this.flyout?.show(this.selectedItem_.getContents());
790788
}
791789
}
792790

@@ -800,7 +798,9 @@ export class Toolbox
800798
return;
801799
}
802800

803-
this.HtmlDiv!.style.display = isVisible ? 'block' : 'none';
801+
if (this.HtmlDiv) {
802+
this.HtmlDiv.style.display = isVisible ? 'block' : 'none';
803+
}
804804
this.isVisible_ = isVisible;
805805
// Invisible toolbox is ignored as drag targets and must have the drag
806806
// target updated.
@@ -944,10 +944,10 @@ export class Toolbox
944944
(oldItem === newItem && !newItem.isCollapsible()) ||
945945
!newItem.getContents().length
946946
) {
947-
this.flyout!.hide();
947+
this.flyout?.hide();
948948
} else {
949-
this.flyout!.show(newItem.getContents());
950-
this.flyout!.scrollToStart();
949+
this.flyout?.show(newItem.getContents());
950+
this.flyout?.scrollToStart();
951951
}
952952
}
953953

@@ -992,10 +992,7 @@ export class Toolbox
992992
const collapsibleItem = this.selectedItem_ as ICollapsibleToolboxItem;
993993
collapsibleItem.toggleExpanded();
994994
return true;
995-
} else if (
996-
this.selectedItem_.getParent() &&
997-
this.selectedItem_.getParent()!.isSelectable()
998-
) {
995+
} else if (this.selectedItem_.getParent()?.isSelectable()) {
999996
this.setSelectedItem(this.selectedItem_.getParent());
1000997
return true;
1001998
}
@@ -1075,7 +1072,7 @@ export class Toolbox
10751072
/** Disposes of this toolbox. */
10761073
dispose() {
10771074
this.workspace_.getComponentManager().removeComponent('toolbox');
1078-
this.flyout!.dispose();
1075+
this.flyout?.dispose();
10791076
this.contents.forEach((item) => item.dispose());
10801077

10811078
for (let j = 0; j < this.boundEvents_.length; j++) {

0 commit comments

Comments
 (0)