From 809148b435ca957a9b44c8de3606771d518f761a Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 16 Jul 2020 11:36:48 -0600 Subject: [PATCH] Fix compiler errors --- blockly_uncompressed.js | 1 + core/connection.js | 10 +-- core/connection_checker.js | 6 +- core/connection_db.js | 12 +++- core/interfaces/i_connection_checker.js | 87 +++++++++++++++++++++++++ core/keyboard_nav/navigation.js | 2 +- core/rendered_connection.js | 2 - core/workspace.js | 3 +- 8 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 core/interfaces/i_connection_checker.js diff --git a/blockly_uncompressed.js b/blockly_uncompressed.js index 5c69d5ac9..f8a07fd95 100644 --- a/blockly_uncompressed.js +++ b/blockly_uncompressed.js @@ -75,6 +75,7 @@ goog.addDependency('../../core/input.js', ['Blockly.Input'], ['Blockly.Connectio goog.addDependency('../../core/insertion_marker_manager.js', ['Blockly.InsertionMarkerManager'], ['Blockly.Events', 'Blockly.blockAnimations'], {'lang': 'es5'}); goog.addDependency('../../core/interfaces/i_accessibility.js', ['Blockly.IASTNodeLocation', 'Blockly.IASTNodeLocationSvg', 'Blockly.IASTNodeLocationWithBlock', 'Blockly.IBlocklyActionable'], [], {}); goog.addDependency('../../core/interfaces/i_bounded_element.js', ['Blockly.IBoundedElement'], [], {}); +goog.addDependency('../../core/interfaces/i_connection_checker.js', ['Blockly.IConnectionChecker'], [], {}); goog.addDependency('../../core/interfaces/i_copyable.js', ['Blockly.ICopyable'], [], {}); goog.addDependency('../../core/interfaces/i_deletable.js', ['Blockly.IDeletable'], [], {}); goog.addDependency('../../core/interfaces/i_deletearea.js', ['Blockly.IDeleteArea'], [], {}); diff --git a/core/connection.js b/core/connection.js index b15b437ff..dfbc0ac3a 100644 --- a/core/connection.js +++ b/core/connection.js @@ -16,7 +16,7 @@ goog.require('Blockly.Events'); goog.require('Blockly.Events.BlockMove'); goog.require('Blockly.Xml'); -goog.requireType('Blockly.ConnectionChecker'); +goog.requireType('Blockly.IConnectionChecker'); goog.requireType('Blockly.IASTNodeLocationWithBlock'); @@ -253,7 +253,7 @@ Blockly.Connection.prototype.isConnected = function() { Blockly.Connection.prototype.canConnectWithReason = function(target) { // TODO: deprecation warning with date, plus tests. return this.getConnectionChecker().canConnectWithReason( - this, target); + this, target, false); }; /** @@ -268,15 +268,15 @@ Blockly.Connection.prototype.checkConnection = function(target) { // TODO: Add deprecation warning notices *and* add tests to make sure these // still work (for any blocks that use them). var checker = this.getConnectionChecker(); - var reason = !checker.canConnectWithReason(this, target, false); + var reason = checker.canConnectWithReason(this, target, false); if (reason != Blockly.Connection.CAN_CONNECT) { - throw new Error(checker.getErrorMessage(this, target, reason)); + throw new Error(checker.getErrorMessage(reason, this, target)); } }; /** * Get the workspace's connection type checker object. - * @return {!Blockly.ConnectionChecker} The connection type checker for the + * @return {!Blockly.IConnectionChecker} The connection type checker for the * source block's workspace. * @package */ diff --git a/core/connection_checker.js b/core/connection_checker.js index a5fa84c65..cb04e1e40 100644 --- a/core/connection_checker.js +++ b/core/connection_checker.js @@ -13,10 +13,12 @@ goog.provide('Blockly.ConnectionChecker'); +goog.requireType('Blockly.IConnectionChecker'); goog.requireType('Blockly.Connection'); /** * Class for connection type checking logic. + * @implements {Blockly.IConnectionChecker} * @constructor */ Blockly.ConnectionChecker = function() { @@ -62,8 +64,8 @@ Blockly.ConnectionChecker.prototype.canConnectWithReason = function( return Blockly.Connection.REASON_CHECKS_FAILED; } - if (isDragging && this.doDragChecks(connOne, connTwo, false)) { - return Blockly.REASON_DRAG_CHECKS_FAILED; + if (isDragging && this.doDragChecks(connOne, connTwo)) { + return Blockly.Connection.REASON_DRAG_CHECKS_FAILED; } return Blockly.Connection.CAN_CONNECT; diff --git a/core/connection_db.js b/core/connection_db.js index 4dca293bb..2026678d2 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -16,14 +16,14 @@ goog.provide('Blockly.ConnectionDB'); goog.require('Blockly.RenderedConnection'); -goog.requireType('Blockly.ConnectionChecker'); +goog.requireType('Blockly.IConnectionChecker'); /** * Database of connections. * Connections are stored in order of their vertical component. This way * connections in an area may be looked up quickly using a binary search. - * @param {!Blockly.ConnectionChecker} checker The workspace's + * @param {!Blockly.IConnectionChecker} checker The workspace's * connection type checker, used to decide if connections are valid during a * drag. * @constructor @@ -35,6 +35,12 @@ Blockly.ConnectionDB = function(checker) { * @private */ this.connections_ = []; + /** + * The workspace's connection type checker, used to decide if connections are + * valid during a drag. + * @type {!Blockly.IConnectionChecker} + * @private + */ this.connectionChecker_ = checker; }; @@ -279,7 +285,7 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, /** * Initialize a set of connection DBs for a workspace. - * @param {!Blockly.ConnectionChecker} checker The workspace's + * @param {!Blockly.IConnectionChecker} checker The workspace's * connection checker, used to decide if connections are valid during a drag. * @return {!Array.} Array of databases. */ diff --git a/core/interfaces/i_connection_checker.js b/core/interfaces/i_connection_checker.js new file mode 100644 index 000000000..f2987f5aa --- /dev/null +++ b/core/interfaces/i_connection_checker.js @@ -0,0 +1,87 @@ +/** + * @license + * Copyright 2020 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @fileoverview The interface for an object that encapsulates logic for + * checking whether a potential connection is safe and valid. + * @author fenichel@google.com (Rachel Fenichel) + */ +'use strict'; + +goog.provide('Blockly.IConnectionChecker'); + +goog.requireType('Blockly.Connection'); + +/** + * Class for connection type checking logic. + * @interface + */ +Blockly.IConnectionChecker = function() {}; + +/** + * Check whether the current connection can connect with the target + * connection. + * @param {Blockly.Connection} one Connection to check compatibility with. + * @param {Blockly.Connection} two Connection to check compatibility with. + * @param {boolean} isDragging True if the connection is being made by dragging + * a block. + * @return {boolean} Whether the connection is legal. + * @public + */ +Blockly.IConnectionChecker.prototype.canConnect; + +/** + * Checks whether the current connection can connect with the target + * connection, and return an error code if there are problems. + * @param {Blockly.Connection} one Connection to check compatibility with. + * @param {Blockly.Connection} two Connection to check compatibility with. + * @param {boolean} isDragging [description] + * @return {number} Blockly.Connection.CAN_CONNECT if the connection is legal, + * an error code otherwise. + * @public + */ +Blockly.IConnectionChecker.prototype.canConnectWithReason; + +/** + * Helper method that translates a connection error code into a string. + * @param {number} errorCode The error code. + * @param {Blockly.Connection} one One of the two connections being checked. + * @param {Blockly.Connection} two The second of the two connections being + * checked. + * @return {string} A developer-readable error string. + * @public + */ +Blockly.IConnectionChecker.prototype.getErrorMessage; + +/** + * Check that connecting the given connections is safe, meaning that it would + * not break any of Blockly's basic assumptions (e.g. no self connections). + * @param {Blockly.Connection} one The first of the connections to check. + * @param {Blockly.Connection} two The second of the connections to check. + * @return {number} An enum with the reason this connection is safe or unsafe. + * @public + */ +Blockly.IConnectionChecker.prototype.doSafetyChecks; + +/** + * Check whether this connection is compatible with another connection with + * respect to the value type system. E.g. square_root("Hello") is not + * compatible. + * @param {!Blockly.Connection} one Connection to compare. + * @param {!Blockly.Connection} two Connection to compare against. + * @return {boolean} True if the connections share a type. + * @public + */ +Blockly.IConnectionChecker.prototype.doTypeChecks; + +/** + * Check whether this connection can be made by dragging. + * @param {!Blockly.Connection} one Connection to compare. + * @param {!Blockly.Connection} two Connection to compare against. + * @return {boolean} True if the connections share a type. + * @public + */ +Blockly.IConnectionChecker.prototype.doDragChecks; diff --git a/core/keyboard_nav/navigation.js b/core/keyboard_nav/navigation.js index fcf9fdfa8..c162e282e 100644 --- a/core/keyboard_nav/navigation.js +++ b/core/keyboard_nav/navigation.js @@ -504,7 +504,7 @@ Blockly.navigation.connect_ = function(movingConnection, destConnection) { var reason = checker.canConnectWithReason( movingConnection, destConnection, false); Blockly.navigation.warn_('Connection failed with error: ' + - checker.getErrorMessage(movingConnection, destConnection, reason)); + checker.getErrorMessage(reason, movingConnection, destConnection)); return false; } }; diff --git a/core/rendered_connection.js b/core/rendered_connection.js index 0046446b3..5ceb347c4 100644 --- a/core/rendered_connection.js +++ b/core/rendered_connection.js @@ -19,8 +19,6 @@ goog.require('Blockly.utils.Coordinate'); goog.require('Blockly.utils.dom'); goog.require('Blockly.utils.object'); -goog.requireType('Blockly.ConnectionChecker'); - /** * Class for a connection between blocks that may be rendered on screen. diff --git a/core/workspace.js b/core/workspace.js index b79ab340c..3f3d0ceff 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -20,6 +20,7 @@ goog.require('Blockly.utils.math'); goog.require('Blockly.VariableMap'); goog.requireType('Blockly.IASTNodeLocation'); +goog.requireType('Blockly.IConnectionChecker'); /** @@ -45,7 +46,7 @@ Blockly.Workspace = function(opt_options) { /** * An object that encapsulates logic for safety, type, and dragging checks. - * @type {Blockly} + * @type {!Blockly.IConnectionChecker} */ this.connectionChecker = new Blockly.ConnectionChecker();