Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove more uses of AnyDuringMigration #6863

Merged
merged 10 commits into from
Feb 28, 2023
72 changes: 26 additions & 46 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Block implements IASTNodeLocation, IDeletable {
* changes. This is usually only called from the constructor, the block type
* initializer function, or an extension initializer function.
*/
onchange?: ((p1: Abstract) => AnyDuringMigration)|null;
onchange?: ((p1: Abstract) => void)|null;

/** The language-neutral ID given to the collapsed input. */
static readonly COLLAPSED_INPUT_NAME: string = constants.COLLAPSED_INPUT_NAME;
Expand Down Expand Up @@ -91,39 +91,38 @@ export class Block implements IASTNodeLocation, IDeletable {
protected styleName_ = '';

/** An optional method called during initialization. */
init?: (() => AnyDuringMigration)|null = undefined;
init?: (() => void);

/** An optional method called during disposal. */
destroy?: (() => void) = undefined;
destroy?: (() => void);

/**
* An optional serialization method for defining how to serialize the
* mutation state to XML. This must be coupled with defining
* `domToMutation`.
*/
mutationToDom?: ((...p1: AnyDuringMigration[]) => Element)|null = undefined;
mutationToDom?: (...p1: AnyDuringMigration[]) => Element;

/**
* An optional deserialization method for defining how to deserialize the
* mutation state from XML. This must be coupled with defining
* `mutationToDom`.
*/
domToMutation?: ((p1: Element) => AnyDuringMigration)|null = undefined;
domToMutation?: (p1: Element) => void;

/**
* An optional serialization method for defining how to serialize the
* block's extra state (eg mutation state) to something JSON compatible.
* This must be coupled with defining `loadExtraState`.
*/
saveExtraState?: (() => AnyDuringMigration)|null = undefined;
saveExtraState?: () => AnyDuringMigration;

/**
* An optional serialization method for defining how to deserialize the
* block's extra state (eg mutation state) from something JSON compatible.
* This must be coupled with defining `saveExtraState`.
*/
loadExtraState?:
((p1: AnyDuringMigration) => AnyDuringMigration)|null = undefined;
loadExtraState?: (p1: AnyDuringMigration) => void;

/**
* An optional property for suppressing adding STATEMENT_PREFIX and
Expand All @@ -137,31 +136,25 @@ export class Block implements IASTNodeLocation, IDeletable {
* shown to the user, but are declared as global variables in the generated
* code.
*/
getDeveloperVariables?: (() => string[]) = undefined;
getDeveloperVariables?: () => string[];

/**
* An optional function that reconfigures the block based on the contents of
* the mutator dialog.
*/
compose?: ((p1: Block) => void) = undefined;
compose?: (p1: Block) => void;

/**
* An optional function that populates the mutator's dialog with
* this block's components.
*/
decompose?: ((p1: Workspace) => Block) = undefined;
decompose?: (p1: Workspace) => Block;
id: string;
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'Connection'.
outputConnection: Connection = null as AnyDuringMigration;
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'Connection'.
nextConnection: Connection = null as AnyDuringMigration;
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'Connection'.
previousConnection: Connection = null as AnyDuringMigration;
outputConnection: Connection|null = null;
nextConnection: Connection|null = null;
previousConnection: Connection|null = null;
inputList: Input[] = [];
inputsInline?: boolean = undefined;
inputsInline?: boolean;
private disabled = false;
tooltip: Tooltip.TipInfo = '';
contextMenu = true;
Expand Down Expand Up @@ -203,19 +196,17 @@ export class Block implements IASTNodeLocation, IDeletable {
protected isInsertionMarker_ = false;

/** Name of the type of hat. */
hat?: string = undefined;
hat?: string;

rendered: boolean|null = null;

/**
* String for block help, or function that returns a URL. Null for no help.
*/
// AnyDuringMigration because: Type 'null' is not assignable to type 'string
// | Function'.
helpUrl: string|Function = null as AnyDuringMigration;
helpUrl: string|Function|null = null;

/** A bound callback function to use when the parent workspace changes. */
private onchangeWrapper_: ((p1: Abstract) => AnyDuringMigration)|null = null;
private onchangeWrapper_: ((p1: Abstract) => void)|null = null;

/**
* A count of statement inputs on the block.
Expand Down Expand Up @@ -422,7 +413,7 @@ export class Block implements IASTNodeLocation, IDeletable {
*/
private unplugFromRow_(opt_healStack?: boolean) {
let parentConnection = null;
if (this.outputConnection.isConnected()) {
if (this.outputConnection?.isConnected()) {
parentConnection = this.outputConnection.targetConnection;
// Disconnect from any superior block.
this.outputConnection.disconnect();
Expand Down Expand Up @@ -487,7 +478,7 @@ export class Block implements IASTNodeLocation, IDeletable {
*/
private unplugFromStack_(opt_healStack?: boolean) {
let previousTarget = null;
if (this.previousConnection.isConnected()) {
if (this.previousConnection?.isConnected()) {
// Remember the connection that any next statements need to connect to.
previousTarget = this.previousConnection.targetConnection;
// Detach this block from the parent's tree.
Expand All @@ -496,7 +487,7 @@ export class Block implements IASTNodeLocation, IDeletable {
const nextBlock = this.getNextBlock();
if (opt_healStack && nextBlock && !nextBlock.isShadow()) {
// Disconnect the next statement.
const nextTarget = this.nextConnection.targetConnection;
const nextTarget = this.nextConnection?.targetConnection ?? null;
nextTarget?.disconnect();
if (previousTarget &&
this.workspace.connectionChecker.canConnect(
Expand Down Expand Up @@ -1052,7 +1043,7 @@ export class Block implements IASTNodeLocation, IDeletable {
* @param onchangeFn The callback to call when the block's workspace changes.
* @throws {Error} if onchangeFn is not falsey and not a function.
*/
setOnChange(onchangeFn: (p1: Abstract) => AnyDuringMigration) {
setOnChange(onchangeFn: (p1: Abstract) => void) {
if (onchangeFn && typeof onchangeFn !== 'function') {
throw Error('onchange must be a function.');
}
Expand Down Expand Up @@ -1219,9 +1210,7 @@ export class Block implements IASTNodeLocation, IDeletable {
'connection.');
}
this.previousConnection.dispose();
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'Connection'.
this.previousConnection = null as AnyDuringMigration;
this.previousConnection = null;
}
}
}
Expand Down Expand Up @@ -1251,9 +1240,7 @@ export class Block implements IASTNodeLocation, IDeletable {
'connection.');
}
this.nextConnection.dispose();
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'Connection'.
this.nextConnection = null as AnyDuringMigration;
this.nextConnection = null;
}
}
}
Expand Down Expand Up @@ -1282,9 +1269,7 @@ export class Block implements IASTNodeLocation, IDeletable {
'Must disconnect output value before removing connection.');
}
this.outputConnection.dispose();
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'Connection'.
this.outputConnection = null as AnyDuringMigration;
this.outputConnection = null;
}
}
}
Expand Down Expand Up @@ -1962,10 +1947,7 @@ export class Block implements IASTNodeLocation, IDeletable {
if (type === inputTypes.STATEMENT) {
this.statementInputCount++;
}
// AnyDuringMigration because: Argument of type 'Connection | null' is not
// assignable to parameter of type 'Connection'.
const input =
new Input(type, name, this, (connection as AnyDuringMigration));
const input = new Input(type, name, this, connection);
// Append input to list.
this.inputList.push(input);
return input;
Expand Down Expand Up @@ -2109,9 +2091,7 @@ export class Block implements IASTNodeLocation, IDeletable {
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'.
this.comment = text as AnyDuringMigration; // For backwards compatibility.
this.comment = text; // For backwards compatibility.
}

/**
Expand Down
48 changes: 5 additions & 43 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
* the block.
*/
static readonly COLLAPSED_WARNING_ID = 'TEMP_COLLAPSED_WARNING_';
override decompose?: ((p1: Workspace) => BlockSvg);
override decompose?: (p1: Workspace) => BlockSvg;
// override compose?: ((p1: BlockSvg) => void)|null;
saveConnections?: ((p1: BlockSvg) => AnyDuringMigration);
saveConnections?: (p1: BlockSvg) => void;
customContextMenu?:
((p1: Array<ContextMenuOption|LegacyContextMenuOption>) =>
AnyDuringMigration)|null;
(p1: Array<ContextMenuOption|LegacyContextMenuOption>) => void;

/**
* An property used internally to reference the block's rendering debugger.
Expand Down Expand Up @@ -153,34 +152,6 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
constructor(workspace: WorkspaceSvg, prototypeName: string, opt_id?: string) {
super(workspace, prototypeName, opt_id);
this.workspace = workspace;

/**
* An optional method called when a mutator dialog is first opened.
* This function must create and initialize a top-level block for the
* mutator dialog, and return it. This function should also populate this
* top-level block with any sub-blocks which are appropriate. This method
* must also be coupled with defining a `compose` method for the default
* mutation dialog button and UI to appear.
*/
this.decompose = this.decompose;

/**
* An optional method called when a mutator dialog saves its content.
* This function is called to modify the original block according to new
* settings. This method must also be coupled with defining a `decompose`
* method for the default mutation dialog button and UI to appear.
*/
this.compose = this.compose;

/**
* An optional method called by the default mutator UI which gives the block
* a chance to save information about what child blocks are connected to
* what mutated connections.
*/
this.saveConnections = this.saveConnections;

/** An optional method for defining custom block context menu items. */
this.customContextMenu = this.customContextMenu;
this.svgGroup_ = dom.createSvgElement(Svg.G, {});

/** A block style object. */
Expand All @@ -191,7 +162,7 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
workspace.getRenderer().makePathObject(this.svgGroup_, this.style);

const svgPath = this.pathObject.svgPath;
(svgPath as AnyDuringMigration).tooltip = this;
(svgPath as any).tooltip = this;
gonfunko marked this conversation as resolved.
Show resolved Hide resolved
Tooltip.bindMouseEvents(svgPath);

// Expose this block's ID on its top-level SVG group.
Expand Down Expand Up @@ -639,11 +610,8 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
if (this.workspace.options.readOnly || !this.contextMenu) {
return null;
}
// AnyDuringMigration because: Argument of type '{ block: this; }' is not
// assignable to parameter of type 'Scope'.
const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions(
ContextMenuRegistry.ScopeType.BLOCK,
{block: this} as AnyDuringMigration);
ContextMenuRegistry.ScopeType.BLOCK, {block: this});

// Allow the block to add or modify menuOptions.
if (this.customContextMenu) {
Expand Down Expand Up @@ -839,10 +807,6 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,

dom.removeNode(this.svgGroup_);
blockWorkspace.resizeContents();
// Sever JavaScript to DOM connections.
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGGElement'.
this.svgGroup_ = null as AnyDuringMigration;
dom.stopTextWidthCache();
}

Expand Down Expand Up @@ -944,8 +908,6 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg,
* @param text The text, or null to delete.
*/
override setCommentText(text: string|null) {
// AnyDuringMigration because: Property 'get' does not exist on type
// '(name: string) => void'.
if (this.commentModel.text === text) {
return;
}
Expand Down
7 changes: 4 additions & 3 deletions core/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ export class Connection implements IASTNodeLocationWithBlock {
if (orphan) {
const orphanConnection = this.type === INPUT ? orphan.outputConnection :
orphan.previousConnection;
if (!orphanConnection) return;
const connection = Connection.getConnectionForOrphanedConnection(
childBlock, (orphanConnection));
childBlock, orphanConnection);
if (connection) {
orphanConnection.connect(connection);
} else {
Expand Down Expand Up @@ -676,11 +677,11 @@ function getSingleConnection(block: Block, orphanBlock: Block): Connection|
null {
let foundConnection = null;
const output = orphanBlock.outputConnection;
const typeChecker = output.getConnectionChecker();
const typeChecker = output?.getConnectionChecker();

for (let i = 0, input; input = block.inputList[i]; i++) {
const connection = input.connection;
if (connection && typeChecker.canConnect(output, connection, false)) {
if (connection && typeChecker?.canConnect(output, connection, false)) {
if (foundConnection) {
return null; // More than one connection.
}
Expand Down
4 changes: 2 additions & 2 deletions core/events/events_block_move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class BlockMove extends BlockBase {
blockConnection = block.previousConnection;
}
let parentConnection;
const connectionType = blockConnection.type;
const connectionType = blockConnection?.type;
if (inputName) {
const input = parentBlock!.getInput(inputName);
if (input) {
Expand All @@ -270,7 +270,7 @@ export class BlockMove extends BlockBase {
} else if (connectionType === ConnectionType.PREVIOUS_STATEMENT) {
parentConnection = parentBlock!.nextConnection;
}
if (parentConnection) {
if (parentConnection && blockConnection) {
blockConnection.connect(parentConnection);
} else {
console.warn('Can\'t connect to non-existent input: ' + inputName);
Expand Down
20 changes: 5 additions & 15 deletions core/field_angle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ export class FieldAngle extends FieldInput<number> {
line_: SVGLineElement|null = null;

/** The degree symbol for this field. */
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGTSpanElement'.
protected symbol_: SVGTSpanElement = null as AnyDuringMigration;
protected symbol_: SVGTSpanElement|null = null;

/** Wrapper click event data. */
private clickWrapper_: browserEvents.Data|null = null;
Expand Down Expand Up @@ -213,10 +211,7 @@ export class FieldAngle extends FieldInput<number> {
this.sourceBlock_.style.colourTertiary);
}

// AnyDuringMigration because: Argument of type 'this' is not assignable to
// parameter of type 'Field'.
dropDownDiv.showPositionedByField(
this as AnyDuringMigration, this.dropdownDispose_.bind(this));
dropDownDiv.showPositionedByField(this, this.dropdownDispose_.bind(this));

this.updateGraph_();
}
Expand Down Expand Up @@ -387,12 +382,8 @@ export class FieldAngle extends FieldInput<number> {
' 0 ', largeFlag, ' ', clockwiseFlag, ' ', x2, ',', y2, ' z');
}
this.gauge_.setAttribute('d', path.join(''));
// AnyDuringMigration because: Argument of type 'number' is not assignable
// to parameter of type 'string'.
this.line_!.setAttribute('x2', x2 as AnyDuringMigration);
// AnyDuringMigration because: Argument of type 'number' is not assignable
// to parameter of type 'string'.
this.line_!.setAttribute('y2', y2 as AnyDuringMigration);
this.line_?.setAttribute('x2', `${x2}`);
this.line_?.setAttribute('y2', `${y2}`);
}

/**
Expand Down Expand Up @@ -435,8 +426,7 @@ export class FieldAngle extends FieldInput<number> {
* @param opt_newValue The input value.
* @returns A valid angle, or null if invalid.
*/
protected override doClassValidation_(opt_newValue?: AnyDuringMigration):
number|null {
protected override doClassValidation_(opt_newValue?: any): number|null {
const value = Number(opt_newValue);
if (isNaN(value) || !isFinite(value)) {
return null;
Expand Down
Loading