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: migrate generators/dart.js to goog.module syntax #5749

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 53 additions & 49 deletions generators/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,' +
Expand All @@ -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();
}
Expand All @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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';
};

Expand All @@ -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')
Expand All @@ -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' +
Expand All @@ -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', '/// ');
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -299,3 +301,5 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
}
return at;
};

exports = Dart;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have done it the preferable way since (as I understand it) mostly importers will be calling methods on the Dart object, but:

We could alternatively use named exports:

Suggested change
exports = Dart;
exports.Dart = Dart;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i went for default export because of how it's used in the block generator files.

2 changes: 1 addition & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.