Skip to content

Commit

Permalink
chore: Remove some more uses of AnyDuringMigration (#6785)
Browse files Browse the repository at this point in the history
* chore: Remove uses of AnyDuringMigration in workspace_comment_svg.ts.

* chore: Remove uses of AnyDuringMigration in toolbox.ts.

* chore: Remove uses of AnyDuringMigration in field_colour.ts.

* chore: Remove uses of AnyDuringMigration from field_image.ts.

* chore: Remove uses of AnyDuringMigration from workspace.ts.

* Update core/workspace_comment_svg.ts

Co-authored-by: Christopher Allen <[email protected]>

* chore: Remove uses of AnyDuringMigration in collapsible_category.ts.

* chore: Revert unary - change to make clang-format happy.

* chore: Remove unnecessary quotes in toolbox.ts.

* Update core/workspace_comment_svg.ts

Co-authored-by: Christopher Allen <[email protected]>

---------

Co-authored-by: Christopher Allen <[email protected]>
  • Loading branch information
gonfunko and cpcallen authored Feb 3, 2023
1 parent a73a70b commit 299016e
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 222 deletions.
6 changes: 3 additions & 3 deletions core/events/events_comment_move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class CommentMove extends CommentBase {
}

this.comment_ = opt_comment;
this.oldCoordinate_ = opt_comment.getXY();
this.oldCoordinate_ = opt_comment.getRelativeToSurfaceXY();
}

/**
Expand All @@ -70,7 +70,7 @@ export class CommentMove extends CommentBase {
'The comment is undefined. Pass a comment to ' +
'the constructor if you want to use the record functionality');
}
this.newCoordinate_ = this.comment_.getXY();
this.newCoordinate_ = this.comment_.getRelativeToSurfaceXY();
}

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ export class CommentMove extends CommentBase {
'or call fromJson');
}
// TODO: Check if the comment is being dragged, and give up if so.
const current = comment.getXY();
const current = comment.getRelativeToSurfaceXY();
comment.moveBy(target.x - current.x, target.y - current.y);
}
}
Expand Down
28 changes: 9 additions & 19 deletions core/field_colour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,13 @@ export class FieldColour extends Field<string> {
protected override isDirty_ = false;

/** Array of colours used by this field. If null, use the global list. */
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'string[]'.
private colours_: string[] = null as AnyDuringMigration;
private colours_: string[]|null = null;

/**
* Array of colour tooltips used by this field. If null, use the global
* list.
*/
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'string[]'.
private titles_: string[] = null as AnyDuringMigration;
private titles_: string[]|null = null;

/**
* Number of colour columns used by this field. If 0, use the global
Expand Down Expand Up @@ -211,8 +207,7 @@ export class FieldColour extends Field<string> {
* @param opt_newValue The input value.
* @returns A valid colour, or null if invalid.
*/
protected override doClassValidation_(opt_newValue?: AnyDuringMigration):
string|null {
protected override doClassValidation_(opt_newValue?: any): string|null {
if (typeof opt_newValue !== 'string') {
return null;
}
Expand Down Expand Up @@ -415,19 +410,15 @@ export class FieldColour extends Field<string> {

/** Handle a mouse enter event. Focus the picker. */
private onMouseEnter_() {
// AnyDuringMigration because: Property 'focus' does not exist on type
// 'Element'.
(this.picker_ as AnyDuringMigration)!.focus({preventScroll: true});
this.picker_?.focus({preventScroll: true});
}

/**
* Handle a mouse leave event. Blur the picker and unhighlight
* the currently highlighted colour.
*/
private onMouseLeave_() {
// AnyDuringMigration because: Property 'blur' does not exist on type
// 'Element'.
(this.picker_ as AnyDuringMigration)!.blur();
this.picker_?.blur();
const highlighted = this.getHighlighted_();
if (highlighted) {
dom.removeClass(highlighted, 'blocklyColourHighlighted');
Expand Down Expand Up @@ -473,11 +464,10 @@ export class FieldColour extends Field<string> {
this.highlightedIndex_ = index;

// Update accessibility roles.
// AnyDuringMigration because: Argument of type 'string | null' is not
// assignable to parameter of type 'string | number | boolean | string[]'.
aria.setState(
this.picker_ as Element, aria.State.ACTIVEDESCENDANT,
cell.getAttribute('id') as AnyDuringMigration);
const cellId = cell.getAttribute('id');
if (cellId && this.picker_) {
aria.setState(this.picker_, aria.State.ACTIVEDESCENDANT, cellId);
}
}

/** Create a colour picker dropdown editor. */
Expand Down
13 changes: 5 additions & 8 deletions core/field_image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ export class FieldImage extends Field<string> {
private readonly imageHeight_: number;

/** The function to be called when this field is clicked. */
private clickHandler_: ((p1: FieldImage) => AnyDuringMigration)|null = null;
private clickHandler_: ((p1: FieldImage) => void)|null = null;

/** The rendered field's image element. */
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGImageElement'.
private imageElement_: SVGImageElement = null as AnyDuringMigration;
private imageElement_: SVGImageElement|null = null;

/**
* Editable fields usually show some sort of UI indicating they are
Expand Down Expand Up @@ -79,7 +77,7 @@ export class FieldImage extends Field<string> {
*/
constructor(
src: string|Sentinel, width: string|number, height: string|number,
opt_alt?: string, opt_onClick?: (p1: FieldImage) => AnyDuringMigration,
opt_alt?: string, opt_onClick?: (p1: FieldImage) => void,
opt_flipRtl?: boolean, opt_config?: FieldImageConfig) {
super(Field.SKIP_SETUP);

Expand Down Expand Up @@ -164,8 +162,7 @@ export class FieldImage extends Field<string> {
* @param opt_newValue The input value.
* @returns A string, or null if invalid.
*/
protected override doClassValidation_(opt_newValue?: AnyDuringMigration):
string|null {
protected override doClassValidation_(opt_newValue?: any): string|null {
if (typeof opt_newValue !== 'string') {
return null;
}
Expand Down Expand Up @@ -226,7 +223,7 @@ export class FieldImage extends Field<string> {
* @param func The function that is called when the image is clicked, or null
* to remove.
*/
setOnClickHandler(func: ((p1: FieldImage) => AnyDuringMigration)|null) {
setOnClickHandler(func: ((p1: FieldImage) => void)|null) {
this.clickHandler_ = func;
}

Expand Down
2 changes: 1 addition & 1 deletion core/interfaces/i_copyable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export namespace ICopyable {
export interface CopyData {
saveInfo: Object|Element;
source: WorkspaceSvg;
typeCounts: Object|null;
typeCounts: {[key: string]: number}|null;
}
}

Expand Down
8 changes: 8 additions & 0 deletions core/interfaces/i_selectable_toolbox_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ export interface ISelectableToolboxItem extends IToolboxItem {
*/
onClick(_e: Event): void;
}

/**
* Type guard that checks whether an IToolboxItem is an ISelectableToolboxItem.
*/
export function isSelectableToolboxItem(toolboxItem: IToolboxItem):
toolboxItem is ISelectableToolboxItem {
return toolboxItem.isSelectable();
}
33 changes: 12 additions & 21 deletions core/toolbox/collapsible_category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export class CollapsibleToolboxCategory extends ToolboxCategory implements

/** The child toolbox items for this category. */
protected toolboxItems_: IToolboxItem[] = [];
override flyoutItems_: AnyDuringMigration;
override isHidden_: AnyDuringMigration;

/**
* @param categoryDef The information needed to create a category in the
Expand All @@ -60,25 +58,18 @@ export class CollapsibleToolboxCategory extends ToolboxCategory implements

override makeDefaultCssConfig_() {
const cssConfig = super.makeDefaultCssConfig_();
(cssConfig as AnyDuringMigration)['contents'] = 'blocklyToolboxContents';
cssConfig['contents'] = 'blocklyToolboxContents';
return cssConfig;
}

override parseContents_(categoryDef: toolbox.CategoryInfo) {
// AnyDuringMigration because: Element implicitly has an 'any' type because
// expression of type '"contents"' can't be used to index type
// 'CategoryInfo'.
const contents = (categoryDef as AnyDuringMigration)['contents'];
let prevIsFlyoutItem = true;

// AnyDuringMigration because: Element implicitly has an 'any' type because
// expression of type '"custom"' can't be used to index type 'CategoryInfo'.
if ((categoryDef as AnyDuringMigration)['custom']) {
// AnyDuringMigration because: Element implicitly has an 'any' type
// because expression of type '"custom"' can't be used to index type
// 'CategoryInfo'.
this.flyoutItems_ = (categoryDef as AnyDuringMigration)['custom'];
} else if (contents) {
if ('custom' in categoryDef) {
this.flyoutItems_ = categoryDef['custom'];
} else {
const contents = categoryDef['contents'];
if (!contents) return;
this.flyoutItems_ = [];
let prevIsFlyoutItem = true;
for (let i = 0; i < contents.length; i++) {
const itemDef = contents[i];
// Separators can exist as either a flyout item or a toolbox item so
Expand Down Expand Up @@ -123,8 +114,8 @@ export class CollapsibleToolboxCategory extends ToolboxCategory implements
super.init();

this.setExpanded(
(this.toolboxItemDef_ as AnyDuringMigration)['expanded'] === 'true' ||
(this.toolboxItemDef_ as AnyDuringMigration)['expanded']);
this.toolboxItemDef_['expanded'] === 'true' ||
!!this.toolboxItemDef_['expanded']);
}

override createDom_() {
Expand All @@ -141,7 +132,7 @@ export class CollapsibleToolboxCategory extends ToolboxCategory implements
override createIconDom_() {
const toolboxIcon = document.createElement('span');
if (!this.parentToolbox_.isHorizontal()) {
const className = (this.cssConfig_ as AnyDuringMigration)['icon'];
const className = this.cssConfig_['icon'];
if (className) {
dom.addClass(toolboxIcon, className);
}
Expand All @@ -161,7 +152,7 @@ export class CollapsibleToolboxCategory extends ToolboxCategory implements
protected createSubCategoriesDom_(subcategories: IToolboxItem[]):
HTMLDivElement {
const contentsContainer = document.createElement('div');
const className = (this.cssConfig_ as AnyDuringMigration)['contents'];
const className = this.cssConfig_['contents'];
if (className) {
dom.addClass(contentsContainer, className);
}
Expand Down
48 changes: 16 additions & 32 deletions core/toolbox/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {IDraggable} from '../interfaces/i_draggable.js';
import type {IFlyout} from '../interfaces/i_flyout.js';
import type {IKeyboardAccessible} from '../interfaces/i_keyboard_accessible.js';
import type {ISelectableToolboxItem} from '../interfaces/i_selectable_toolbox_item.js';
import {isSelectableToolboxItem} from '../interfaces/i_selectable_toolbox_item.js';
import type {IStyleable} from '../interfaces/i_styleable.js';
import type {IToolbox} from '../interfaces/i_toolbox.js';
import type {IToolboxItem} from '../interfaces/i_toolbox_item.js';
Expand Down Expand Up @@ -100,7 +101,6 @@ export class Toolbox extends DeleteArea implements IAutoHideable,
* Ex: [[node, name, func], [node, name, func]].
*/
protected boundEvents_: browserEvents.Data[] = [];
override wouldDelete_: AnyDuringMigration;

/** The workspace this toolbox is on. */
protected readonly workspace_: WorkspaceSvg;
Expand All @@ -112,10 +112,9 @@ export class Toolbox extends DeleteArea implements IAutoHideable,
this.workspace_ = workspace;

/** The JSON describing the contents of this toolbox. */
// AnyDuringMigration because: Type 'ToolboxInfo | { contents: never[]; }'
// is not assignable to type 'ToolboxInfo'.
this.toolboxDef_ = (workspace.options.languageTree || {'contents': []}) as
AnyDuringMigration;
this.toolboxDef_ =
(workspace.options.languageTree ||
{contents: new Array<toolbox.ToolboxItemInfo>()});

/** Whether the toolbox should be laid out horizontally. */
this.horizontalLayout_ = workspace.options.horizontalLayout;
Expand Down Expand Up @@ -299,9 +298,8 @@ export class Toolbox extends DeleteArea implements IAutoHideable,
if (!handled && this.selectedItem_) {
// TODO(#6097): Figure out who implements onKeyDown and which interface it
// should be part of.
const untypedItem = this.selectedItem_ as AnyDuringMigration;
if (untypedItem.onKeyDown) {
handled = untypedItem.onKeyDown(e);
if ((this.selectedItem_ as any).onKeyDown) {
handled = (this.selectedItem_ as any).onKeyDown(e);
}
}

Expand Down Expand Up @@ -796,33 +794,20 @@ export class Toolbox extends DeleteArea implements IAutoHideable,
setSelectedItem(newItem: IToolboxItem|null) {
const oldItem = this.selectedItem_;

if (!newItem && !oldItem || newItem && !newItem.isSelectable()) {
if (!newItem && !oldItem || newItem && !isSelectableToolboxItem(newItem)) {
return;
}
newItem = newItem as ISelectableToolboxItem;

// AnyDuringMigration because: Argument of type 'IToolboxItem' is not
// assignable to parameter of type 'ISelectableToolboxItem'.
if (this.shouldDeselectItem_(oldItem, newItem as AnyDuringMigration) &&
oldItem !== null) {
if (this.shouldDeselectItem_(oldItem, newItem) && oldItem !== null) {
this.deselectItem_(oldItem);
}

// AnyDuringMigration because: Argument of type 'IToolboxItem' is not
// assignable to parameter of type 'ISelectableToolboxItem'.
if (this.shouldSelectItem_(oldItem, newItem as AnyDuringMigration) &&
newItem !== null) {
// AnyDuringMigration because: Argument of type 'IToolboxItem' is not
// assignable to parameter of type 'ISelectableToolboxItem'.
this.selectItem_(oldItem, newItem as AnyDuringMigration);
if (this.shouldSelectItem_(oldItem, newItem) && newItem !== null) {
this.selectItem_(oldItem, newItem);
}

// AnyDuringMigration because: Argument of type 'IToolboxItem' is not
// assignable to parameter of type 'ISelectableToolboxItem'.
this.updateFlyout_(oldItem, newItem as AnyDuringMigration);
// AnyDuringMigration because: Argument of type 'IToolboxItem' is not
// assignable to parameter of type 'ISelectableToolboxItem'.
this.fireSelectEvent_(oldItem, newItem as AnyDuringMigration);
this.updateFlyout_(oldItem, newItem);
this.fireSelectEvent_(oldItem, newItem);
}

/**
Expand Down Expand Up @@ -1044,11 +1029,10 @@ export class Toolbox extends DeleteArea implements IAutoHideable,
this.boundEvents_ = [];
this.contents_ = [];

// AnyDuringMigration because: Argument of type 'HTMLDivElement | null' is
// not assignable to parameter of type 'Element'.
this.workspace_.getThemeManager().unsubscribe(
this.HtmlDiv as AnyDuringMigration);
dom.removeNode(this.HtmlDiv);
if (this.HtmlDiv) {
this.workspace_.getThemeManager().unsubscribe(this.HtmlDiv);
dom.removeNode(this.HtmlDiv);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/utils/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface StaticCategoryInfo {
colour: string|undefined;
cssconfig: CategoryCssConfig|undefined;
hidden: string|undefined;
expanded?: string|boolean;
}

/**
Expand All @@ -114,6 +115,7 @@ export interface DynamicCategoryInfo {
colour: string|undefined;
cssconfig: CategoryCssConfig|undefined;
hidden: string|undefined;
expanded?: string|boolean;
}

/**
Expand Down
Loading

0 comments on commit 299016e

Please sign in to comment.