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

Migrate core/extensions.js to goog.module syntax #5065

Merged
merged 5 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
81 changes: 42 additions & 39 deletions core/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* @name Blockly.Extensions
* @namespace
*/
goog.provide('Blockly.Extensions');
goog.module('Blockly.Extensions');
moniika marked this conversation as resolved.
Show resolved Hide resolved
goog.module.declareLegacyNamespace();

goog.require('Blockly.utils');

Expand All @@ -28,7 +29,7 @@ goog.requireType('Blockly.Block');
* The set of all registered extensions, keyed by extension name/id.
* @private
*/
Blockly.Extensions.ALL_ = Object.create(null);
const ALL = Object.create(null);
moniika marked this conversation as resolved.
Show resolved Hide resolved

/**
* Registers a new extension function. Extensions are functions that help
Expand All @@ -40,17 +41,17 @@ Blockly.Extensions.ALL_ = Object.create(null);
* @throws {Error} if the extension name is empty, the extension is already
* registered, or extensionFn is not a function.
*/
Blockly.Extensions.register = function(name, initFn) {
const register = function(name, initFn) {
if ((typeof name != 'string') || (name.trim() == '')) {
throw Error('Error: Invalid extension name "' + name + '"');
}
if (Blockly.Extensions.ALL_[name]) {
if (ALL[name]) {
throw Error('Error: Extension "' + name + '" is already registered.');
}
if (typeof initFn != 'function') {
throw Error('Error: Extension "' + name + '" must be a function');
}
Blockly.Extensions.ALL_[name] = initFn;
ALL[name] = initFn;
};

/**
Expand All @@ -60,11 +61,11 @@ Blockly.Extensions.register = function(name, initFn) {
* @throws {Error} if the extension name is empty or the extension is already
* registered.
*/
Blockly.Extensions.registerMixin = function(name, mixinObj) {
const registerMixin = function(name, mixinObj) {
if (!mixinObj || typeof mixinObj != 'object') {
throw Error('Error: Mixin "' + name + '" must be a object');
}
Blockly.Extensions.register(name, function() {
register(name, function() {
this.mixin(mixinObj);
});
};
Expand All @@ -82,25 +83,25 @@ Blockly.Extensions.registerMixin = function(name, mixinObj) {
* flyout of the mutator dialog.
* @throws {Error} if the mutation is invalid or can't be applied to the block.
*/
Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn,
const registerMutator = function(name, mixinObj, opt_helperFn,
opt_blockList) {
const errorPrefix = 'Error when registering mutator "' + name + '": ';

// Sanity check the mixin object before registering it.
Blockly.Extensions.checkHasFunction_(
checkHasFunction(
errorPrefix, mixinObj.domToMutation, 'domToMutation');
Blockly.Extensions.checkHasFunction_(
checkHasFunction(
errorPrefix, mixinObj.mutationToDom, 'mutationToDom');

const hasMutatorDialog =
Blockly.Extensions.checkMutatorDialog_(mixinObj, errorPrefix);
checkMutatorDialog(mixinObj, errorPrefix);

if (opt_helperFn && (typeof opt_helperFn != 'function')) {
throw Error('Extension "' + name + '" is not a function');
}

// Sanity checks passed.
Blockly.Extensions.register(name, function() {
register(name, function() {
if (hasMutatorDialog) {
if (!Blockly.Mutator) {
throw Error(errorPrefix + 'Missing require for Blockly.Mutator');
Expand All @@ -120,9 +121,9 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn,
* Unregisters the extension registered with the given name.
* @param {string} name The name of the extension to unregister.
*/
Blockly.Extensions.unregister = function(name) {
if (Blockly.Extensions.ALL_[name]) {
delete Blockly.Extensions.ALL_[name];
const unregister = function(name) {
if (ALL[name]) {
delete ALL[name];
} else {
console.warn('No extension mapping for name "' + name +
'" found to unregister');
Expand All @@ -137,27 +138,27 @@ Blockly.Extensions.unregister = function(name) {
* @param {boolean} isMutator True if this extension defines a mutator.
* @throws {Error} if the extension is not found.
*/
Blockly.Extensions.apply = function(name, block, isMutator) {
const extensionFn = Blockly.Extensions.ALL_[name];
const apply = function(name, block, isMutator) {
const extensionFn = ALL[name];
if (typeof extensionFn != 'function') {
throw Error('Error: Extension "' + name + '" not found.');
}
let mutatorProperties;
if (isMutator) {
// Fail early if the block already has mutation properties.
Blockly.Extensions.checkNoMutatorProperties_(name, block);
checkNoMutatorProperties(name, block);
} else {
// Record the old properties so we can make sure they don't change after
// applying the extension.
mutatorProperties = Blockly.Extensions.getMutatorProperties_(block);
mutatorProperties = getMutatorProperties(block);
}
extensionFn.apply(block);

if (isMutator) {
const errorPrefix = 'Error after applying mutator "' + name + '": ';
Blockly.Extensions.checkBlockHasMutatorProperties_(errorPrefix, block);
checkBlockHasMutatorProperties(errorPrefix, block);
} else {
if (!Blockly.Extensions.mutatorPropertiesMatch_(
if (!mutatorPropertiesMatch(
/** @type {!Array<Object>} */ (mutatorProperties), block)) {
throw Error('Error when applying extension "' + name + '": ' +
'mutation properties changed when applying a non-mutator extension.');
Expand All @@ -173,7 +174,7 @@ Blockly.Extensions.apply = function(name, block, isMutator) {
* @throws {Error} if the property does not exist or is not a function.
* @private
*/
Blockly.Extensions.checkHasFunction_ = function(errorPrefix, func,
const checkHasFunction = function(errorPrefix, func,
propertyName) {
if (!func) {
throw Error(errorPrefix +
Expand All @@ -194,8 +195,8 @@ Blockly.Extensions.checkHasFunction_ = function(errorPrefix, func,
* @throws {Error} if any of the properties already exist on the block.
* @private
*/
Blockly.Extensions.checkNoMutatorProperties_ = function(mutationName, block) {
const properties = Blockly.Extensions.getMutatorProperties_(block);
const checkNoMutatorProperties = function(mutationName, block) {
const properties = getMutatorProperties(block);
if (properties.length) {
throw Error('Error: tried to apply mutation "' + mutationName +
'" to a block that already has mutator functions.' +
Expand All @@ -215,7 +216,7 @@ Blockly.Extensions.checkNoMutatorProperties_ = function(mutationName, block) {
* @throws {Error} if the object has only one of the functions.
* @private
*/
Blockly.Extensions.checkMutatorDialog_ = function(object, errorPrefix) {
const checkMutatorDialog = function(object, errorPrefix) {
const hasCompose = object.compose !== undefined;
const hasDecompose = object.decompose !== undefined;

Expand All @@ -240,7 +241,7 @@ Blockly.Extensions.checkMutatorDialog_ = function(object, errorPrefix) {
* @param {!Blockly.Block} block The block to inspect.
* @private
*/
Blockly.Extensions.checkBlockHasMutatorProperties_ = function(errorPrefix,
const checkBlockHasMutatorProperties = function(errorPrefix,
block) {
if (typeof block.domToMutation != 'function') {
throw Error(errorPrefix + 'Applying a mutator didn\'t add "domToMutation"');
Expand All @@ -251,7 +252,7 @@ Blockly.Extensions.checkBlockHasMutatorProperties_ = function(errorPrefix,

// A block with a mutator isn't required to have a mutation dialog, but
// it should still have both or neither of compose and decompose.
Blockly.Extensions.checkMutatorDialog_(block, errorPrefix);
checkMutatorDialog(block, errorPrefix);
};

/**
Expand All @@ -261,7 +262,7 @@ Blockly.Extensions.checkBlockHasMutatorProperties_ = function(errorPrefix,
* should be functions, but may be anything other than undefined.
* @private
*/
Blockly.Extensions.getMutatorProperties_ = function(block) {
const getMutatorProperties = function(block) {
const result = [];
// List each function explicitly by reference to allow for renaming
// during compilation.
Expand Down Expand Up @@ -289,8 +290,8 @@ Blockly.Extensions.getMutatorProperties_ = function(block) {
* @return {boolean} True if the property lists match.
* @private
*/
Blockly.Extensions.mutatorPropertiesMatch_ = function(oldProperties, block) {
const newProperties = Blockly.Extensions.getMutatorProperties_(block);
const mutatorPropertiesMatch = function(oldProperties, block) {
const newProperties = getMutatorProperties(block);
if (newProperties.length != oldProperties.length) {
return false;
}
Expand Down Expand Up @@ -321,7 +322,7 @@ Blockly.Extensions.mutatorPropertiesMatch_ = function(oldProperties, block) {
* tooltip text.
* @return {!Function} The extension function.
*/
Blockly.Extensions.buildTooltipForDropdown = function(dropdownName,
const buildTooltipForDropdown = function(dropdownName,
lookupTable) {
// List of block types already validated, to minimize duplicate warnings.
const blockTypesChecked = [];
Expand All @@ -345,7 +346,7 @@ Blockly.Extensions.buildTooltipForDropdown = function(dropdownName,
*/
const extensionFn = function () {
if (this.type && blockTypesChecked.indexOf(this.type) == -1) {
Blockly.Extensions.checkDropdownOptionsInTable_(
checkDropdownOptionsInTable(
this, dropdownName, lookupTable);
blockTypesChecked.push(this.type);
}
Expand Down Expand Up @@ -380,7 +381,7 @@ Blockly.Extensions.buildTooltipForDropdown = function(dropdownName,
* @param {!Object<string, string>} lookupTable The string lookup table
* @private
*/
Blockly.Extensions.checkDropdownOptionsInTable_ = function(block, dropdownName,
const checkDropdownOptionsInTable = function(block, dropdownName,
lookupTable) {
// Validate all dropdown options have values.
const dropdown = block.getField(dropdownName);
Expand All @@ -405,7 +406,7 @@ Blockly.Extensions.checkDropdownOptionsInTable_ = function(block, dropdownName,
* @param {string} fieldName The field with the replacement text.
* @return {!Function} The extension function.
*/
Blockly.Extensions.buildTooltipWithFieldText = function(msgTemplate,
const buildTooltipWithFieldText = function(msgTemplate,
fieldName) {
// Check the tooltip string messages for invalid references.
// Wait for load, in case Blockly.Msg is not yet populated.
Expand Down Expand Up @@ -440,13 +441,15 @@ Blockly.Extensions.buildTooltipWithFieldText = function(msgTemplate,
* @this {Blockly.Block}
* @private
*/
Blockly.Extensions.extensionParentTooltip_ = function() {
this.tooltipWhenNotConnected_ = this.tooltip;
const extensionParentTooltip = function() {
this.tooltipWhenNotConnected = this.tooltip;
this.setTooltip(function() {
const parent = this.getParent();
return (parent && parent.getInputsInline() && parent.tooltip) ||
this.tooltipWhenNotConnected_;
this.tooltipWhenNotConnected;
}.bind(this));
};
Blockly.Extensions.register('parent_tooltip_when_inline',
Blockly.Extensions.extensionParentTooltip_);
register('parent_tooltip_when_inline',
extensionParentTooltip);

exports = {ALL_: ALL, register, registerMixin, registerMutator, unregister, apply, buildTooltipForDropdown, buildTooltipWithFieldText};
moniika marked this conversation as resolved.
Show resolved Hide resolved
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.