chore: replace more uses of var with const and let (#5628)

* chore: fix uses of var in core/block_dragger

* chore: fix uses of var in core/extensions.js

* chore: fix uses of var in core/field_multilineinput.js

* chore: fix uses of var in assorted core files

* chore: fix uses of var in node test runner and playground screenshot code

* fix: undefined return from measureFontMetrics

* fix: violations of no-const-assign

* chore: only one variable declaration per line
This commit is contained in:
Rachel Fenichel
2021-10-25 09:28:31 -07:00
committed by GitHub
parent 5cdc5f587f
commit f70032aaa6
11 changed files with 61 additions and 54 deletions

View File

@@ -275,12 +275,16 @@ BlockDragger.prototype.endDrag = function(e, currentDragDeltaXY) {
const preventMove = !!this.dragTarget_ &&
this.dragTarget_.shouldPreventMove(this.draggingBlock_);
/** @type {Coordinate} */
let newLoc;
/** @type {Coordinate} */
let delta;
if (preventMove) {
var newLoc = this.startXY_;
newLoc = this.startXY_;
} else {
const newValues = this.getNewLocationAfterDrag_(currentDragDeltaXY);
var delta = newValues.delta;
var newLoc = newValues.newLocation;
delta = newValues.delta;
newLoc = newValues.newLocation;
}
this.draggingBlock_.moveOffDragSurface(newLoc);

View File

@@ -95,7 +95,7 @@ const registerMutator = function(name, mixinObj, opt_helperFn, opt_blockList) {
const errorPrefix = 'Error when registering mutator "' + name + '": ';
checkHasMutatorProperties(errorPrefix, mixinObj);
var hasMutatorDialog = checkMutatorDialog(mixinObj, errorPrefix);
const hasMutatorDialog = checkMutatorDialog(mixinObj, errorPrefix);
if (opt_helperFn && (typeof opt_helperFn !== 'function')) {
throw Error(errorPrefix + 'Extension "' + name + '" is not a function');
@@ -261,8 +261,8 @@ const checkMutatorDialog = function(object, errorPrefix) {
* not actually a function.
*/
const checkHasFunctionPair = function(object, name1, name2, errorPrefix) {
var has1 = object[name1] !== undefined;
var has2 = object[name2] !== undefined;
const has1 = object[name1] !== undefined;
const has2 = object[name2] !== undefined;
if (has1 && has2) {
if (typeof object[name1] !== 'function') {
@@ -285,8 +285,8 @@ const checkHasFunctionPair = function(object, name1, name2, errorPrefix) {
* @param {!Object} object The object to inspect.
*/
const checkHasMutatorProperties = function(errorPrefix, object) {
var hasXmlHooks = checkXmlHooks(object, errorPrefix);
var hasJsonHooks = checkJsonHooks(object, errorPrefix);
const hasXmlHooks = checkXmlHooks(object, errorPrefix);
const hasJsonHooks = checkJsonHooks(object, errorPrefix);
if (!hasXmlHooks && !hasJsonHooks) {
throw Error(
errorPrefix +

View File

@@ -246,7 +246,7 @@ FieldMultilineInput.prototype.render_ = function() {
}
if (this.isBeingEdited_) {
var htmlInput = /** @type {!HTMLElement} */ (this.htmlInput_);
const htmlInput = /** @type {!HTMLElement} */ (this.htmlInput_);
if (this.isOverflowedY_) {
dom.addClass(htmlInput, 'blocklyHtmlTextAreaInputOverflowedY');
} else {
@@ -265,7 +265,7 @@ FieldMultilineInput.prototype.render_ = function() {
} else {
this.resizeEditor_();
}
var htmlInput = /** @type {!HTMLElement} */ (this.htmlInput_);
const htmlInput = /** @type {!HTMLElement} */ (this.htmlInput_);
if (!this.isTextValid_) {
dom.addClass(htmlInput, 'blocklyInvalidInput');
aria.setState(htmlInput, aria.State.INVALID, true);
@@ -284,7 +284,7 @@ FieldMultilineInput.prototype.updateSize_ = function() {
const nodes = this.textGroup_.childNodes;
let totalWidth = 0;
let totalHeight = 0;
for (var i = 0; i < nodes.length; i++) {
for (let i = 0; i < nodes.length; i++) {
const tspan = /** @type {!Element} */ (nodes[i]);
const textWidth = dom.getTextWidth(tspan);
if (textWidth > totalWidth) {
@@ -306,7 +306,7 @@ FieldMultilineInput.prototype.updateSize_ = function() {
const fontWeight = this.getConstants().FIELD_TEXT_FONTWEIGHT;
const fontFamily = this.getConstants().FIELD_TEXT_FONTFAMILY;
for (var i = 0; i < actualEditorLines.length; i++) {
for (let i = 0; i < actualEditorLines.length; i++) {
if (actualEditorLines[i].length > this.maxDisplayLength) {
actualEditorLines[i] =
actualEditorLines[i].substring(0, this.maxDisplayLength);

View File

@@ -432,7 +432,7 @@ ASTNode.prototype.findPrevForField_ = function() {
* @private
*/
ASTNode.prototype.navigateBetweenStacks_ = function(forward) {
var curLocation = this.getLocation();
let curLocation = this.getLocation();
if (curLocation.getSourceBlock) {
curLocation = /** @type {!IASTNodeLocationWithBlock} */ (curLocation)
.getSourceBlock();

View File

@@ -45,7 +45,7 @@ const {inputTypes} = goog.require('Blockly.inputTypes');
* }}
* @alias Blockly.serialization.blocks.ConnectionState
*/
var ConnectionState;
let ConnectionState;
exports.ConnectionState = ConnectionState;
/**
@@ -67,7 +67,7 @@ exports.ConnectionState = ConnectionState;
* }}
* @alias Blockly.serialization.blocks.State
*/
var State;
let State;
exports.State = State;
/**

View File

@@ -35,7 +35,7 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
* }}
* @alias Blockly.serialization.variables.State
*/
var State;
let State;
exports.State = State;
/**

View File

@@ -722,7 +722,7 @@ Trashcan.prototype.cleanBlockJson_ = function(json) {
}
const inputs = json['inputs'];
for (var name in inputs) {
for (const name in inputs) {
const input = inputs[name];
cleanRec(input['block']);
cleanRec(input['shadow']);

View File

@@ -413,8 +413,11 @@ const measureFontMetrics = function(text, fontSize, fontWeight, fontFamily) {
div.appendChild(block);
document.body.appendChild(div);
const result = {
height: 0,
baseline: 0,
};
try {
var result = {};
div.style.alignItems = 'baseline';
result.baseline = block.offsetTop - span.offsetTop;
div.style.alignItems = 'flex-end';

View File

@@ -2688,8 +2688,8 @@ WorkspaceSvg.prototype.hideChaff = function(opt_onlyClosePopups) {
WidgetDiv.hide();
DropDownDiv.hideWithoutAnimation();
var onlyClosePopups = !!opt_onlyClosePopups;
var autoHideables = this.getComponentManager().getComponents(
const onlyClosePopups = !!opt_onlyClosePopups;
const autoHideables = this.getComponentManager().getComponents(
ComponentManager.Capability.AUTOHIDEABLE, true);
autoHideables.forEach(
(autoHideable) => autoHideable.autoHide(onlyClosePopups));

View File

@@ -8,10 +8,10 @@
* @fileoverview Mocha tests that test Blockly in Node.
*/
var assert = require('chai').assert;
var Blockly = require('../../dist/');
const assert = require('chai').assert;
const Blockly = require('../../dist/');
var xmlText = '<xml xmlns="https://developers.google.com/blockly/xml">\n' +
const xmlText = '<xml xmlns="https://developers.google.com/blockly/xml">\n' +
' <block type="text_print" x="37" y="63">\n' +
' <value name="TEXT">\n' +
' <shadow type="text">\n' +
@@ -23,33 +23,33 @@ var xmlText = '<xml xmlns="https://developers.google.com/blockly/xml">\n' +
suite('Test Node.js', function() {
test('Import XML', function() {
var xml = Blockly.Xml.textToDom(xmlText);
const xml = Blockly.Xml.textToDom(xmlText);
// Create workspace and import the XML
var workspace = new Blockly.Workspace();
const workspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(xml, workspace);
});
test('Roundtrip XML', function() {
var xml = Blockly.Xml.textToDom(xmlText);
const xml = Blockly.Xml.textToDom(xmlText);
var workspace = new Blockly.Workspace();
const workspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(xml, workspace);
var headlessXml = Blockly.Xml.workspaceToDom(workspace, true);
var headlessText = Blockly.Xml.domToPrettyText(headlessXml);
const headlessXml = Blockly.Xml.workspaceToDom(workspace, true);
const headlessText = Blockly.Xml.domToPrettyText(headlessXml);
assert.equal(headlessText, xmlText, 'equal');
});
test('Generate Code', function() {
var xml = Blockly.Xml.textToDom(xmlText);
const xml = Blockly.Xml.textToDom(xmlText);
// Create workspace and import the XML
var workspace = new Blockly.Workspace();
const workspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(xml, workspace);
// Convert code
var code = Blockly.JavaScript.workspaceToCode(workspace);
const code = Blockly.JavaScript.workspaceToCode(workspace);
// Check output
assert.equal('window.alert(\'Hello from Blockly!\');', code.trim(), 'equal');
});

View File

@@ -17,18 +17,18 @@
* @param {!Function} callback Callback.
*/
function svgToPng_(data, width, height, callback) {
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
var img = new Image();
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const img = new Image();
var pixelDensity = 10;
const pixelDensity = 10;
canvas.width = width * pixelDensity;
canvas.height = height * pixelDensity;
img.onload = function() {
context.drawImage(
img, 0, 0, width, height, 0, 0, canvas.width, canvas.height);
try {
var dataUri = canvas.toDataURL('image/png');
const dataUri = canvas.toDataURL('image/png');
callback(dataUri);
} catch (err) {
console.warn('Error converting the workspace svg to a png');
@@ -47,22 +47,22 @@ function svgToPng_(data, width, height, callback) {
function workspaceToSvg_(workspace, callback, customCss) {
// Go through all text areas and set their value.
var textAreas = document.getElementsByTagName("textarea");
for (var i = 0; i < textAreas.length; i++) {
const textAreas = document.getElementsByTagName("textarea");
for (let i = 0; i < textAreas.length; i++) {
textAreas[i].innerHTML = textAreas[i].value;
}
var bBox = workspace.getBlocksBoundingBox();
var x = bBox.x || bBox.left;
var y = bBox.y || bBox.top;
var width = bBox.width || bBox.right - x;
var height = bBox.height || bBox.bottom - y;
const bBox = workspace.getBlocksBoundingBox();
const x = bBox.x || bBox.left;
const y = bBox.y || bBox.top;
const width = bBox.width || bBox.right - x;
const height = bBox.height || bBox.bottom - y;
var blockCanvas = workspace.getCanvas();
var clone = blockCanvas.cloneNode(true);
const blockCanvas = workspace.getCanvas();
const clone = blockCanvas.cloneNode(true);
clone.removeAttribute('transform');
var svg = document.createElementNS('http://www.w3.org/2000/svg','svg');
const svg = document.createElementNS('http://www.w3.org/2000/svg','svg');
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
svg.appendChild(clone);
svg.setAttribute('viewBox',
@@ -75,17 +75,17 @@ function workspaceToSvg_(workspace, callback, customCss) {
svg.setAttribute('height', height);
svg.setAttribute("style", 'background-color: transparent');
var css = [].slice.call(document.head.querySelectorAll('style'))
const css = [].slice.call(document.head.querySelectorAll('style'))
.filter(function(el) { return /\.blocklySvg/.test(el.innerText) ||
(el.id.indexOf('blockly-') === 0); }).map(function(el) {
return el.innerText; }).join('\n');
var style = document.createElement('style');
const style = document.createElement('style');
style.innerHTML = css + '\n' + customCss;
svg.insertBefore(style, svg.firstChild);
var svgAsXML = (new XMLSerializer).serializeToString(svg);
let svgAsXML = (new XMLSerializer).serializeToString(svg);
svgAsXML = svgAsXML.replace(/&nbsp/g, '&#160');
var data = 'data:image/svg+xml,' + encodeURIComponent(svgAsXML);
const data = 'data:image/svg+xml,' + encodeURIComponent(svgAsXML);
svgToPng_(data, width, height, callback);
}
@@ -96,7 +96,7 @@ function workspaceToSvg_(workspace, callback, customCss) {
*/
Blockly.downloadScreenshot = function(workspace) {
workspaceToSvg_(workspace, function(datauri) {
var a = document.createElement('a');
const a = document.createElement('a');
a.download = 'screenshot.png';
a.target = '_self';
a.href = datauri;