From 43f54b1b060f7a0627fe2e2f0a10e78f2e1638cb Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 22 Aug 2022 14:13:26 -0700 Subject: [PATCH] chore: Fix or individually disable no-this-alias lint rule violations. (#6371) --- .eslintrc.json | 2 -- core/block.ts | 3 +++ core/block_svg.ts | 10 +++++----- core/blockly.ts | 6 ++---- core/connection.ts | 23 +++++++++++------------ core/flyout_base.ts | 5 ++--- core/rendered_connection.ts | 8 ++++---- core/scrollbar.ts | 6 ++---- core/toolbox/category.ts | 1 + core/variable_map.ts | 7 +++---- core/workspace_comment_svg.ts | 28 ++++++++++++---------------- 11 files changed, 45 insertions(+), 54 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index d6919d2fd..e58ff27e7 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -125,8 +125,6 @@ "@typescript-eslint/no-inferrable-types": ["off"], // Temporarily disable. 33 problems. "@typescript-eslint/no-empty-function": ["off"], - // Temporarily disable. 17 problems. - "@typescript-eslint/no-this-alias": ["off"], // Temporarily disable. 3 problems. "@typescript-eslint/no-empty-interface": ["off"], // Temporarily disable. 34 problems. diff --git a/core/block.ts b/core/block.ts index 9037345ae..dc1c1a22e 100644 --- a/core/block.ts +++ b/core/block.ts @@ -570,6 +570,7 @@ export class Block implements IASTNodeLocation, IDeletable { * @return The block (if any) that surrounds the current block. */ getSurroundParent(): this|null { + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ let block = this; let prevBlock; do { @@ -625,6 +626,7 @@ export class Block implements IASTNodeLocation, IDeletable { */ getRootBlock(): this { let rootBlock: this; + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ let block: this|null = this; do { rootBlock = block; @@ -641,6 +643,7 @@ export class Block implements IASTNodeLocation, IDeletable { * @internal */ getTopStackBlock(): this { + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ let block = this; let previous; do { diff --git a/core/block_svg.ts b/core/block_svg.ts index c5ac9034e..3ea6c792b 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -1242,6 +1242,7 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg, * @internal */ bringToFront() { + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ let block = this; do { const root = block.getSvgRoot(); @@ -1550,19 +1551,18 @@ export class BlockSvg extends Block implements IASTNodeLocationSvg, * @internal */ scheduleSnapAndBump() { - const block = this; // Ensure that any snap and bump are part of this move's event group. const group = eventUtils.getGroup(); - setTimeout(function() { + setTimeout(() => { eventUtils.setGroup(group); - block.snapToGrid(); + this.snapToGrid(); eventUtils.setGroup(false); }, config.bumpDelay / 2); - setTimeout(function() { + setTimeout(() => { eventUtils.setGroup(group); - block.bumpNeighbours(); + this.bumpNeighbours(); eventUtils.setGroup(false); }, config.bumpDelay); } diff --git a/core/blockly.ts b/core/blockly.ts index 08afa5a34..29e85ad36 100644 --- a/core/blockly.ts +++ b/core/blockly.ts @@ -566,13 +566,11 @@ WorkspaceCommentSvg.prototype.showContextMenu = if (this.workspace.options.readOnly) { return; } - // Save the current workspace comment in a variable for use in closures. - const comment = this; const menuOptions = []; if (this.isDeletable() && this.isMovable()) { - menuOptions.push(ContextMenu.commentDuplicateOption(comment)); - menuOptions.push(ContextMenu.commentDeleteOption(comment)); + menuOptions.push(ContextMenu.commentDuplicateOption(this)); + menuOptions.push(ContextMenu.commentDeleteOption(this)); } ContextMenu.show(e, menuOptions, this.RTL); diff --git a/core/connection.ts b/core/connection.ts index 8dadd3439..f1cd3057c 100644 --- a/core/connection.ts +++ b/core/connection.ts @@ -91,8 +91,7 @@ export class Connection implements IASTNodeLocationWithBlock { */ protected connect_(childConnection: Connection) { const INPUT = ConnectionType.INPUT_VALUE; - const parentConnection = this; - const parentBlock = parentConnection.getSourceBlock(); + const parentBlock = this.getSourceBlock(); const childBlock = childConnection.getSourceBlock(); // Make sure the childConnection is available. @@ -102,16 +101,16 @@ export class Connection implements IASTNodeLocationWithBlock { // Make sure the parentConnection is available. let orphan; - if (parentConnection.isConnected()) { - const shadowState = parentConnection.stashShadowState_(); - const target = parentConnection.targetBlock(); + if (this.isConnected()) { + const shadowState = this.stashShadowState_(); + const target = this.targetBlock(); if (target!.isShadow()) { target!.dispose(false); } else { - parentConnection.disconnect(); + this.disconnect(); orphan = target; } - parentConnection.applyShadowState_(shadowState); + this.applyShadowState_(shadowState); } // Connect the new connection to the parent. @@ -120,7 +119,7 @@ export class Connection implements IASTNodeLocationWithBlock { event = new (eventUtils.get(eventUtils.BLOCK_MOVE))!(childBlock) as BlockMove; } - connectReciprocally(parentConnection, childConnection); + connectReciprocally(this, childConnection); childBlock.setParent(parentBlock); if (event) { event.recordNew(); @@ -129,15 +128,14 @@ export class Connection implements IASTNodeLocationWithBlock { // Deal with the orphan if it exists. if (orphan) { - const orphanConnection = parentConnection.type === INPUT ? - orphan.outputConnection : - orphan.previousConnection; + const orphanConnection = this.type === INPUT ? orphan.outputConnection : + orphan.previousConnection; const connection = Connection.getConnectionForOrphanedConnection( childBlock, (orphanConnection)); if (connection) { orphanConnection.connect(connection); } else { - orphanConnection.onFailedConnect(parentConnection); + orphanConnection.onFailedConnect(this); } } } @@ -256,6 +254,7 @@ export class Connection implements IASTNodeLocationWithBlock { // Superior block. parentBlock = this.sourceBlock_; childBlock = otherConnection.getSourceBlock(); + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ parentConnection = this; } else { // Inferior block. diff --git a/core/flyout_base.ts b/core/flyout_base.ts index 8aebc0855..5b39fb973 100644 --- a/core/flyout_base.ts +++ b/core/flyout_base.ts @@ -897,12 +897,11 @@ export abstract class Flyout extends DeleteArea implements IFlyout { * @return Function to call when block is clicked. */ private blockMouseDown_(block: BlockSvg): Function { - const flyout = this; return (e: MouseEvent) => { - const gesture = flyout.targetWorkspace.getGesture(e); + const gesture = this.targetWorkspace.getGesture(e); if (gesture) { gesture.setStartBlock(block); - gesture.handleFlyoutStart(e, flyout); + gesture.handleFlyoutStart(e, this); } }; } diff --git a/core/rendered_connection.ts b/core/rendered_connection.ts index 5b92e5ee4..c9c65e84d 100644 --- a/core/rendered_connection.ts +++ b/core/rendered_connection.ts @@ -153,6 +153,7 @@ export class RenderedConnection extends Connection { return; } // Swap the connections and move the 'static' connection instead. + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ staticConnection = this; reverse = true; } @@ -495,8 +496,7 @@ export class RenderedConnection extends Connection { const renderedChildConnection = childConnection as RenderedConnection; - const parentConnection = this; - const parentBlock = parentConnection.getSourceBlock(); + const parentBlock = this.getSourceBlock(); const childBlock = renderedChildConnection.getSourceBlock(); const parentRendered = parentBlock.rendered; const childRendered = childBlock.rendered; @@ -508,8 +508,8 @@ export class RenderedConnection extends Connection { childBlock.updateDisabled(); } if (parentRendered && childRendered) { - if (parentConnection.type === ConnectionType.NEXT_STATEMENT || - parentConnection.type === ConnectionType.PREVIOUS_STATEMENT) { + if (this.type === ConnectionType.NEXT_STATEMENT || + this.type === ConnectionType.PREVIOUS_STATEMENT) { // Child block may need to square off its corners if it is in a stack. // Rendering a child will render its parent. childBlock.render(); diff --git a/core/scrollbar.ts b/core/scrollbar.ts index 39370f4ac..4f98bed4f 100644 --- a/core/scrollbar.ts +++ b/core/scrollbar.ts @@ -164,12 +164,10 @@ export class Scrollbar { this.lengthAttribute_ = 'height'; this.positionAttribute_ = 'y'; } - const scrollbar = this; this.onMouseDownBarWrapper_ = browserEvents.conditionalBind( - this.svgBackground_!, 'mousedown', scrollbar, - scrollbar.onMouseDownBar_); + this.svgBackground_!, 'mousedown', this, this.onMouseDownBar_); this.onMouseDownHandleWrapper_ = browserEvents.conditionalBind( - this.svgHandle_!, 'mousedown', scrollbar, scrollbar.onMouseDownHandle_); + this.svgHandle_!, 'mousedown', this, this.onMouseDownHandle_); } /** diff --git a/core/toolbox/category.ts b/core/toolbox/category.ts index 6c2818ea6..30dd1ea70 100644 --- a/core/toolbox/category.ts +++ b/core/toolbox/category.ts @@ -481,6 +481,7 @@ export class ToolboxCategory extends ToolboxItem implements * @return True only if every ancestor is expanded */ protected allAncestorsExpanded_(): boolean { + /* eslint-disable-next-line @typescript-eslint/no-this-alias */ let category: IToolboxItem = this; while (category.getParent()) { category = category.getParent()!; diff --git a/core/variable_map.ts b/core/variable_map.ts index d72163b76..1166ebcc9 100644 --- a/core/variable_map.ts +++ b/core/variable_map.ts @@ -227,20 +227,19 @@ export class VariableMap { } } - const map = this; if (uses.length > 1) { // Confirm before deleting multiple blocks. const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION'] .replace('%1', String(uses.length)) .replace('%2', variableName); - dialog.confirm(confirmText, function(ok) { + dialog.confirm(confirmText, (ok) => { if (ok && variable) { - map.deleteVariableInternal(variable, uses); + this.deleteVariableInternal(variable, uses); } }); } else { // No confirmation necessary for a single block. - map.deleteVariableInternal(variable, uses); + this.deleteVariableInternal(variable, uses); } } else { console.warn('Can\'t delete non-existent variable: ' + id); diff --git a/core/workspace_comment_svg.ts b/core/workspace_comment_svg.ts index 528dc88fe..098d461ab 100644 --- a/core/workspace_comment_svg.ts +++ b/core/workspace_comment_svg.ts @@ -978,20 +978,18 @@ export class WorkspaceCommentSvg extends WorkspaceComment implements * @internal */ setFocus() { - const comment = this; this.focused_ = true; // Defer CSS changes. - setTimeout(function() { - if (comment.disposed_) { + setTimeout(() => { + if (this.disposed_) { return; } - comment.textarea_!.focus(); - comment.addFocus(); + this.textarea_!.focus(); + this.addFocus(); dom.addClass( - comment.svgRectTarget_ as SVGRectElement, - 'blocklyCommentTargetFocused'); + this.svgRectTarget_ as SVGRectElement, 'blocklyCommentTargetFocused'); dom.addClass( - comment.svgHandleTarget_ as SVGRectElement, + this.svgHandleTarget_ as SVGRectElement, 'blocklyCommentHandleTargetFocused'); }, 0); } @@ -1001,21 +999,19 @@ export class WorkspaceCommentSvg extends WorkspaceComment implements * @internal */ blurFocus() { - const comment = this; this.focused_ = false; // Defer CSS changes. - setTimeout(function() { - if (comment.disposed_) { + setTimeout(() => { + if (this.disposed_) { return; } - comment.textarea_!.blur(); - comment.removeFocus(); + this.textarea_!.blur(); + this.removeFocus(); dom.removeClass( - comment.svgRectTarget_ as SVGRectElement, - 'blocklyCommentTargetFocused'); + this.svgRectTarget_ as SVGRectElement, 'blocklyCommentTargetFocused'); dom.removeClass( - comment.svgHandleTarget_ as SVGRectElement, + this.svgHandleTarget_ as SVGRectElement, 'blocklyCommentHandleTargetFocused'); }, 0); }