Remove all goog.asserts and goog.isXxx

First step to reducing Closure’s footprint.
This commit is contained in:
Neil Fraser
2018-06-10 00:15:22 -07:00
committed by Neil Fraser
parent 02dc7b1d1d
commit 3909bd420a
35 changed files with 251 additions and 205 deletions

View File

@@ -40,7 +40,6 @@ goog.require('Blockly.Warning');
goog.require('Blockly.Workspace');
goog.require('Blockly.Xml');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
goog.require('goog.string');
@@ -57,11 +56,11 @@ goog.require('goog.string');
*/
Blockly.Block = function(workspace, prototypeName, opt_id) {
if (typeof Blockly.Generator.prototype[prototypeName] !== 'undefined') {
console.warn('FUTURE ERROR: Block prototypeName "' + prototypeName
+ '" conflicts with Blockly.Generator members. Registering Generators '
+ 'for this block type will incur errors.'
+ '\nThis name will be DISALLOWED (throwing an error) in future '
+ 'versions of Blockly.');
console.warn('FUTURE ERROR: Block prototypeName "' + prototypeName +
'" conflicts with Blockly.Generator members. Registering Generators ' +
'for this block type will incur errors.' +
'\nThis name will be DISALLOWED (throwing an error) in future ' +
'versions of Blockly.');
}
/** @type {string} */
@@ -153,15 +152,16 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
/** @type {string} */
this.type = prototypeName;
var prototype = Blockly.Blocks[prototypeName];
goog.asserts.assertObject(prototype,
'Error: Unknown block type "%s".', prototypeName);
if (!prototype || typeof prototype != 'object') {
throw Error('Unknown block type: ' + prototypeName);
}
goog.mixin(this, prototype);
}
workspace.addTopBlock(this);
// Call an initialization function, if it exists.
if (goog.isFunction(this.init)) {
if (typeof this.init == 'function') {
this.init();
}
// Record initial inline state.
@@ -184,7 +184,7 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
}
// Bind an onchange function, if it exists.
if (goog.isFunction(this.onchange)) {
if (typeof this.onchange == 'function') {
this.setOnChange(this.onchange);
}
};
@@ -501,10 +501,10 @@ Blockly.Block.prototype.setParent = function(newParent) {
// Disconnect from superior blocks.
if (this.previousConnection && this.previousConnection.isConnected()) {
throw 'Still connected to previous block.';
throw Error('Still connected to previous block.');
}
if (this.outputConnection && this.outputConnection.isConnected()) {
throw 'Still connected to parent block.';
throw Error('Still connected to parent block.');
}
this.parentBlock_ = null;
// This block hasn't actually moved on-screen, so there's no need to update
@@ -686,14 +686,14 @@ Blockly.Block.prototype.getHue = function() {
* or a message reference string pointing to one of those two values.
*/
Blockly.Block.prototype.setColour = function(colour) {
var dereferenced = goog.isString(colour) ?
var dereferenced = (typeof colour == 'string') ?
Blockly.utils.replaceMessageReferences(colour) : colour;
var hue = Number(dereferenced);
if (!isNaN(hue) && 0 <= hue && hue <= 360) {
this.hue_ = hue;
this.colour_ = Blockly.hueToRgb(hue);
} else if (goog.isString(dereferenced) &&
} else if ((typeof dereferenced == 'string') &&
/^#[0-9a-fA-F]{6}$/.test(dereferenced)) {
this.colour_ = dereferenced;
// Only store hue if colour is set as a hue.
@@ -717,8 +717,8 @@ Blockly.Block.prototype.setColour = function(colour) {
* @throws {Error} if onchangeFn is not falsey or a function.
*/
Blockly.Block.prototype.setOnChange = function(onchangeFn) {
if (onchangeFn && !goog.isFunction(onchangeFn)) {
throw new Error("onchange must be a function.");
if (onchangeFn && typeof onchangeFn != 'function') {
throw new Error('onchange must be a function.');
}
if (this.onchangeWrapper_) {
this.workspace.removeChangeListener(this.onchangeWrapper_);
@@ -840,7 +840,9 @@ Blockly.Block.prototype.getFieldValue = function(name) {
*/
Blockly.Block.prototype.setFieldValue = function(newValue, name) {
var field = this.getField(name);
goog.asserts.assertObject(field, 'Field "%s" not found.', name);
if (!field) {
throw ReferenceError('Field "' + name + '" not found.');
}
field.setValue(newValue);
};
@@ -856,16 +858,20 @@ Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) {
opt_check = null;
}
if (!this.previousConnection) {
goog.asserts.assert(!this.outputConnection,
'Remove output connection prior to adding previous connection.');
if (this.outputConnection) {
throw Error('Remove output connection prior to adding previous ' +
'connection.');
}
this.previousConnection =
this.makeConnection_(Blockly.PREVIOUS_STATEMENT);
}
this.previousConnection.setCheck(opt_check);
} else {
if (this.previousConnection) {
goog.asserts.assert(!this.previousConnection.isConnected(),
'Must disconnect previous statement before removing connection.');
if (this.previousConnection.isConnected()) {
throw Error('Must disconnect previous statement before removing ' +
'connection.');
}
this.previousConnection.dispose();
this.previousConnection = null;
}
@@ -889,8 +895,10 @@ Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) {
this.nextConnection.setCheck(opt_check);
} else {
if (this.nextConnection) {
goog.asserts.assert(!this.nextConnection.isConnected(),
'Must disconnect next statement before removing connection.');
if (this.nextConnection.isConnected()) {
throw Error('Must disconnect next statement before removing ' +
'connection.');
}
this.nextConnection.dispose();
this.nextConnection = null;
}
@@ -910,15 +918,18 @@ Blockly.Block.prototype.setOutput = function(newBoolean, opt_check) {
opt_check = null;
}
if (!this.outputConnection) {
goog.asserts.assert(!this.previousConnection,
'Remove previous connection prior to adding output connection.');
if (this.previousConnection) {
throw Error('Remove previous connection prior to adding output ' +
'connection.');
}
this.outputConnection = this.makeConnection_(Blockly.OUTPUT_VALUE);
}
this.outputConnection.setCheck(opt_check);
} else {
if (this.outputConnection) {
goog.asserts.assert(!this.outputConnection.isConnected(),
'Must disconnect output value before removing connection.');
if (this.outputConnection.isConnected()) {
throw Error('Must disconnect output value before removing connection.');
}
this.outputConnection.dispose();
this.outputConnection = null;
}
@@ -1093,9 +1104,10 @@ Blockly.Block.prototype.jsonInit = function(json) {
var warningPrefix = json['type'] ? 'Block "' + json['type'] + '": ' : '';
// Validate inputs.
goog.asserts.assert(
json['output'] == undefined || json['previousStatement'] == undefined,
warningPrefix + 'Must not have both an output and a previousStatement.');
if (json['output'] && json['previousStatement']) {
throw Error(warningPrefix +
'Must not have both an output and a previousStatement.');
}
// Set basic properties of block.
this.jsonInitColour_(json, warningPrefix);
@@ -1135,7 +1147,7 @@ Blockly.Block.prototype.jsonInit = function(json) {
var localizedValue = Blockly.utils.replaceMessageReferences(rawValue);
this.setHelpUrl(localizedValue);
}
if (goog.isString(json['extensions'])) {
if (typeof json['extensions'] == 'string') {
console.warn(
warningPrefix + 'JSON attribute \'extensions\' should be an array of' +
' strings. Found raw string in JSON for \'' + json['type'] +
@@ -1188,8 +1200,8 @@ Blockly.Block.prototype.jsonInitColour_ = function(json, warningPrefix) {
* @param {boolean=} opt_disableCheck Option flag to disable overwrite checks.
*/
Blockly.Block.prototype.mixin = function(mixinObj, opt_disableCheck) {
if (goog.isDef(opt_disableCheck) && !goog.isBoolean(opt_disableCheck)) {
throw new Error("opt_disableCheck must be a boolean if provided");
if (opt_disableCheck !== undefined && typeof opt_disableCheck != 'boolean') {
throw new Error('opt_disableCheck must be a boolean if provided');
}
if (!opt_disableCheck) {
var overwrites = [];
@@ -1368,9 +1380,12 @@ Blockly.Block.prototype.moveInputBefore = function(name, refName) {
}
}
}
goog.asserts.assert(inputIndex != -1, 'Named input "%s" not found.', name);
goog.asserts.assert(
refIndex != -1, 'Reference input "%s" not found.', refName);
if (inputIndex == -1) {
throw ReferenceError('Named input "' + name + '" not found.');
}
if (refIndex == -1) {
throw ReferenceError('Reference input "' + refName + '" not found.');
}
this.moveNumberedInputBefore(inputIndex, refIndex);
};
@@ -1382,11 +1397,15 @@ Blockly.Block.prototype.moveInputBefore = function(name, refName) {
Blockly.Block.prototype.moveNumberedInputBefore = function(
inputIndex, refIndex) {
// Validate arguments.
goog.asserts.assert(inputIndex != refIndex, 'Can\'t move input to itself.');
goog.asserts.assert(inputIndex < this.inputList.length,
'Input index ' + inputIndex + ' out of bounds.');
goog.asserts.assert(refIndex <= this.inputList.length,
'Reference input ' + refIndex + ' out of bounds.');
if (inputIndex == refIndex) {
throw Error('Can\'t move input to itself.');
}
if (inputIndex >= this.inputList.length) {
throw RangeError('Input index ' + inputIndex + ' out of bounds.');
}
if (refIndex > this.inputList.length) {
throw RangeError('Reference input ' + refIndex + ' out of bounds.');
}
// Remove input.
var input = this.inputList[inputIndex];
this.inputList.splice(inputIndex, 1);
@@ -1401,7 +1420,7 @@ Blockly.Block.prototype.moveNumberedInputBefore = function(
* Remove an input from this block.
* @param {string} name The name of the input.
* @param {boolean=} opt_quiet True to prevent error if input is not present.
* @throws {goog.asserts.AssertionError} if the input is not present and
* @throws {ReferenceError} if the input is not present and
* opt_quiet is not true.
*/
Blockly.Block.prototype.removeInput = function(name, opt_quiet) {
@@ -1424,7 +1443,7 @@ Blockly.Block.prototype.removeInput = function(name, opt_quiet) {
}
}
if (!opt_quiet) {
goog.asserts.fail('Input "%s" not found.', name);
throw ReferenceErrer('Input not found: ' + name);
}
};
@@ -1508,7 +1527,9 @@ Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
* @param {number} dy Vertical offset, in workspace units.
*/
Blockly.Block.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
if (this.parentBlock_) {
throw Error('Block has parent.');
}
var event = new Blockly.Events.BlockMove(this);
this.xy_.translate(dx, dy);
event.recordNew();

View File

@@ -31,7 +31,6 @@
goog.provide('Blockly.BlockDragSurfaceSvg');
goog.require('Blockly.utils');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
@@ -113,8 +112,9 @@ Blockly.BlockDragSurfaceSvg.prototype.createDom = function() {
* surface.
*/
Blockly.BlockDragSurfaceSvg.prototype.setBlocksAndShow = function(blocks) {
goog.asserts.assert(
this.dragGroup_.childNodes.length == 0, 'Already dragging a block.');
if (this.dragGroup_.childNodes.length) {
throw Error('Already dragging a block.');
}
// appendChild removes the blocks from the previous parent
this.dragGroup_.appendChild(blocks);
this.SVG_.style.display = 'block';
@@ -214,7 +214,8 @@ Blockly.BlockDragSurfaceSvg.prototype.clearAndHide = function(opt_newSurface) {
this.dragGroup_.removeChild(this.getCurrentBlock());
}
this.SVG_.style.display = 'none';
goog.asserts.assert(
this.dragGroup_.childNodes.length == 0, 'Drag group was not cleared.');
if (this.dragGroup_.childNodes.length) {
throw Error('Drag group was not cleared.');
}
this.surfaceXY_ = null;
};

View File

@@ -31,7 +31,6 @@ goog.require('Blockly.DraggedConnectionManager');
goog.require('Blockly.Events.BlockMove');
goog.require('goog.math.Coordinate');
goog.require('goog.asserts');
/**

View File

@@ -300,7 +300,7 @@ Blockly.Events.Delete = function(block) {
return; // Blank event to be populated by fromJson.
}
if (block.getParent()) {
throw 'Connected blocks cannot be deleted.';
throw Error('Connected blocks cannot be deleted.');
}
Blockly.Events.Delete.superClass_.constructor.call(this, block);

View File

@@ -37,7 +37,6 @@ goog.require('Blockly.Tooltip');
goog.require('Blockly.Touch');
goog.require('Blockly.utils');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.math.Coordinate');
@@ -145,7 +144,9 @@ Blockly.BlockSvg.INLINE = -1;
* May be called more than once.
*/
Blockly.BlockSvg.prototype.initSvg = function() {
goog.asserts.assert(this.workspace.rendered, 'Workspace is headless.');
if (!this.workspace.rendered) {
throw TypeError('Workspace is headless.');
}
for (var i = 0, input; input = this.inputList[i]; i++) {
input.init();
}
@@ -327,7 +328,9 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
* @param {number} dy Vertical offset in workspace units.
*/
Blockly.BlockSvg.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
if (this.parentBlock_) {
throw Error('Block has parent.');
}
var eventsEnabled = Blockly.Events.isEnabled();
if (eventsEnabled) {
var event = new Blockly.Events.BlockMove(this);
@@ -594,7 +597,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) {
* @private
*/
Blockly.BlockSvg.prototype.showHelp_ = function() {
var url = goog.isFunction(this.helpUrl) ? this.helpUrl() : this.helpUrl;
var url = (typeof this.helpUrl == 'function') ? this.helpUrl() : this.helpUrl;
if (url) {
window.open(url);
}
@@ -936,7 +939,7 @@ Blockly.BlockSvg.prototype.getCommentText = function() {
*/
Blockly.BlockSvg.prototype.setCommentText = function(text) {
var changedState = false;
if (goog.isString(text)) {
if (typeof text == 'string') {
if (!this.comment) {
this.comment = new Blockly.Comment(this);
changedState = true;
@@ -1009,7 +1012,7 @@ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) {
}
var changedState = false;
if (goog.isString(text)) {
if (typeof text == 'string') {
if (!this.warning) {
this.warning = new Blockly.Warning(this);
changedState = true;
@@ -1213,7 +1216,7 @@ Blockly.BlockSvg.prototype.setInputsInline = function(newBoolean) {
* Remove an input from this block.
* @param {string} name The name of the input.
* @param {boolean=} opt_quiet True to prevent error if input is not present.
* @throws {goog.asserts.AssertionError} if the input is not present and
* @throws {ReferenceError} if the input is not present and
* opt_quiet is not true.
*/
Blockly.BlockSvg.prototype.removeInput = function(name, opt_quiet) {

View File

@@ -31,7 +31,6 @@ goog.require('Blockly.Events.CommentMove');
goog.require('Blockly.WorkspaceCommentSvg');
goog.require('goog.math.Coordinate');
goog.require('goog.asserts');
/**

View File

@@ -28,7 +28,6 @@ goog.provide('Blockly.Connection');
goog.require('Blockly.Events.BlockMove');
goog.require('goog.asserts');
goog.require('goog.dom');
@@ -160,7 +159,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {
// Value connections.
// If female block is already connected, disconnect and bump the male.
if (!orphanBlock.outputConnection) {
throw 'Orphan block does not have an output connection.';
throw Error('Orphan block does not have an output connection.');
}
// Attempt to reattach the orphan at the end of the newly inserted
// block. Since this block may be a row, walk down to the end
@@ -176,7 +175,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {
// Statement blocks may be inserted into the middle of a stack.
// Split the stack.
if (!orphanBlock.previousConnection) {
throw 'Orphan block does not have a previous connection.';
throw Error('Orphan block does not have a previous connection.');
}
// Attempt to reattach the orphan at the bottom of the newly inserted
// block. Since this block may be a stack, walk down to the end.
@@ -238,7 +237,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {
*/
Blockly.Connection.prototype.dispose = function() {
if (this.isConnected()) {
throw 'Disconnect connection before disposing of it.';
throw Error('Disconnect connection before disposing of it.');
}
if (this.inDB_) {
this.db_.removeConnection_(this);
@@ -317,22 +316,22 @@ Blockly.Connection.prototype.checkConnection_ = function(target) {
case Blockly.Connection.CAN_CONNECT:
break;
case Blockly.Connection.REASON_SELF_CONNECTION:
throw 'Attempted to connect a block to itself.';
throw Error('Attempted to connect a block to itself.');
case Blockly.Connection.REASON_DIFFERENT_WORKSPACES:
// Usually this means one block has been deleted.
throw 'Blocks not on same workspace.';
throw Error('Blocks not on same workspace.');
case Blockly.Connection.REASON_WRONG_TYPE:
throw 'Attempt to connect incompatible types.';
throw Error('Attempt to connect incompatible types.');
case Blockly.Connection.REASON_TARGET_NULL:
throw 'Target connection is null.';
throw Error('Target connection is null.');
case Blockly.Connection.REASON_CHECKS_FAILED:
var msg = 'Connection checks failed. ';
msg += this + ' expected ' + this.check_ + ', found ' + target.check_;
throw msg;
throw Error(msg);
case Blockly.Connection.REASON_SHADOW_PARENT:
throw 'Connecting non-shadow to shadow block.';
throw Error('Connecting non-shadow to shadow block.');
default:
throw 'Unknown connection failure: this should never happen!';
throw Error('Unknown connection failure: this should never happen!');
}
};
@@ -414,7 +413,9 @@ Blockly.Connection.prototype.connect = function(otherConnection) {
* @private
*/
Blockly.Connection.connectReciprocally_ = function(first, second) {
goog.asserts.assert(first && second, 'Cannot connect null connections.');
if (!first || !second) {
throw Error('Cannot connect null connections.');
}
first.targetConnection = second;
second.targetConnection = first;
};
@@ -474,10 +475,12 @@ Blockly.Connection.lastConnectionInRow_ = function(startBlock, orphanBlock) {
*/
Blockly.Connection.prototype.disconnect = function() {
var otherConnection = this.targetConnection;
goog.asserts.assert(otherConnection, 'Source connection not connected.');
goog.asserts.assert(otherConnection.targetConnection == this,
'Target connection not connected to source connection.');
if (!otherConnection) {
throw Error('Source connection not connected.');
}
if (otherConnection.targetConnection != this) {
throw Error('Target connection not connected to source connection.');
}
var parentBlock, childBlock, parentConnection;
if (this.isSuperior()) {
// Superior block.
@@ -531,7 +534,7 @@ Blockly.Connection.prototype.respawnShadow_ = function() {
} else if (blockShadow.previousConnection) {
this.connect(blockShadow.previousConnection);
} else {
throw 'Child block does not have output or previous statement.';
throw Error('Child block does not have output or previous statement.');
}
}
};
@@ -591,7 +594,7 @@ Blockly.Connection.prototype.onCheckChanged_ = function() {
Blockly.Connection.prototype.setCheck = function(check) {
if (check) {
// Ensure that check is in an array.
if (!goog.isArray(check)) {
if (!Array.isArray(check)) {
check = [check];
}
this.check_ = check;

View File

@@ -51,7 +51,7 @@ Blockly.ConnectionDB.constructor = Blockly.ConnectionDB;
*/
Blockly.ConnectionDB.prototype.addConnection = function(connection) {
if (connection.inDB_) {
throw 'Connection already in database.';
throw Error('Connection already in database.');
}
if (connection.getSourceBlock().isInFlyout) {
// Don't bother maintaining a database of connections in a flyout.
@@ -137,11 +137,11 @@ Blockly.ConnectionDB.prototype.findPositionForConnection_ = function(
*/
Blockly.ConnectionDB.prototype.removeConnection_ = function(connection) {
if (!connection.inDB_) {
throw 'Connection not in database.';
throw Error('Connection not in database.');
}
var removalIndex = this.findConnection(connection);
if (removalIndex == -1) {
throw 'Unable to find connection in connectionDB.';
throw Error('Unable to find connection in connectionDB.');
}
connection.inDB_ = false;
this.splice(removalIndex, 1);

View File

@@ -241,7 +241,8 @@ Blockly.ContextMenu.blockDeleteOption = function(block) {
* @package
*/
Blockly.ContextMenu.blockHelpOption = function(block) {
var url = goog.isFunction(block.helpUrl) ? block.helpUrl() : block.helpUrl;
var url = (typeof block.helpUrl == 'function') ?
block.helpUrl() : block.helpUrl;
var helpOption = {
enabled: !!url,
text: Blockly.Msg['HELP'],

View File

@@ -372,7 +372,7 @@ Blockly.Events.fromJson = function(json, workspace) {
event = new Blockly.Events.CommentDelete(null);
break;
default:
throw 'Unknown event type.';
throw Error('Unknown event type.');
}
event.fromJson(json);
event.workspaceId = workspace.id;

View File

@@ -35,7 +35,6 @@ goog.provide('Blockly.Extensions');
goog.require('Blockly.Mutator');
goog.require('Blockly.utils');
goog.require('goog.string');
/**
* The set of all registered extensions, keyed by extension name/id.
@@ -54,13 +53,13 @@ Blockly.Extensions.ALL_ = {};
* registered, or extensionFn is not a function.
*/
Blockly.Extensions.register = function(name, initFn) {
if (!goog.isString(name) || goog.string.isEmptyOrWhitespace(name)) {
if ((typeof name != 'string') || (name.trim() == '')) {
throw new Error('Error: Invalid extension name "' + name + '"');
}
if (Blockly.Extensions.ALL_[name]) {
throw new Error('Error: Extension "' + name + '" is already registered.');
}
if (!goog.isFunction(initFn)) {
if (typeof initFn != 'function') {
throw new Error('Error: Extension "' + name + '" must be a function');
}
Blockly.Extensions.ALL_[name] = initFn;
@@ -74,7 +73,7 @@ Blockly.Extensions.register = function(name, initFn) {
* registered.
*/
Blockly.Extensions.registerMixin = function(name, mixinObj) {
if (!goog.isObject(mixinObj)){
if (!mixinObj || typeof mixinObj != 'object'){
throw new Error('Error: Mixin "' + name + '" must be a object');
}
Blockly.Extensions.register(name, function() {
@@ -108,7 +107,7 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn,
var hasMutatorDialog =
Blockly.Extensions.checkMutatorDialog_(mixinObj, errorPrefix);
if (opt_helperFn && !goog.isFunction(opt_helperFn)) {
if (opt_helperFn && (typeof opt_helperFn != 'function')) {
throw new Error('Extension "' + name + '" is not a function');
}
@@ -136,7 +135,7 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn,
*/
Blockly.Extensions.apply = function(name, block, isMutator) {
var extensionFn = Blockly.Extensions.ALL_[name];
if (!goog.isFunction(extensionFn)) {
if (typeof extensionFn != 'function') {
throw new Error('Error: Extension "' + name + '" not found.');
}
if (isMutator) {

View File

@@ -31,7 +31,6 @@ goog.provide('Blockly.Field');
goog.require('Blockly.Events.BlockChange');
goog.require('Blockly.Gesture');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.math.Size');
goog.require('goog.style');
@@ -71,10 +70,10 @@ Blockly.Field.TYPE_MAP_ = {};
* object containing a fromJson function.
*/
Blockly.Field.register = function(type, fieldClass) {
if (!goog.isString(type) || goog.string.isEmptyOrWhitespace(type)) {
if ((typeof type != 'string') || (type.trim() == '')) {
throw new Error('Invalid field type "' + type + '"');
}
if (!goog.isObject(fieldClass) || !goog.isFunction(fieldClass.fromJson)) {
if (!fieldClass || (typeof fieldClass.fromJson != 'function')) {
throw new Error('Field "' + fieldClass +
'" must have a fromJson function');
}
@@ -171,7 +170,9 @@ Blockly.Field.prototype.EDITABLE = true;
* @param {!Blockly.Block} block The block containing this field.
*/
Blockly.Field.prototype.setSourceBlock = function(block) {
goog.asserts.assert(!this.sourceBlock_, 'Field already bound to a block.');
if (this.sourceBlock_) {
throw TypeError('Field already bound to a block.');
}
this.sourceBlock_ = block;
};

View File

@@ -53,7 +53,7 @@ goog.require('goog.userAgent');
* @constructor
*/
Blockly.FieldDropdown = function(menuGenerator, opt_validator) {
if (!goog.isFunction(menuGenerator)) {
if (typeof menuGenerator != 'function') {
Blockly.FieldDropdown.validateOptions_(menuGenerator);
}
this.menuGenerator_ = menuGenerator;
@@ -291,7 +291,7 @@ Blockly.FieldDropdown.prototype.trimOptions_ = function() {
this.prefixField = null;
this.suffixField = null;
var options = this.menuGenerator_;
if (!goog.isArray(options)) {
if (!Array.isArray(options)) {
return;
}
var hasImages = false;
@@ -362,7 +362,7 @@ Blockly.FieldDropdown.applyTrim_ = function(options, prefixLength, suffixLength)
* Otherwise false.
*/
Blockly.FieldDropdown.prototype.isOptionListDynamic = function() {
return goog.isFunction(this.menuGenerator_);
return typeof this.menuGenerator_ == 'function';
};
/**
@@ -372,7 +372,7 @@ Blockly.FieldDropdown.prototype.isOptionListDynamic = function() {
* @throws If generated options are incorrectly structured.
*/
Blockly.FieldDropdown.prototype.getOptions = function() {
if (goog.isFunction(this.menuGenerator_)) {
if (this.isOptionListDynamic()) {
var generatedOptions = this.menuGenerator_.call(this);
Blockly.FieldDropdown.validateOptions_(generatedOptions);
return generatedOptions;
@@ -537,23 +537,24 @@ Blockly.FieldDropdown.prototype.dispose = function() {
* @private
*/
Blockly.FieldDropdown.validateOptions_ = function(options) {
if (!goog.isArray(options)) {
throw 'FieldDropdown options must be an array.';
if (!Array.isArray(options)) {
throw Error('FieldDropdown options must be an array.');
}
var foundError = false;
for (var i = 0; i < options.length; ++i) {
var tuple = options[i];
if (!goog.isArray(options)) {
if (!Array.isArray(options)) {
foundError = true;
console.error(
'Invalid option[' + i + ']: Each FieldDropdown option must be an ' +
'array. Found: ', tuple);
} else if (!goog.isString(tuple[1])) {
} else if (typeof tuple[1] != 'string') {
foundError = true;
console.error(
'Invalid option[' + i + ']: Each FieldDropdown option id must be ' +
'a string. Found ' + tuple[1] + ' in: ', tuple);
} else if (!goog.isString(tuple[0]) && !goog.isString(tuple[0].src)) {
} else if ((typeof tuple[0] != 'string') &&
(typeof tuple[0].src != 'string')) {
foundError = true;
console.error(
'Invalid option[' + i + ']: Each FieldDropdown option must have a ' +
@@ -562,7 +563,7 @@ Blockly.FieldDropdown.validateOptions_ = function(options) {
}
}
if (foundError) {
throw 'Found invalid FieldDropdown options.';
throw TypeError('Found invalid FieldDropdown options.');
}
};

View File

@@ -28,7 +28,6 @@ goog.provide('Blockly.FieldTextInput');
goog.require('Blockly.Field');
goog.require('Blockly.Msg');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.userAgent');
@@ -300,7 +299,9 @@ Blockly.FieldTextInput.prototype.onHtmlInputChange_ = function(_e) {
*/
Blockly.FieldTextInput.prototype.validate_ = function() {
var valid = true;
goog.asserts.assertObject(Blockly.FieldTextInput.htmlInput_);
if (!Blockly.FieldTextInput.htmlInput_) {
throw TypeError('htmlInput not defined');
}
var htmlInput = Blockly.FieldTextInput.htmlInput_;
if (this.sourceBlock_) {
valid = this.callValidator(htmlInput.value);

View File

@@ -30,7 +30,6 @@ goog.require('Blockly.FieldDropdown');
goog.require('Blockly.Msg');
goog.require('Blockly.VariableModel');
goog.require('Blockly.Variables');
goog.require('goog.asserts');
goog.require('goog.string');
@@ -132,8 +131,9 @@ Blockly.FieldVariable.prototype.dispose = function() {
* @param {!Blockly.Block} block The block containing this field.
*/
Blockly.FieldVariable.prototype.setSourceBlock = function(block) {
goog.asserts.assert(!block.isShadow(),
'Variable fields are not allowed to exist on shadow blocks.');
if (block.isShadow()) {
throw Error('Variable fields are not allowed to exist on shadow blocks.');
}
Blockly.FieldVariable.superClass_.setSourceBlock.call(this, block);
};

View File

@@ -426,11 +426,13 @@ Blockly.Flyout.prototype.show = function(xmlList) {
if (typeof xmlList == 'string') {
var fnToApply = this.workspace_.targetWorkspace.getToolboxCategoryCallback(
xmlList);
goog.asserts.assert(goog.isFunction(fnToApply),
'Couldn\'t find a callback function when opening a toolbox category.');
if (typeof fnToApply != 'function') {
throw TypeError('Couldn\'t find a callback function when opening a toolbox category.');
}
xmlList = fnToApply(this.workspace_.targetWorkspace);
goog.asserts.assert(goog.isArray(xmlList),
'The result of a toolbox category callback must be an array.');
if (!Array.isArray(xmlList)) {
throw TypeError('Result of toolbox category callback must be an array.');
}
}
this.setVisible(true);
@@ -762,7 +764,7 @@ Blockly.Flyout.prototype.placeNewBlock_ = function(oldBlock) {
var targetWorkspace = this.targetWorkspace_;
var svgRootOld = oldBlock.getSvgRoot();
if (!svgRootOld) {
throw 'oldBlock is not rendered.';
throw Error('oldBlock is not rendered.');
}
// Create the new block by cloning the block in the flyout (via XML).
@@ -776,7 +778,7 @@ Blockly.Flyout.prototype.placeNewBlock_ = function(oldBlock) {
var block = Blockly.Xml.domToBlock(xml, targetWorkspace);
var svgRootNew = block.getSvgRoot();
if (!svgRootNew) {
throw 'block is not rendered.';
throw Error('block is not rendered.');
}
// The offset in pixels between the main workspace's origin and the upper left

View File

@@ -28,7 +28,6 @@ goog.provide('Blockly.FlyoutDragger');
goog.require('Blockly.WorkspaceDragger');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');

View File

@@ -126,7 +126,7 @@ Blockly.HorizontalFlyout.prototype.setMetrics_ = function(xyRatio) {
return;
}
if (goog.isNumber(xyRatio.x)) {
if (typeof xyRatio.x == 'number') {
this.workspace_.scrollX = -metrics.contentWidth * xyRatio.x;
}

View File

@@ -125,7 +125,7 @@ Blockly.VerticalFlyout.prototype.setMetrics_ = function(xyRatio) {
if (!metrics) {
return;
}
if (goog.isNumber(xyRatio.y)) {
if (typeof xyRatio.y == 'number') {
this.workspace_.scrollY = -metrics.contentHeight * xyRatio.y;
}
this.workspace_.translate(this.workspace_.scrollX + metrics.absoluteLeft,

View File

@@ -28,7 +28,6 @@
goog.provide('Blockly.Generator');
goog.require('Blockly.Block');
goog.require('goog.asserts');
/**
@@ -99,7 +98,7 @@ Blockly.Generator.prototype.workspaceToCode = function(workspace) {
var blocks = workspace.getTopBlocks(true);
for (var x = 0, block; block = blocks[x]; x++) {
var line = this.blockToCode(block);
if (goog.isArray(line)) {
if (Array.isArray(line)) {
// Value blocks return tuples of code and operator order.
// Top-level blocks don't care about operator order.
line = line[0];
@@ -173,31 +172,32 @@ Blockly.Generator.prototype.blockToCode = function(block) {
}
var func = this[block.type];
goog.asserts.assertFunction(func,
'Language "%s" does not know how to generate code for block type "%s".',
this.name_, block.type);
if (typeof func != 'function') {
throw Error('Language "' + this.name_ + '" does not know how to generate ' +
' code for block type "' + block.type + '".');
}
// First argument to func.call is the value of 'this' in the generator.
// Prior to 24 September 2013 'this' was the only way to access the block.
// The current prefered method of accessing the block is through the second
// argument to func.call, which becomes the first parameter to the generator.
var code = func.call(block, block);
if (goog.isArray(code)) {
if (Array.isArray(code)) {
// Value blocks return tuples of code and operator order.
goog.asserts.assert(block.outputConnection,
'Expecting string from statement block "%s".', block.type);
if (!block.outputConnection) {
throw Error('Expecting string from statement block: ' + block.type);
}
return [this.scrub_(block, code[0]), code[1]];
} else if (goog.isString(code)) {
} else if (typeof code == 'string') {
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
if (this.STATEMENT_PREFIX) {
code = this.STATEMENT_PREFIX.replace(/%1/g, '\'' + id + '\'') +
code;
code = this.STATEMENT_PREFIX.replace(/%1/g, '\'' + id + '\'') + code;
}
return this.scrub_(block, code);
} else if (code === null) {
// Block has handled code generation itself.
return '';
} else {
goog.asserts.fail('Invalid code generated: %s', code);
throw SyntaxError('Invalid code generated: ' + code);
}
};
@@ -212,7 +212,7 @@ Blockly.Generator.prototype.blockToCode = function(block) {
*/
Blockly.Generator.prototype.valueToCode = function(block, name, outerOrder) {
if (isNaN(outerOrder)) {
goog.asserts.fail('Expecting valid order from block "%s".', block.type);
throw TypeError('Expecting valid order from block: ' + block.type);
}
var targetBlock = block.getInputTargetBlock(name);
if (!targetBlock) {
@@ -225,12 +225,13 @@ Blockly.Generator.prototype.valueToCode = function(block, name, outerOrder) {
}
// Value blocks must return code and order of operations info.
// Statement blocks must only return code.
goog.asserts.assertArray(tuple, 'Expecting tuple from value block "%s".',
targetBlock.type);
if (!Array.isArray(tuple)) {
throw TypeError('Expecting tuple from value block: ' + targetBlock.type);
}
var code = tuple[0];
var innerOrder = tuple[1];
if (isNaN(innerOrder)) {
goog.asserts.fail('Expecting valid order from value block "%s".',
throw TypeError('Expecting valid order from value block: ' +
targetBlock.type);
}
if (!code) {
@@ -282,8 +283,10 @@ Blockly.Generator.prototype.statementToCode = function(block, name) {
var code = this.blockToCode(targetBlock);
// Value blocks must return code and order of operations info.
// Statement blocks must only return code.
goog.asserts.assertString(code, 'Expecting code from statement block "%s".',
targetBlock && targetBlock.type);
if (typeof code != 'string') {
throw TypeError('Expecting code from statement block: ' +
(targetBlock && targetBlock.type));
}
if (code) {
code = this.prefixLines(/** @type {string} */ (code), this.INDENT);
}

View File

@@ -37,7 +37,6 @@ goog.require('Blockly.Tooltip');
goog.require('Blockly.Touch');
goog.require('Blockly.WorkspaceDragger');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
@@ -429,8 +428,9 @@ Blockly.Gesture.prototype.updateIsDraggingWorkspace_ = function() {
*/
Blockly.Gesture.prototype.updateIsDragging_ = function() {
// Sanity check.
goog.asserts.assert(!this.calledUpdateIsDragging_,
'updateIsDragging_ should only be called once per gesture.');
if (this.calledUpdateIsDragging_) {
throw Error('updateIsDragging_ should only be called once per gesture.');
}
this.calledUpdateIsDragging_ = true;
// First check if it was a bubble drag. Bubbles always sit on top of blocks.
@@ -648,9 +648,10 @@ Blockly.Gesture.prototype.handleRightClick = function(e) {
* @package
*/
Blockly.Gesture.prototype.handleWsStart = function(e, ws) {
goog.asserts.assert(!this.hasStarted_,
'Tried to call gesture.handleWsStart, but the gesture had already been ' +
'started.');
if (this.hasStarted_) {
throw Error('Tried to call gesture.handleWsStart, ' +
'but the gesture had already been started.');
}
this.setStartWorkspace_(ws);
this.mostRecentEvent_ = e;
this.doStart(e);
@@ -663,9 +664,10 @@ Blockly.Gesture.prototype.handleWsStart = function(e, ws) {
* @package
*/
Blockly.Gesture.prototype.handleFlyoutStart = function(e, flyout) {
goog.asserts.assert(!this.hasStarted_,
'Tried to call gesture.handleFlyoutStart, but the gesture had already ' +
'been started.');
if (this.hasStarted_) {
throw Error('Tried to call gesture.handleFlyoutStart, ' +
'but the gesture had already been started.');
}
this.setStartFlyout_(flyout);
this.handleWsStart(e, flyout.getWorkspace());
};
@@ -677,9 +679,10 @@ Blockly.Gesture.prototype.handleFlyoutStart = function(e, flyout) {
* @package
*/
Blockly.Gesture.prototype.handleBlockStart = function(e, block) {
goog.asserts.assert(!this.hasStarted_,
'Tried to call gesture.handleBlockStart, but the gesture had already ' +
'been started.');
if (this.hasStarted_) {
throw Error('Tried to call gesture.handleBlockStart, ' +
'but the gesture had already been started.');
}
this.setStartBlock(block);
this.mostRecentEvent_ = e;
};
@@ -691,9 +694,10 @@ Blockly.Gesture.prototype.handleBlockStart = function(e, block) {
* @package
*/
Blockly.Gesture.prototype.handleBubbleStart = function(e, bubble) {
goog.asserts.assert(!this.hasStarted_,
'Tried to call gesture.handleBubbleStart, but the gesture had already ' +
'been started.');
if (this.hasStarted_) {
throw Error('Tried to call gesture.handleBubbleStart, ' +
'but the gesture had already been started.');
}
this.setStartBubble(bubble);
this.mostRecentEvent_ = e;
};
@@ -779,9 +783,10 @@ Blockly.Gesture.prototype.bringBlockToFront_ = function() {
* @package
*/
Blockly.Gesture.prototype.setStartField = function(field) {
goog.asserts.assert(!this.hasStarted_,
'Tried to call gesture.setStartField, but the gesture had already been ' +
'started.');
if (this.hasStarted_) {
throw Error('Tried to call gesture.setStartField, ' +
'but the gesture had already been started.');
}
if (!this.startField_) {
this.startField_ = field;
}

View File

@@ -47,13 +47,13 @@ goog.require('goog.userAgent');
Blockly.inject = function(container, opt_options) {
Blockly.checkBlockColourConstants();
if (goog.isString(container)) {
if (typeof container == 'string') {
container = document.getElementById(container) ||
document.querySelector(container);
}
// Verify that the container is in document.
if (!goog.dom.contains(document, container)) {
throw 'Error: container is not in current document.';
throw Error('Error: container is not in current document.');
}
var options = new Blockly.Options(opt_options || {});
var subContainer = goog.dom.createDom(goog.dom.TagName.DIV, 'injectionDiv');

View File

@@ -28,7 +28,6 @@ goog.provide('Blockly.Input');
goog.require('Blockly.Connection');
goog.require('Blockly.FieldLabel');
goog.require('goog.asserts');
/**
@@ -42,7 +41,7 @@ goog.require('goog.asserts');
*/
Blockly.Input = function(type, name, block, connection) {
if (type != Blockly.DUMMY_INPUT && !name) {
throw 'Value inputs and statement inputs must have non-empty name.';
throw Error('Value inputs and statement inputs must have non-empty name.');
}
/** @type {number} */
this.type = type;
@@ -104,7 +103,7 @@ Blockly.Input.prototype.insertFieldAt = function(index, field, opt_name) {
return index;
}
// Generate a FieldLabel when given a plain text field.
if (goog.isString(field)) {
if (typeof field == 'string') {
field = new Blockly.FieldLabel(/** @type {string} */ (field));
}
field.setSourceBlock(this.sourceBlock_);
@@ -136,7 +135,7 @@ Blockly.Input.prototype.insertFieldAt = function(index, field, opt_name) {
/**
* Remove a field from this input.
* @param {string} name The name of the field.
* @throws {goog.asserts.AssertionError} if the field is not present.
* @throws {ReferenceError} if the field is not present.
*/
Blockly.Input.prototype.removeField = function(name) {
for (var i = 0, field; field = this.fieldRow[i]; i++) {
@@ -151,7 +150,7 @@ Blockly.Input.prototype.removeField = function(name) {
return;
}
}
goog.asserts.fail('Field "%s" not found.', name);
throw ReferenceError('Field "%s" not found.', name);
};
/**
@@ -205,7 +204,7 @@ Blockly.Input.prototype.setVisible = function(visible) {
*/
Blockly.Input.prototype.setCheck = function(check) {
if (!this.connection) {
throw 'This input does not have a connection.';
throw Error('This input does not have a connection.');
}
this.connection.setCheck(check);
return this;

View File

@@ -166,7 +166,7 @@ Blockly.RenderedConnection.prototype.tighten_ = function() {
var block = this.targetBlock();
var svgRoot = block.getSvgRoot();
if (!svgRoot) {
throw 'block is not rendered.';
throw Error('block is not rendered.');
}
// Workspace coordinates.
var xy = Blockly.utils.getRelativeXY(svgRoot);
@@ -351,7 +351,7 @@ Blockly.RenderedConnection.prototype.respawnShadow_ = function() {
Blockly.RenderedConnection.superClass_.respawnShadow_.call(this);
var blockShadow = this.targetBlock();
if (!blockShadow) {
throw 'Couldn\'t respawn the shadow block that should exist here.';
throw Error('Couldn\'t respawn the shadow block that should exist here.');
}
blockShadow.initSvg();
blockShadow.render(false);

View File

@@ -660,7 +660,7 @@ Blockly.Scrollbar.prototype.setVisible = function(visible) {
// Ideally this would also apply to scrollbar pairs, but that's a bigger
// headache (due to interactions with the corner square).
if (this.pair_) {
throw 'Unable to toggle visibility of paired scrollbars.';
throw Error('Unable to toggle visibility of paired scrollbars.');
}
this.isVisible_ = visible;
if (visibilityChanged) {

View File

@@ -286,7 +286,8 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) {
this.syncTrees_(newTree, this.tree_, this.workspace_.options.pathToMedia);
if (this.tree_.blocks.length) {
throw 'Toolbox cannot have both blocks and categories in the root level.';
throw Error('Toolbox cannot have both blocks and categories ' +
'in the root level.');
}
// Fire a resize event since the toolbox may have changed width and height.

View File

@@ -167,7 +167,8 @@ Blockly.Tooltip.onMouseOver_ = function(e) {
// If the tooltip is an object, treat it as a pointer to the next object in
// the chain to look at. Terminate when a string or function is found.
var element = e.target;
while (!goog.isString(element.tooltip) && !goog.isFunction(element.tooltip)) {
while ((typeof element.tooltip != 'string') &&
(typeof element.tooltip != 'function')) {
element = element.tooltip;
}
if (Blockly.Tooltip.element_ != element) {
@@ -289,7 +290,7 @@ Blockly.Tooltip.show_ = function() {
goog.dom.removeChildren(/** @type {!Element} */ (Blockly.Tooltip.DIV));
// Get the new text.
var tip = Blockly.Tooltip.element_.tooltip;
while (goog.isFunction(tip)) {
while (typeof tip == 'function') {
tip = tip();
}
tip = Blockly.utils.wrap(tip, Blockly.Tooltip.LIMIT);

View File

@@ -29,7 +29,6 @@ goog.provide('Blockly.TouchGesture');
goog.require('Blockly.Gesture');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');

View File

@@ -403,7 +403,7 @@ Blockly.utils.tokenizeInterpolation = function(message) {
* @return {!string} String with message references replaced.
*/
Blockly.utils.replaceMessageReferences = function(message) {
if (!goog.isString(message)) {
if (typeof message != 'string') {
return message;
}
var interpolatedResult = Blockly.utils.tokenizeInterpolation_(message, false);
@@ -511,9 +511,9 @@ Blockly.utils.tokenizeInterpolation_ = function(message,
} else if (state == 3) { // String table reference
if (c == '') {
// Premature end before closing '}'
buffer.splice(0, 0, '%{'); // Re-insert leading delimiter
buffer.splice(0, 0, '%{'); // Re-insert leading delimiter
i--; // Parse this char again.
state = 0; // and parse as string literal.
state = 0; // and parse as string literal.
} else if (c != '}') {
buffer.push(c);
} else {
@@ -529,7 +529,7 @@ Blockly.utils.tokenizeInterpolation_ = function(message,
keyUpper.substring(4) : null;
if (bklyKey && bklyKey in Blockly.Msg) {
var rawValue = Blockly.Msg[bklyKey];
if (goog.isString(rawValue)) {
if (typeof rawValue == 'string') {
// Attempt to dereference substrings, too, appending to the end.
Array.prototype.push.apply(tokens,
Blockly.utils.tokenizeInterpolation_(
@@ -551,7 +551,7 @@ Blockly.utils.tokenizeInterpolation_ = function(message,
} else {
tokens.push('%{' + rawKey + '}');
buffer.length = 0;
state = 0; // and parse as string literal.
state = 0; // and parse as string literal.
}
}
}
@@ -839,7 +839,7 @@ Blockly.utils.insertAfter_ = function(newNode, refNode) {
var siblingNode = refNode.nextSibling;
var parentNode = refNode.parentNode;
if (!parentNode) {
throw 'Reference node has no parent.';
throw Error('Reference node has no parent.');
}
if (siblingNode) {
parentNode.insertBefore(newNode, siblingNode);

View File

@@ -161,7 +161,7 @@ Blockly.Workspace.prototype.addTopBlock = function(block) {
*/
Blockly.Workspace.prototype.removeTopBlock = function(block) {
if (!goog.array.remove(this.topBlocks_, block)) {
throw 'Block not present in workspace\'s list of top-most blocks.';
throw Error('Block not present in workspace\'s list of top-most blocks.');
}
};
@@ -212,7 +212,8 @@ Blockly.Workspace.prototype.addTopComment = function(comment) {
*/
Blockly.Workspace.prototype.removeTopComment = function(comment) {
if (!goog.array.remove(this.topComments_, comment)) {
throw 'Comment not present in workspace\'s list of top-most comments.';
throw Error('Comment not present in workspace\'s list of top-most ' +
'comments.');
}
// Note: If the comment database starts to hold block comments, this may need
// to move to a separate function.

View File

@@ -132,7 +132,9 @@ Blockly.WorkspaceCommentSvg.prototype.dispose = function() {
* @package
*/
Blockly.WorkspaceCommentSvg.prototype.initSvg = function() {
goog.asserts.assert(this.workspace.rendered, 'Workspace is headless.');
if (!this.workspace.rendered) {
throw TypeError('Workspace is headless.');
}
if (!this.workspace.options.readOnly && !this.eventsInit_) {
Blockly.bindEventWithChecks_(
this.svgRectTarget_, 'mousedown', this, this.pathMouseDown_);

View File

@@ -32,7 +32,6 @@ goog.provide('Blockly.WorkspaceDragSurfaceSvg');
goog.require('Blockly.utils');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
@@ -138,14 +137,16 @@ Blockly.WorkspaceDragSurfaceSvg.prototype.getSurfaceTranslation = function() {
*/
Blockly.WorkspaceDragSurfaceSvg.prototype.clearAndHide = function(newSurface) {
if (!newSurface) {
throw 'Couldn\'t clear and hide the drag surface: missing new surface.';
throw Error('Couldn\'t clear and hide the drag surface: missing ' +
'new surface.');
}
var blockCanvas = this.SVG_.childNodes[0];
var bubbleCanvas = this.SVG_.childNodes[1];
if (!blockCanvas || !bubbleCanvas ||
!Blockly.utils.hasClass(blockCanvas, 'blocklyBlockCanvas') ||
!Blockly.utils.hasClass(bubbleCanvas, 'blocklyBubbleCanvas')) {
throw 'Couldn\'t clear and hide the drag surface. A node was missing.';
throw Error('Couldn\'t clear and hide the drag surface. ' +
'A node was missing.');
}
// If there is a previous sibling, put the blockCanvas back right afterwards,
@@ -160,8 +161,9 @@ Blockly.WorkspaceDragSurfaceSvg.prototype.clearAndHide = function(newSurface) {
Blockly.utils.insertAfter_(bubbleCanvas, blockCanvas);
// Hide the drag surface.
this.SVG_.style.display = 'none';
goog.asserts.assert(
this.SVG_.childNodes.length == 0, 'Drag surface was not cleared.');
if (this.SVG_.childNodes.length) {
throw TypeError('Drag surface was not cleared.');
}
Blockly.utils.setCssTransform(this.SVG_, '');
this.previousSibling_ = null;
};
@@ -180,8 +182,9 @@ Blockly.WorkspaceDragSurfaceSvg.prototype.clearAndHide = function(newSurface) {
*/
Blockly.WorkspaceDragSurfaceSvg.prototype.setContentsAndShow = function(
blockCanvas, bubbleCanvas, previousSibling, width, height, scale) {
goog.asserts.assert(
this.SVG_.childNodes.length == 0, 'Already dragging a block.');
if (this.SVG_.childNodes.length) {
throw TypeError('Already dragging a block.');
}
this.previousSibling_ = previousSibling;
// Make sure the blocks and bubble canvas are scaled appropriately.
blockCanvas.setAttribute('transform', 'translate(0, 0) scale(' + scale + ')');

View File

@@ -27,7 +27,6 @@
goog.provide('Blockly.WorkspaceDragger');
goog.require('goog.math.Coordinate');
goog.require('goog.asserts');
/**

View File

@@ -1427,23 +1427,23 @@ Blockly.WorkspaceSvg.prototype.updateToolbox = function(tree) {
tree = Blockly.Options.parseToolboxTree(tree);
if (!tree) {
if (this.options.languageTree) {
throw 'Can\'t nullify an existing toolbox.';
throw Error('Can\'t nullify an existing toolbox.');
}
return; // No change (null to null).
}
if (!this.options.languageTree) {
throw 'Existing toolbox is null. Can\'t create new toolbox.';
throw Error('Existing toolbox is null. Can\'t create new toolbox.');
}
if (tree.getElementsByTagName('category').length) {
if (!this.toolbox_) {
throw 'Existing toolbox has no categories. Can\'t change mode.';
throw Error('Existing toolbox has no categories. Can\'t change mode.');
}
this.options.languageTree = tree;
this.toolbox_.populate_(tree);
this.toolbox_.addColour_();
} else {
if (!this.flyout_) {
throw 'Existing toolbox has categories. Can\'t change mode.';
throw Error('Existing toolbox has categories. Can\'t change mode.');
}
this.options.languageTree = tree;
this.flyout_.show(tree.childNodes);
@@ -1878,13 +1878,14 @@ Blockly.WorkspaceSvg.getTopLevelWorkspaceMetrics_ = function() {
*/
Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_ = function(xyRatio) {
if (!this.scrollbar) {
throw 'Attempt to set top level workspace scroll without scrollbars.';
throw Error('Attempt to set top level workspace scroll without ' +
'scrollbars.');
}
var metrics = this.getMetrics();
if (goog.isNumber(xyRatio.x)) {
if (typeof xyRatio.x == 'number') {
this.scrollX = -metrics.contentWidth * xyRatio.x - metrics.contentLeft;
}
if (goog.isNumber(xyRatio.y)) {
if (typeof xyRatio.y == 'number') {
this.scrollY = -metrics.contentHeight * xyRatio.y - metrics.contentTop;
}
var x = this.scrollX + metrics.absoluteLeft;
@@ -1932,8 +1933,9 @@ Blockly.WorkspaceSvg.prototype.clear = function() {
* given button is clicked.
*/
Blockly.WorkspaceSvg.prototype.registerButtonCallback = function(key, func) {
goog.asserts.assert(goog.isFunction(func),
'Button callbacks must be functions.');
if (typeof func != 'function') {
throw TypeError('Button callbacks must be functions.');
}
this.flyoutButtonCallbacks_[key] = func;
};
@@ -1967,8 +1969,9 @@ Blockly.WorkspaceSvg.prototype.removeButtonCallback = function(key) {
*/
Blockly.WorkspaceSvg.prototype.registerToolboxCategoryCallback = function(key,
func) {
goog.asserts.assert(goog.isFunction(func),
'Toolbox category callbacks must be functions.');
if (typeof func != 'function') {
throw TypeError('Toolbox category callbacks must be functions.');
}
this.toolboxCategoryCallbacks_[key] = func;
};

View File

@@ -373,7 +373,7 @@ Blockly.Xml.textToDom = function(text) {
if (!doc || !doc.documentElement ||
doc.documentElement.nodeName.toLowerCase() != 'xml') {
// Whatever we got back from the parser is not the expected structure.
throw Error('Blockly.Xml.textToDom expected an <xml> document.');
throw TypeError('Blockly.Xml.textToDom expected an <xml> document.');
}
return doc.documentElement;
};
@@ -448,7 +448,7 @@ Blockly.Xml.domToWorkspace = function(xml, workspace) {
}
variablesFirst = false;
} else if (name == 'shadow') {
throw Error('Shadow block cannot be a top-level block.');
throw TypeError('Shadow block cannot be a top-level block.');
} else if (name == 'comment') {
if (workspace.rendered) {
Blockly.WorkspaceCommentSvg.fromXml(xmlChild, workspace, width);
@@ -631,7 +631,7 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
var block = null;
var prototypeName = xmlBlock.getAttribute('type');
if (!prototypeName) {
throw Error('Block type unspecified: ' + xmlBlock.outerHTML);
throw TypeError('Block type unspecified: ' + xmlBlock.outerHTML);
}
var id = xmlBlock.getAttribute('id');
block = workspace.newBlock(prototypeName, id);
@@ -720,7 +720,7 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
} else if (blockChild.previousConnection) {
input.connection.connect(blockChild.previousConnection);
} else {
throw Error(
throw TypeError(
'Child block does not have output or previous statement.');
}
}
@@ -731,16 +731,16 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
}
if (childBlockElement) {
if (!block.nextConnection) {
throw Error('Next statement does not exist.');
throw TypeError('Next statement does not exist.');
}
// If there is more than one XML 'next' tag.
if (block.nextConnection.isConnected()) {
throw Error('Next statement is already connected.');
throw TypeError('Next statement is already connected.');
}
blockChild = Blockly.Xml.domToBlockHeadless_(childBlockElement,
workspace);
if (!blockChild.previousConnection) {
throw Error('Next block does not have previous statement.');
throw TypeError('Next block does not have previous statement.');
}
block.nextConnection.connect(blockChild.previousConnection);
}
@@ -779,13 +779,13 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
// Ensure all children are also shadows.
var children = block.getChildren(false);
for (var i = 0, child; child = children[i]; i++) {
if (child.isShadow()) {
throw Error('Shadow block not allowed non-shadow child.');
if (!child.isShadow()) {
throw TypeError('Shadow block not allowed non-shadow child.');
}
}
// Ensure this block doesn't have any variable inputs.
if (block.getVarModels().length) {
throw Error('Shadow blocks cannot have variable references.');
throw ReferenceError('Shadow blocks cannot have variable references.');
}
block.setShadow(true);
}