Skip to content

Commit

Permalink
fix: make eventUtils throw if event type not registered (#6381)
Browse files Browse the repository at this point in the history
* chore: update lint rules

* fix: have eventUtils.get throw if event isn't found

* chore: remove nonnull assertion from eventUtils.get and format
  • Loading branch information
maribethb authored Aug 24, 2022
1 parent 980fe13 commit 60bc01a
Show file tree
Hide file tree
Showing 29 changed files with 98 additions and 94 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}],
"func-call-spacing": ["off"],
"@typescript-eslint/func-call-spacing": ["warn"],
// Temporarily disable. 23 problems.
"@typescript-eslint/no-explicit-any": ["off"],
// Temporarily disable. 128 problems.
Expand All @@ -132,8 +134,6 @@
"@typescript-eslint/no-empty-function": ["off"],
// Temporarily disable. 3 problems.
"@typescript-eslint/no-empty-interface": ["off"],
// Temporarily disable. 34 problems.
"func-call-spacing": ["off"],

// TsDoc rules (using JsDoc plugin)
// Disable built-in jsdoc verifier.
Expand Down
22 changes: 11 additions & 11 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class Block implements IASTNodeLocation, IDeletable {

// Fire a create event.
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))!(this));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(this));
}
} finally {
if (!existingGroup) {
Expand Down Expand Up @@ -323,7 +323,7 @@ export class Block implements IASTNodeLocation, IDeletable {

this.unplug(healStack);
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_DELETE))!(this));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_DELETE))(this));
}

if (this.onchangeWrapper_) {
Expand Down Expand Up @@ -1248,8 +1248,8 @@ export class Block implements IASTNodeLocation, IDeletable {
*/
setInputsInline(newBoolean: boolean) {
if (this.inputsInline !== newBoolean) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(this, 'inline', null, this.inputsInline, newBoolean));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
this, 'inline', null, this.inputsInline, newBoolean));
this.inputsInline = newBoolean;
}
}
Expand Down Expand Up @@ -1318,8 +1318,8 @@ export class Block implements IASTNodeLocation, IDeletable {
if (this.isEnabled() !== enabled) {
const oldValue = this.disabled;
this.disabled = !enabled;
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(this, 'disabled', null, oldValue, !enabled));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
this, 'disabled', null, oldValue, !enabled));
}
}

Expand Down Expand Up @@ -1357,8 +1357,8 @@ export class Block implements IASTNodeLocation, IDeletable {
*/
setCollapsed(collapsed: boolean) {
if (this.collapsed_ !== collapsed) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(this, 'collapsed', null, this.collapsed_, collapsed));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
this, 'collapsed', null, this.collapsed_, collapsed));
this.collapsed_ = collapsed;
}
}
Expand Down Expand Up @@ -2064,8 +2064,8 @@ export class Block implements IASTNodeLocation, IDeletable {
if (this.commentModel.text === text) {
return;
}
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(this, 'comment', null, this.commentModel.text, text));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
this, 'comment', null, this.commentModel.text, text));
this.commentModel.text = text;
// AnyDuringMigration because: Type 'string | null' is not assignable to
// type 'string | Comment'.
Expand Down Expand Up @@ -2111,7 +2111,7 @@ export class Block implements IASTNodeLocation, IDeletable {
throw Error('Block has parent.');
}
const event =
new (eventUtils.get(eventUtils.BLOCK_MOVE))!(this) as BlockMove;
new (eventUtils.get(eventUtils.BLOCK_MOVE))(this) as BlockMove;
this.xy_.translate(dx, dy);
event.recordNew();
eventUtils.fire(event);
Expand Down
12 changes: 6 additions & 6 deletions core/block_dragger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ export class BlockDragger implements IBlockDragger {

/** Fire a UI event at the start of a block drag. */
protected fireDragStartEvent_() {
const event = new (eventUtils.get(eventUtils.BLOCK_DRAG))!
(this.draggingBlock_, true, this.draggingBlock_.getDescendants(false));
const event = new (eventUtils.get(eventUtils.BLOCK_DRAG))(
this.draggingBlock_, true, this.draggingBlock_.getDescendants(false));
eventUtils.fire(event);
}

Expand Down Expand Up @@ -312,8 +312,8 @@ export class BlockDragger implements IBlockDragger {

/** Fire a UI event at the end of a block drag. */
protected fireDragEndEvent_() {
const event = new (eventUtils.get(eventUtils.BLOCK_DRAG))!
(this.draggingBlock_, false, this.draggingBlock_.getDescendants(false));
const event = new (eventUtils.get(eventUtils.BLOCK_DRAG))(
this.draggingBlock_, false, this.draggingBlock_.getDescendants(false));
eventUtils.fire(event);
}

Expand Down Expand Up @@ -352,8 +352,8 @@ export class BlockDragger implements IBlockDragger {

/** Fire a move event at the end of a block drag. */
protected fireMoveEvent_() {
const event = new (eventUtils.get(eventUtils.BLOCK_MOVE))!
(this.draggingBlock_) as BlockMove;
const event = new (eventUtils.get(eventUtils.BLOCK_MOVE))(
this.draggingBlock_) as BlockMove;
event.oldCoordinate = this.startXY_;
event.recordNew();
eventUtils.fire(event);
Expand Down
8 changes: 4 additions & 4 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
eventUtils.enable();
}
}
const event = new (eventUtils.get(eventUtils.SELECTED))!
(oldId, this.id, this.workspace.id);
const event = new (eventUtils.get(eventUtils.SELECTED))(
oldId, this.id, this.workspace.id);
eventUtils.fire(event);
common.setSelected(this);
this.addSelect();
Expand All @@ -294,8 +294,8 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
if (common.getSelected() !== this) {
return;
}
const event = new (eventUtils.get(eventUtils.SELECTED))!
(this.id, null, this.workspace.id);
const event = new (eventUtils.get(eventUtils.SELECTED))(
this.id, null, this.workspace.id);
event.workspaceId = this.workspace.id;
eventUtils.fire(event);
common.setSelected(null);
Expand Down
4 changes: 2 additions & 2 deletions core/bubble_dragger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ export class BubbleDragger {
/** Fire a move event at the end of a bubble drag. */
private fireMoveEvent_() {
if (this.bubble instanceof WorkspaceCommentSvg) {
const event = new (eventUtils.get(eventUtils.COMMENT_MOVE))!
(this.bubble) as CommentMove;
const event = new (eventUtils.get(eventUtils.COMMENT_MOVE))(
this.bubble) as CommentMove;
event.setOldCoordinate(this.startXY_);
event.recordNew();
eventUtils.fire(event);
Expand Down
10 changes: 5 additions & 5 deletions core/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ export class Comment extends Icon {
*/
function(this: Comment, _e: Event) {
if (this.cachedText_ !== this.model_.text) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(this.block_, 'comment', null, this.cachedText_,
this.model_.text));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
this.block_, 'comment', null, this.cachedText_,
this.model_.text));
}
});
this.onInputWrapper_ = browserEvents.conditionalBind(
Expand Down Expand Up @@ -232,8 +232,8 @@ export class Comment extends Icon {
if (visible === this.isVisible()) {
return;
}
eventUtils.fire(new (eventUtils.get(eventUtils.BUBBLE_OPEN))!
(this.block_, visible, 'comment'));
eventUtils.fire(new (eventUtils.get(eventUtils.BUBBLE_OPEN))(
this.block_, visible, 'comment'));
this.model_.pinned = visible;
if (visible) {
this.createBubble_();
Expand Down
4 changes: 2 additions & 2 deletions core/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class Connection implements IASTNodeLocationWithBlock {
let event;
if (eventUtils.isEnabled()) {
event =
new (eventUtils.get(eventUtils.BLOCK_MOVE))!(childBlock) as BlockMove;
new (eventUtils.get(eventUtils.BLOCK_MOVE))(childBlock) as BlockMove;
}
connectReciprocally(this, childConnection);
childBlock.setParent(parentBlock);
Expand Down Expand Up @@ -293,7 +293,7 @@ export class Connection implements IASTNodeLocationWithBlock {
let event;
if (eventUtils.isEnabled()) {
event =
new (eventUtils.get(eventUtils.BLOCK_MOVE))!(childBlock) as BlockMove;
new (eventUtils.get(eventUtils.BLOCK_MOVE))(childBlock) as BlockMove;
}
const otherConnection = this.targetConnection;
if (otherConnection) {
Expand Down
2 changes: 1 addition & 1 deletion core/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export function callbackFactory(block: Block, xml: Element): Function {
eventUtils.enable();
}
if (eventUtils.isEnabled() && !newBlock.isShadow()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))!(newBlock));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(newBlock));
}
newBlock.select();
};
Expand Down
2 changes: 2 additions & 0 deletions core/dropdowndiv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ export function show(
const internal = {
/**
* Get sizing info about the bounding element.
*
* @return An object containing size information about the bounding element
* (bounding box and width/height).
*/
Expand All @@ -348,6 +349,7 @@ const internal = {
/**
* Helper to position the drop-down and the arrow, maintaining bounds.
* See explanation of origin points in show.
*
* @param primaryX Desired origin point x, in absolute px.
* @param primaryY Desired origin point y, in absolute px.
* @param secondaryX Secondary/alternative origin point x, in absolute px.
Expand Down
10 changes: 7 additions & 3 deletions core/events/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,16 @@ export function fromJson(
* Gets the class for a specific event type from the registry.
*
* @param eventType The type of the event to get.
* @returns The event class with the given type or null if none exists.
* @returns The event class with the given type.
* @alias Blockly.Events.utils.get
*/
export function get(eventType: string):
(new (...p1: AnyDuringMigration[]) => Abstract)|null {
return registry.getClass(registry.Type.EVENT, eventType);
(new (...p1: AnyDuringMigration[]) => Abstract) {
const event = registry.getClass(registry.Type.EVENT, eventType);
if (!event) {
throw new Error(`Event type ${eventType} not found in registry.`);
}
return event;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,8 @@ export abstract class Field implements IASTNodeLocationSvg,

this.doValueUpdate_(newValue);
if (source && eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(source, 'field', this.name || null, oldValue, newValue));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
source, 'field', this.name || null, oldValue, newValue));
}
if (this.isDirty_) {
this.forceRerender();
Expand Down
6 changes: 3 additions & 3 deletions core/field_textinput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ export class FieldTextInput extends Field {
// Revert value when the text becomes invalid.
this.value_ = this.htmlInput_!.getAttribute('data-untyped-default-value');
if (this.sourceBlock_ && eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(this.sourceBlock_, 'field', this.name || null,
oldValue, this.value_));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
this.sourceBlock_, 'field', this.name || null, oldValue,
this.value_));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/flyout_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,13 +991,13 @@ export abstract class Flyout extends DeleteArea implements IFlyout {
// Fire a VarCreate event for each (if any) new variable created.
for (let i = 0; i < newVariables.length; i++) {
const thisVariable = newVariables[i];
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_CREATE))!
(thisVariable));
eventUtils.fire(
new (eventUtils.get(eventUtils.VAR_CREATE))(thisVariable));
}

// Block events come after var events, in case they refer to newly created
// variables.
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))!(newBlock));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(newBlock));
}
if (this.autoClose) {
this.hide();
Expand Down
8 changes: 4 additions & 4 deletions core/gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ export class Gesture {
* @param ws The workspace that a user clicks on.
*/
private fireWorkspaceClick_(ws: WorkspaceSvg) {
eventUtils.fire(new (eventUtils.get(eventUtils.CLICK))!
(null, ws.id, 'workspace'));
eventUtils.fire(
new (eventUtils.get(eventUtils.CLICK))(null, ws.id, 'workspace'));
}

/**
Expand Down Expand Up @@ -712,8 +712,8 @@ export class Gesture {
}
} else {
// Clicks events are on the start block, even if it was a shadow.
const event = new (eventUtils.get(eventUtils.CLICK))!
(this.startBlock_, this.startWorkspace_.id, 'block');
const event = new (eventUtils.get(eventUtils.CLICK))(
this.startBlock_, this.startWorkspace_.id, 'block');
eventUtils.fire(event);
}
this.bringBlockToFront_();
Expand Down
6 changes: 3 additions & 3 deletions core/mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ export class Mutator extends Icon {
// No change.
return;
}
eventUtils.fire(new (eventUtils.get(eventUtils.BUBBLE_OPEN))!
(this.block_, visible, 'mutator'));
eventUtils.fire(new (eventUtils.get(eventUtils.BUBBLE_OPEN))(
this.block_, visible, 'mutator'));
if (visible) {
// Create the bubble.
this.bubble_ = new Bubble(
Expand Down Expand Up @@ -460,7 +460,7 @@ export class Mutator extends Icon {

const newExtraState = BlockChange.getExtraBlockState_(block);
if (oldExtraState !== newExtraState) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!(
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
block, 'mutation', null, oldExtraState, newExtraState));
// Ensure that any bump is part of this mutation's event group.
const mutationGroup = eventUtils.getGroup();
Expand Down
4 changes: 2 additions & 2 deletions core/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ export function mutateCallers(defBlock: Block) {
// undo action since it is deterministically tied to the procedure's
// definition mutation.
eventUtils.setRecordUndo(false);
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))!
(caller, 'mutation', null, oldMutation, newMutation));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
caller, 'mutation', null, oldMutation, newMutation));
eventUtils.setRecordUndo(oldRecordUndo);
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/renderers/common/marker_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ export class MarkerSvg {
*/
private fireMarkerEvent_(oldNode: ASTNode, curNode: ASTNode) {
const curBlock = curNode.getSourceBlock();
const event = new (eventUtils.get(eventUtils.MARKER_MOVE))!
(curBlock, this.isCursor(), oldNode, curNode);
const event = new (eventUtils.get(eventUtils.MARKER_MOVE))(
curBlock, this.isCursor(), oldNode, curNode);
eventUtils.fire(event);
}

Expand Down
2 changes: 1 addition & 1 deletion core/serialization/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export function appendInternal(
const block = appendPrivate(state, workspace, {parentConnection, isShadow});

eventUtils.enable();
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))!(block));
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block));
eventUtils.setGroup(existingGroup);
eventUtils.setRecordUndo(prevRecordUndo);

Expand Down
3 changes: 1 addition & 2 deletions core/serialization/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export function load(
}
dom.stopTextWidthCache();

eventUtils.fire(new (eventUtils.get(eventUtils.FINISHED_LOADING))!
(workspace));
eventUtils.fire(new (eventUtils.get(eventUtils.FINISHED_LOADING))(workspace));

eventUtils.setGroup(existingGroup);
eventUtils.setRecordUndo(prevRecordUndo);
Expand Down
4 changes: 2 additions & 2 deletions core/toolbox/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ export class Toolbox extends DeleteArea implements IAutoHideable,
if (oldItem === newItem) {
newElement = null;
}
const event = new (eventUtils.get(eventUtils.TOOLBOX_ITEM_SELECT))!
(oldElement, newElement, this.workspace_.id);
const event = new (eventUtils.get(eventUtils.TOOLBOX_ITEM_SELECT))(
oldElement, newElement, this.workspace_.id);
eventUtils.fire(event);
}

Expand Down
4 changes: 2 additions & 2 deletions core/trashcan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ export class Trashcan extends DeleteArea implements IAutoHideable,
* @param trashcanOpen Whether the flyout is opening.
*/
private fireUiEvent_(trashcanOpen: boolean) {
const uiEvent = new (eventUtils.get(eventUtils.TRASHCAN_OPEN))!
(trashcanOpen, this.workspace.id);
const uiEvent = new (eventUtils.get(eventUtils.TRASHCAN_OPEN))(
trashcanOpen, this.workspace.id);
eventUtils.fire(uiEvent);
}

Expand Down
Loading

0 comments on commit 60bc01a

Please sign in to comment.