From af2bf583affb75e84a26d1fb3c0786967716ea71 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 30 Nov 2021 11:11:18 -0800 Subject: [PATCH] refactor: migrate `generators/dart.js` to `goog.module` syntax (#5749) * chore: migrate generators/dart.js to goog.module * chore: migrate generators/dart.js to named requires * chore: Update generators/dart.js to alphabetize imports Co-authored-by: Christopher Allen Co-authored-by: Christopher Allen --- generators/dart.js | 102 +++++++++++++++++++++++---------------------- tests/deps.js | 2 +- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/generators/dart.js b/generators/dart.js index 6d069c168..3be566bb3 100644 --- a/generators/dart.js +++ b/generators/dart.js @@ -6,25 +6,27 @@ /** * @fileoverview Helper functions for generating Dart for blocks. - * @suppress {missingRequire|checkTypes|globalThis} + * @suppress {checkTypes|globalThis} */ 'use strict'; -goog.provide('Blockly.Dart'); +goog.module('Blockly.Dart'); +goog.module.declareLegacyNamespace(); + +const Variables = goog.require('Blockly.Variables'); +const stringUtils = goog.require('Blockly.utils.string'); +const {Block} = goog.requireType('Blockly.Block'); +const {Generator} = goog.require('Blockly.Generator'); +const {Names, NameType} = goog.require('Blockly.Names'); +const {Workspace} = goog.requireType('Blockly.Workspace'); +const {inputTypes} = goog.require('Blockly.inputTypes'); -goog.require('Blockly.Generator'); -goog.require('Blockly.Names'); -goog.require('Blockly.Variables'); -goog.require('Blockly.inputTypes'); -goog.require('Blockly.utils.string'); -goog.requireType('Blockly.Block'); -goog.requireType('Blockly.Workspace'); /** * Dart code generator. - * @type {!Blockly.Generator} + * @type {!Generator} */ -Blockly.Dart = new Blockly.Generator('Dart'); +const Dart = new Generator('Dart'); /** * List of illegal variable names. @@ -33,7 +35,7 @@ Blockly.Dart = new Blockly.Generator('Dart'); * accidentally clobbering a built-in object or function. * @private */ -Blockly.Dart.addReservedWords( +Dart.addReservedWords( // https://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf // Section 16.1.1 'assert,break,case,catch,class,const,continue,default,do,else,enum,' + @@ -56,41 +58,41 @@ Blockly.Dart.addReservedWords( * Order of operation ENUMs. * https://dart.dev/guides/language/language-tour#operators */ -Blockly.Dart.ORDER_ATOMIC = 0; // 0 "" ... -Blockly.Dart.ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] . ?. -Blockly.Dart.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr -Blockly.Dart.ORDER_MULTIPLICATIVE = 3; // * / % ~/ -Blockly.Dart.ORDER_ADDITIVE = 4; // + - -Blockly.Dart.ORDER_SHIFT = 5; // << >> -Blockly.Dart.ORDER_BITWISE_AND = 6; // & -Blockly.Dart.ORDER_BITWISE_XOR = 7; // ^ -Blockly.Dart.ORDER_BITWISE_OR = 8; // | -Blockly.Dart.ORDER_RELATIONAL = 9; // >= > <= < as is is! -Blockly.Dart.ORDER_EQUALITY = 10; // == != -Blockly.Dart.ORDER_LOGICAL_AND = 11; // && -Blockly.Dart.ORDER_LOGICAL_OR = 12; // || -Blockly.Dart.ORDER_IF_NULL = 13; // ?? -Blockly.Dart.ORDER_CONDITIONAL = 14; // expr ? expr : expr -Blockly.Dart.ORDER_CASCADE = 15; // .. -Blockly.Dart.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |= -Blockly.Dart.ORDER_NONE = 99; // (...) +Dart.ORDER_ATOMIC = 0; // 0 "" ... +Dart.ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] . ?. +Dart.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr +Dart.ORDER_MULTIPLICATIVE = 3; // * / % ~/ +Dart.ORDER_ADDITIVE = 4; // + - +Dart.ORDER_SHIFT = 5; // << >> +Dart.ORDER_BITWISE_AND = 6; // & +Dart.ORDER_BITWISE_XOR = 7; // ^ +Dart.ORDER_BITWISE_OR = 8; // | +Dart.ORDER_RELATIONAL = 9; // >= > <= < as is is! +Dart.ORDER_EQUALITY = 10; // == != +Dart.ORDER_LOGICAL_AND = 11; // && +Dart.ORDER_LOGICAL_OR = 12; // || +Dart.ORDER_IF_NULL = 13; // ?? +Dart.ORDER_CONDITIONAL = 14; // expr ? expr : expr +Dart.ORDER_CASCADE = 15; // .. +Dart.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |= +Dart.ORDER_NONE = 99; // (...) /** * Whether the init method has been called. * @type {?boolean} */ -Blockly.Dart.isInitialized = false; +Dart.isInitialized = false; /** * Initialise the database of variable names. - * @param {!Blockly.Workspace} workspace Workspace to generate code from. + * @param {!Workspace} workspace Workspace to generate code from. */ -Blockly.Dart.init = function(workspace) { +Dart.init = function(workspace) { // Call Blockly.Generator's init. Object.getPrototypeOf(this).init.call(this); if (!this.nameDB_) { - this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_); + this.nameDB_ = new Names(this.RESERVED_WORDS_); } else { this.nameDB_.reset(); } @@ -101,17 +103,17 @@ Blockly.Dart.init = function(workspace) { const defvars = []; // Add developer variables (not created or named by the user). - const devVarList = Blockly.Variables.allDeveloperVariables(workspace); + const devVarList = Variables.allDeveloperVariables(workspace); for (let i = 0; i < devVarList.length; i++) { defvars.push(this.nameDB_.getName(devVarList[i], - Blockly.Names.DEVELOPER_VARIABLE_TYPE)); + NameType.DEVELOPER_VARIABLE)); } // Add user variables, but only ones that are being used. - const variables = Blockly.Variables.allUsedVarModels(workspace); + const variables = Variables.allUsedVarModels(workspace); for (let i = 0; i < variables.length; i++) { defvars.push(this.nameDB_.getName(variables[i].getId(), - Blockly.VARIABLE_CATEGORY_NAME)); + NameType.VARIABLE)); } // Declare all of the variables. @@ -127,7 +129,7 @@ Blockly.Dart.init = function(workspace) { * @param {string} code Generated code. * @return {string} Completed code. */ -Blockly.Dart.finish = function(code) { +Dart.finish = function(code) { // Indent every line. if (code) { code = this.prefixLines(code, this.INDENT); @@ -160,7 +162,7 @@ Blockly.Dart.finish = function(code) { * @param {string} line Line of generated code. * @return {string} Legal line of code. */ -Blockly.Dart.scrubNakedValue = function(line) { +Dart.scrubNakedValue = function(line) { return line + ';\n'; }; @@ -170,7 +172,7 @@ Blockly.Dart.scrubNakedValue = function(line) { * @return {string} Dart string. * @protected */ -Blockly.Dart.quote_ = function(string) { +Dart.quote_ = function(string) { // Can't use goog.string.quote since $ must also be escaped. string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') @@ -186,7 +188,7 @@ Blockly.Dart.quote_ = function(string) { * @return {string} Dart string. * @protected */ -Blockly.Dart.multiline_quote_ = function (string) { +Dart.multiline_quote_ = function (string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // + '\n' + @@ -197,20 +199,20 @@ Blockly.Dart.multiline_quote_ = function (string) { * Common tasks for generating Dart from blocks. * Handles comments for the specified block and any connected value blocks. * Calls any statements following this block. - * @param {!Blockly.Block} block The current block. + * @param {!Block} block The current block. * @param {string} code The Dart code created for this block. * @param {boolean=} opt_thisOnly True to generate code for only this statement. * @return {string} Dart code with comments and subsequent blocks added. * @protected */ -Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) { +Dart.scrub_ = function(block, code, opt_thisOnly) { let commentCode = ''; // Only collect comments for blocks that aren't inline. if (!block.outputConnection || !block.outputConnection.targetConnection) { // Collect comment for this block. let comment = block.getCommentText(); if (comment) { - comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3); + comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3); if (block.getProcedureDef) { // Use documentation comment for function comments. commentCode += this.prefixLines(comment + '\n', '/// '); @@ -221,7 +223,7 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) { // Collect comments for all value arguments. // Don't collect comments for nested statements. for (let i = 0; i < block.inputList.length; i++) { - if (block.inputList[i].type === Blockly.inputTypes.VALUE) { + if (block.inputList[i].type === inputTypes.VALUE) { const childBlock = block.inputList[i].connection.targetBlock(); if (childBlock) { comment = this.allNestedComments(childBlock); @@ -239,14 +241,14 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) { /** * Gets a property and adjusts the value while taking into account indexing. - * @param {!Blockly.Block} block The block. + * @param {!Block} block The block. * @param {string} atId The property ID of the element to get. * @param {number=} opt_delta Value to add. * @param {boolean=} opt_negate Whether to negate the value. * @param {number=} opt_order The highest order acting on this value. * @return {string|number} */ -Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate, +Dart.getAdjusted = function(block, atId, opt_delta, opt_negate, opt_order) { let delta = opt_delta || 0; let order = opt_order || this.ORDER_NONE; @@ -271,7 +273,7 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate, /** @type {string|number} */ let at = this.valueToCode(block, atId, outerOrder) || defaultAtIndex; - if (Blockly.utils.string.isNumber(at)) { + if (stringUtils.isNumber(at)) { // If the index is a naked number, adjust it right now. at = parseInt(at, 10) + delta; if (opt_negate) { @@ -299,3 +301,5 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate, } return at; }; + +exports = Dart; diff --git a/tests/deps.js b/tests/deps.js index c1c36549b..5244ef923 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -266,7 +266,7 @@ goog.addDependency('../../core/workspace_dragger.js', ['Blockly.WorkspaceDragger goog.addDependency('../../core/workspace_svg.js', ['Blockly.WorkspaceSvg'], ['Blockly.BlockSvg', 'Blockly.ComponentManager', 'Blockly.ConnectionDB', 'Blockly.ContextMenu', 'Blockly.ContextMenuRegistry', 'Blockly.DropDownDiv', 'Blockly.Events.BlockCreate', 'Blockly.Events.ThemeChange', 'Blockly.Events.ViewportChange', 'Blockly.Events.utils', 'Blockly.Gesture', 'Blockly.Grid', 'Blockly.IASTNodeLocationSvg', 'Blockly.MarkerManager', 'Blockly.MetricsManager', 'Blockly.Msg', 'Blockly.Options', 'Blockly.ThemeManager', 'Blockly.Themes.Classic', 'Blockly.Tooltip', 'Blockly.TouchGesture', 'Blockly.WidgetDiv', 'Blockly.Workspace', 'Blockly.WorkspaceAudio', 'Blockly.Xml', 'Blockly.blockRendering', 'Blockly.browserEvents', 'Blockly.common', 'Blockly.internalConstants', 'Blockly.registry', 'Blockly.serialization.blocks', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.array', 'Blockly.utils.dom', 'Blockly.utils.object', 'Blockly.utils.svgMath', 'Blockly.utils.toolbox', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/xml.js', ['Blockly.Xml'], ['Blockly.Events.utils', 'Blockly.inputTypes', 'Blockly.utils.Size', 'Blockly.utils.dom', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/zoom_controls.js', ['Blockly.ZoomControls'], ['Blockly.ComponentManager', 'Blockly.Css', 'Blockly.Events.Click', 'Blockly.Events.utils', 'Blockly.IPositionable', 'Blockly.Touch', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.uiPosition', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../generators/dart.js', ['Blockly.Dart'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6'}); +goog.addDependency('../../generators/dart.js', ['Blockly.Dart'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/dart/all.js', ['Blockly.Dart.all'], ['Blockly.Dart.colour', 'Blockly.Dart.lists', 'Blockly.Dart.logic', 'Blockly.Dart.loops', 'Blockly.Dart.math', 'Blockly.Dart.procedures', 'Blockly.Dart.texts', 'Blockly.Dart.variables', 'Blockly.Dart.variablesDynamic'], {'module': 'goog'}); goog.addDependency('../../generators/dart/colour.js', ['Blockly.Dart.colour'], ['Blockly.Dart'], {'lang': 'es6'}); goog.addDependency('../../generators/dart/lists.js', ['Blockly.Dart.lists'], ['Blockly.Dart'], {'lang': 'es6'});