From dbf10cf167d65a005fbb9f966dff740a8ffcd58d Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Fri, 17 Sep 2021 19:30:21 +0100 Subject: [PATCH] Clean up get/set accessor definitions (#5488) * Use Object.defineProperties instead of .defineProperty when installing get and set accessors for previously-mutable exports. * Add entries to renamings.js where feasible (i.e., for static rather than instance properties). (Don't do blockly.js yet, to avoid merge conflicts.) --- core/contextmenu.js | 19 ++++++++++++--- core/generator.js | 42 ++++++++++++++++++---------------- scripts/migration/renamings.js | 21 +++++++++++++++++ tests/deps.js | 2 +- tests/deps.mocha.js | 2 +- 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/core/contextmenu.js b/core/contextmenu.js index cc91ebcb1..d2b55c162 100644 --- a/core/contextmenu.js +++ b/core/contextmenu.js @@ -29,6 +29,7 @@ const WidgetDiv = goog.require('Blockly.WidgetDiv'); const Xml = goog.require('Blockly.Xml'); const aria = goog.require('Blockly.utils.aria'); const browserEvents = goog.require('Blockly.browserEvents'); +const deprecation = goog.require('Blockly.utils.deprecation'); const dom = goog.require('Blockly.utils.dom'); const internalConstants = goog.require('Blockly.internalConstants'); const userAgent = goog.require('Blockly.utils.userAgent'); @@ -64,9 +65,21 @@ const setCurrentBlock = function(block) { exports.setCurrentBlock = setCurrentBlock; // Ad JS accessors for backwards compatibility. -Object.defineProperty(exports, 'currentBlock', { - get: getCurrentBlock, - set: setCurrentBlock, +Object.defineProperties(exports, { + currentBlock: { + get: function() { + deprecation.warn( + 'Blockly.ContextMenu.currentBlock', 'September 2021', 'September 2022', + 'Blockly.Tooltip.getCurrentBlock()'); + return getCurrentBlock(); + }, + set: function(block) { + deprecation.warn( + 'Blockly.ContextMenu.currentBlock', 'September 2021', 'September 2022', + 'Blockly.Tooltip.setCurrentBlock(block)'); + setCurrentBlock(block); + }, + }, }); /** diff --git a/core/generator.js b/core/generator.js index 9984f186f..7ebb000d2 100644 --- a/core/generator.js +++ b/core/generator.js @@ -411,27 +411,29 @@ Generator.prototype.functionNames_; */ Generator.prototype.nameDB_; -Object.defineProperty(Generator.prototype, 'variableDB_', { - /** - * Getter. - * @deprecated 'variableDB_' was renamed to 'nameDB_' (May 2021). - * @this {Generator} - * @return {!Names|undefined} Name database. - */ - get: function() { - deprecation.warn('variableDB_', 'May 2021', 'May 2026', 'nameDB_'); - return this.nameDB_; +Object.defineProperties(Generator.prototype, { + variableDB_: { + /** + * Getter. + * @deprecated 'variableDB_' was renamed to 'nameDB_' (May 2021). + * @this {Generator} + * @return {!Names|undefined} Name database. + */ + get: function() { + deprecation.warn('variableDB_', 'May 2021', 'May 2026', 'nameDB_'); + return this.nameDB_; + }, + /** + * Setter. + * @deprecated 'variableDB_' was renamed to 'nameDB_' (May 2021). + * @this {Generator} + * @param {!Names|undefined} nameDb New name database. + */ + set: function(nameDb) { + deprecation.warn('variableDB_', 'May 2021', 'May 2026', 'nameDB_'); + this.nameDB_ = nameDb; + }, }, - /** - * Setter. - * @deprecated 'variableDB_' was renamed to 'nameDB_' (May 2021). - * @this {Generator} - * @param {!Names|undefined} nameDb New name database. - */ - set: function(nameDb) { - deprecation.warn('variableDB_', 'May 2021', 'May 2026', 'nameDB_'); - this.nameDB_ = nameDb; - } }); /** diff --git a/scripts/migration/renamings.js b/scripts/migration/renamings.js index 311ec631d..5de3a85b2 100644 --- a/scripts/migration/renamings.js +++ b/scripts/migration/renamings.js @@ -86,6 +86,27 @@ const renamings = { // Blockly.common.getMainWorkspace().hideChaff(). See PR #5460. }, }, + 'Blockly.ContextMenu': { + exports: { + currentBlock: {get: 'getCurrentBlock', set: 'setCurrentBlock'}, + }, + }, + 'Blockly.Events': { + exports: { + recordUndo: {get: 'getRecordUndo', set: 'setRecordUndo'}, + }, + }, + 'Blockly.Tooltip': { + exports: { + DIV: {get: 'getDiv', set: 'setDiv'}, + visible: {get: 'isVisible'}, + }, + }, + 'Blockly.WidgetDiv': { + exports: { + DIV: {get: 'getDiv'}, + }, + }, 'Blockly.utils': { exports: { genUid: {module: 'Blockly.utils.idGenerator'}, diff --git a/tests/deps.js b/tests/deps.js index c8d5d7073..50046a84a 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -28,7 +28,7 @@ goog.addDependency('../../core/connection_checker.js', ['Blockly.ConnectionCheck goog.addDependency('../../core/connection_db.js', ['Blockly.ConnectionDB'], ['Blockly.connectionTypes', 'Blockly.constants'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/connection_types.js', ['Blockly.connectionTypes'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/constants.js', ['Blockly.constants'], [], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.deprecation', 'Blockly.utils.dom', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/contextmenu_items.js', ['Blockly.ContextMenuItems'], ['Blockly.ContextMenuRegistry', 'Blockly.Events', 'Blockly.Msg', 'Blockly.clipboard', 'Blockly.dialog', 'Blockly.inputTypes', 'Blockly.utils.idGenerator', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/contextmenu_registry.js', ['Blockly.ContextMenuRegistry'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/css.js', ['Blockly.Css'], [], {'lang': 'es6', 'module': 'goog'}); diff --git a/tests/deps.mocha.js b/tests/deps.mocha.js index 19133a5e9..85722ea11 100644 --- a/tests/deps.mocha.js +++ b/tests/deps.mocha.js @@ -28,7 +28,7 @@ goog.addDependency('../../core/connection_checker.js', ['Blockly.ConnectionCheck goog.addDependency('../../core/connection_db.js', ['Blockly.ConnectionDB'], ['Blockly.connectionTypes', 'Blockly.constants'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/connection_types.js', ['Blockly.connectionTypes'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/constants.js', ['Blockly.constants'], [], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.deprecation', 'Blockly.utils.dom', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/contextmenu_items.js', ['Blockly.ContextMenuItems'], ['Blockly.ContextMenuRegistry', 'Blockly.Events', 'Blockly.Msg', 'Blockly.clipboard', 'Blockly.dialog', 'Blockly.inputTypes', 'Blockly.utils.idGenerator', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/contextmenu_registry.js', ['Blockly.ContextMenuRegistry'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/css.js', ['Blockly.Css'], [], {'lang': 'es6', 'module': 'goog'});