From a4d634d84d1fdf572511d39092bddc526ebb242b Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Sat, 26 Aug 2017 13:23:14 -0400 Subject: [PATCH 1/3] Update lint dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ec4cf86f6..10535ef70 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,8 @@ "babel-loader": "^7.0.0", "babel-preset-es2015": "^6.24.1", "copy-webpack-plugin": "4.0.1", - "eslint": "^3.16.0", - "eslint-config-scratch": "^3.1.0", + "eslint": "^4.5.0", + "eslint-config-scratch": "^4.0.0", "expose-loader": "0.7.3", "gh-pages": "^0.12.0", "got": "5.7.1", From 51138765882a5dd080f6a122b4977742589b6b39 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Sat, 26 Aug 2017 13:07:47 -0400 Subject: [PATCH 2/3] Pass with --fix --- src/blocks/scratch3_looks.js | 2 +- src/blocks/scratch3_pen.js | 2 +- src/blocks/scratch3_sound.js | 8 +-- src/engine/adapter.js | 96 +++++++++++++++++------------------ src/engine/blocks.js | 7 +-- src/engine/execute.js | 4 +- src/engine/list.js | 2 +- src/engine/sequencer.js | 6 +-- src/playground/playground.js | 8 +-- src/serialization/sb3.js | 4 +- src/util/cast.js | 2 +- src/util/string-util.js | 4 +- test/unit/blocks_operators.js | 4 +- test/unit/util_timer.js | 2 +- 14 files changed, 74 insertions(+), 77 deletions(-) diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index 0caa04ff6..ca9dac2bd 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -87,7 +87,7 @@ class Scratch3LooksBlocks { * @return {Array.} Any threads started by this switch. */ _setCostumeOrBackdrop (target, - requestedCostume, optZeroIndex) { + requestedCostume, optZeroIndex) { if (typeof requestedCostume === 'number') { target.setCostume(optZeroIndex ? requestedCostume : requestedCostume - 1); diff --git a/src/blocks/scratch3_pen.js b/src/blocks/scratch3_pen.js index 13c0ed42b..a55074040 100644 --- a/src/blocks/scratch3_pen.js +++ b/src/blocks/scratch3_pen.js @@ -295,7 +295,7 @@ class Scratch3PenBlocks { penState.penAttributes.color4f[0] = rgb.r / 255.0; penState.penAttributes.color4f[1] = rgb.g / 255.0; penState.penAttributes.color4f[2] = rgb.b / 255.0; - if (rgb.hasOwnProperty('a')) { // Will there always be an 'a'? + if (rgb.hasOwnProperty('a')) { // Will there always be an 'a'? penState.penAttributes.color4f[3] = rgb.a / 255.0; } else { penState.penAttributes.color4f[3] = 1; diff --git a/src/blocks/scratch3_sound.js b/src/blocks/scratch3_sound.js index 6bd19ede2..1f7745742 100644 --- a/src/blocks/scratch3_sound.js +++ b/src/blocks/scratch3_sound.js @@ -51,20 +51,20 @@ class Scratch3SoundBlocks { return {min: 0, max: 100}; } - /** The minimum and maximum tempo values, in bpm. + /** The minimum and maximum tempo values, in bpm. * @type {{min: number, max: number}} */ static get TEMPO_RANGE () { return {min: 20, max: 500}; } - /** The minimum and maximum values for each sound effect. + /** The minimum and maximum values for each sound effect. * @type {{effect:{min: number, max: number}}} */ static get EFFECT_RANGE () { return { - pitch: {min: -600, max: 600}, // -5 to 5 octaves - pan: {min: -100, max: 100} // 100% left to 100% right + pitch: {min: -600, max: 600}, // -5 to 5 octaves + pan: {min: -100, max: 100} // 100% left to 100% right }; } diff --git a/src/engine/adapter.js b/src/engine/adapter.js index e9c7c3243..ffb8d043e 100644 --- a/src/engine/adapter.js +++ b/src/engine/adapter.js @@ -57,61 +57,61 @@ const domToBlock = function (blockDOM, blocks, isTopBlock, parent) { // as we won't be using all of them for Scratch. switch (xmlChild.name.toLowerCase()) { case 'field': - { - // Add the field to this block. - const fieldName = xmlChild.attribs.name; - // Add id in case it is a variable field - const fieldId = xmlChild.attribs.id; - let fieldData = ''; - if (xmlChild.children.length > 0 && xmlChild.children[0].data) { - fieldData = xmlChild.children[0].data; - } else { - // If the child of the field with a data property - // doesn't exist, set the data to an empty string. - fieldData = ''; - } - block.fields[fieldName] = { - name: fieldName, - id: fieldId, - value: fieldData - }; - break; + { + // Add the field to this block. + const fieldName = xmlChild.attribs.name; + // Add id in case it is a variable field + const fieldId = xmlChild.attribs.id; + let fieldData = ''; + if (xmlChild.children.length > 0 && xmlChild.children[0].data) { + fieldData = xmlChild.children[0].data; + } else { + // If the child of the field with a data property + // doesn't exist, set the data to an empty string. + fieldData = ''; } + block.fields[fieldName] = { + name: fieldName, + id: fieldId, + value: fieldData + }; + break; + } case 'value': case 'statement': - { - // Recursively generate block structure for input block. - domToBlock(childBlockNode, blocks, false, block.id); - if (childShadowNode && childBlockNode !== childShadowNode) { - // Also generate the shadow block. - domToBlock(childShadowNode, blocks, false, block.id); - } - // Link this block's input to the child block. - const inputName = xmlChild.attribs.name; - block.inputs[inputName] = { - name: inputName, - block: childBlockNode.attribs.id, - shadow: childShadowNode ? childShadowNode.attribs.id : null - }; - break; + { + // Recursively generate block structure for input block. + domToBlock(childBlockNode, blocks, false, block.id); + if (childShadowNode && childBlockNode !== childShadowNode) { + // Also generate the shadow block. + domToBlock(childShadowNode, blocks, false, block.id); } + // Link this block's input to the child block. + const inputName = xmlChild.attribs.name; + block.inputs[inputName] = { + name: inputName, + block: childBlockNode.attribs.id, + shadow: childShadowNode ? childShadowNode.attribs.id : null + }; + break; + } case 'next': - { - if (!childBlockNode || !childBlockNode.attribs) { - // Invalid child block. - continue; - } - // Recursively generate block structure for next block. - domToBlock(childBlockNode, blocks, false, block.id); - // Link next block to this block. - block.next = childBlockNode.attribs.id; - break; + { + if (!childBlockNode || !childBlockNode.attribs) { + // Invalid child block. + continue; } + // Recursively generate block structure for next block. + domToBlock(childBlockNode, blocks, false, block.id); + // Link next block to this block. + block.next = childBlockNode.attribs.id; + break; + } case 'mutation': - { - block.mutation = mutationAdapter(xmlChild); - break; - } + { + block.mutation = mutationAdapter(xmlChild); + break; + } } } }; diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 85aeabfb8..e0dce87b8 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -52,7 +52,7 @@ class Blocks { return this._scripts; } - /** + /** * Get the next block for a particular block * @param {?string} id ID of block to get the next block for * @return {?string} ID of next block in the sequence @@ -458,10 +458,7 @@ class Blocks { `<${tagName} id="${block.id}" type="${block.opcode}" - ${block.topLevel ? - `x="${block.x}" y="${block.y}"` : - '' - } + ${block.topLevel ? `x="${block.x}" y="${block.y}"` : ''} >`; // Add any mutation. Must come before inputs. if (block.mutation) { diff --git a/src/engine/execute.js b/src/engine/execute.js index 1f5a25b35..06a320e9e 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -66,8 +66,8 @@ const execute = function (sequencer, thread) { * or after a promise resolves. * @param {*} resolvedValue Value eventually returned from the primitive. */ - // @todo move this to callback attached to the thread when we have performance - // metrics (dd) + // @todo move this to callback attached to the thread when we have performance + // metrics (dd) const handleReport = function (resolvedValue) { thread.pushReportedValue(resolvedValue); if (isHat) { diff --git a/src/engine/list.js b/src/engine/list.js index bf6e8bc06..0f3ec0f2b 100644 --- a/src/engine/list.js +++ b/src/engine/list.js @@ -3,7 +3,7 @@ * Object representing a Scratch list. */ - /** +/** * @param {!string} name Name of the list. * @param {Array} contents Contents of the list, as an array. * @constructor diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index d5c79eae9..fb2c7060a 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -70,7 +70,7 @@ class Sequencer { this.stepThread(activeThread); activeThread.warpTimer = null; if (activeThread.isKilled) { - i--; // if the thread is removed from the list (killed), do not increase index + i--; // if the thread is removed from the list (killed), do not increase index } } if (activeThread.status === Thread.STATUS_RUNNING) { @@ -158,8 +158,8 @@ class Sequencer { // to be re-executed. return; } - // Don't go to the next block for this level of the stack, - // since loops need to be re-executed. + // Don't go to the next block for this level of the stack, + // since loops need to be re-executed. continue; } else if (stackFrame.waitingReporter) { diff --git a/src/playground/playground.js b/src/playground/playground.js index d9aac1c30..2c1ef3308 100644 --- a/src/playground/playground.js +++ b/src/playground/playground.js @@ -288,10 +288,10 @@ window.onload = function () { vm.setTurboMode(turboOn); }); document.getElementById('compatmode').addEventListener('change', - () => { - const compatibilityMode = document.getElementById('compatmode').checked; - vm.setCompatibilityMode(compatibilityMode); - }); + () => { + const compatibilityMode = document.getElementById('compatmode').checked; + vm.setCompatibilityMode(compatibilityMode); + }); const tabBlockExplorer = document.getElementById('tab-blockexplorer'); const tabThreadExplorer = document.getElementById('tab-threadexplorer'); const tabRenderExplorer = document.getElementById('tab-renderexplorer'); diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index 35267a5e2..f8556b6d9 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -60,7 +60,7 @@ const parseScratchObject = function (object, runtime) { sprite.name = object.name; } if (object.hasOwnProperty('blocks')) { - for (let blockId in object.blocks) { + for (const blockId in object.blocks) { blocks.createBlock(object.blocks[blockId]); } // console.log(blocks); @@ -100,7 +100,7 @@ const parseScratchObject = function (object, runtime) { const target = sprite.createClone(); // Load target properties from JSON. if (object.hasOwnProperty('variables')) { - for (let j in object.variables) { + for (const j in object.variables) { const variable = object.variables[j]; const newVariable = new Variable( variable.id, diff --git a/src/util/cast.js b/src/util/cast.js index c000ee1d9..d859d32ec 100644 --- a/src/util/cast.js +++ b/src/util/cast.js @@ -120,7 +120,7 @@ class Cast { const s2 = String(v2).toLowerCase(); return s1.localeCompare(s2); } - // Compare as numbers. + // Compare as numbers. return n1 - n2; } diff --git a/src/util/string-util.js b/src/util/string-util.js index 48dde0277..b71054a04 100644 --- a/src/util/string-util.js +++ b/src/util/string-util.js @@ -32,9 +32,9 @@ class StringUtil { const index = text.indexOf(separator); if (index >= 0) { return [text.substring(0, index), text.substring(index + 1)]; - } else { - return [text, null]; } + return [text, null]; + } } diff --git a/test/unit/blocks_operators.js b/test/unit/blocks_operators.js index 9f43603f5..d0dcbd551 100644 --- a/test/unit/blocks_operators.js +++ b/test/unit/blocks_operators.js @@ -28,8 +28,8 @@ test('multiply', t => { test('divide', t => { t.strictEqual(blocks.divide({NUM1: '2', NUM2: '2'}), 1); - t.strictEqual(blocks.divide({NUM1: '1', NUM2: '0'}), Infinity); // @todo - t.ok(isNaN(blocks.divide({NUM1: 'foo', NUM2: 'bar'}))); // @todo + t.strictEqual(blocks.divide({NUM1: '1', NUM2: '0'}), Infinity); // @todo + t.ok(isNaN(blocks.divide({NUM1: 'foo', NUM2: 'bar'}))); // @todo t.end(); }); diff --git a/test/unit/util_timer.js b/test/unit/util_timer.js index 5c0f21790..bf6d6b9ac 100644 --- a/test/unit/util_timer.js +++ b/test/unit/util_timer.js @@ -26,7 +26,7 @@ test('time', t => { test('start / timeElapsed', t => { const timer = new Timer(); const delay = 100; - const threshold = 1000 / 60; // 60 hz + const threshold = 1000 / 60; // 60 hz // Start timer timer.start(); From 32bc087bba0258043be96628133fa5325c6dd487 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Sat, 26 Aug 2017 13:14:26 -0400 Subject: [PATCH 3/3] Fix remaining lint issues --- src/blocks/scratch3_motion.js | 32 ++++++++++++++-------------- src/engine/execute.js | 6 ++---- src/engine/sequencer.js | 6 ++---- src/io/mouse.js | 4 ++-- src/util/cast.js | 2 +- test/unit/blocks_control.js | 4 ++-- test/unit/sprites_rendered-target.js | 4 ++-- 7 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/blocks/scratch3_motion.js b/src/blocks/scratch3_motion.js index 21caa4d64..d15ad8abb 100644 --- a/src/blocks/scratch3_motion.js +++ b/src/blocks/scratch3_motion.js @@ -106,22 +106,7 @@ class Scratch3MotionBlocks { } glide (args, util) { - if (!util.stackFrame.timer) { - // First time: save data for future use. - util.stackFrame.timer = new Timer(); - util.stackFrame.timer.start(); - util.stackFrame.duration = Cast.toNumber(args.SECS); - util.stackFrame.startX = util.target.x; - util.stackFrame.startY = util.target.y; - util.stackFrame.endX = Cast.toNumber(args.X); - util.stackFrame.endY = Cast.toNumber(args.Y); - if (util.stackFrame.duration <= 0) { - // Duration too short to glide. - util.target.setXY(util.stackFrame.endX, util.stackFrame.endY); - return; - } - util.yield(); - } else { + if (util.stackFrame.timer) { const timeElapsed = util.stackFrame.timer.timeElapsed(); if (timeElapsed < util.stackFrame.duration * 1000) { // In progress: move to intermediate position. @@ -137,6 +122,21 @@ class Scratch3MotionBlocks { // Finished: move to final position. util.target.setXY(util.stackFrame.endX, util.stackFrame.endY); } + } else { + // First time: save data for future use. + util.stackFrame.timer = new Timer(); + util.stackFrame.timer.start(); + util.stackFrame.duration = Cast.toNumber(args.SECS); + util.stackFrame.startX = util.target.x; + util.stackFrame.startY = util.target.y; + util.stackFrame.endX = Cast.toNumber(args.X); + util.stackFrame.endY = Cast.toNumber(args.Y); + if (util.stackFrame.duration <= 0) { + // Duration too short to glide. + util.target.setXY(util.stackFrame.endX, util.stackFrame.endY); + return; + } + util.yield(); } } diff --git a/src/engine/execute.js b/src/engine/execute.js index 06a320e9e..c74823ec6 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -86,12 +86,10 @@ const execute = function (sequencer, thread) { sequencer.retireThread(thread); } } - } else { + } else if (!resolvedValue) { // Not an edge-activated hat: retire the thread // if predicate was false. - if (!resolvedValue) { - sequencer.retireThread(thread); - } + sequencer.retireThread(thread); } } else { // In a non-hat, report the value visually if necessary if diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index fb2c7060a..2846f011a 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -228,11 +228,9 @@ class Sequencer { const doWarp = definitionBlock.mutation.warp; if (doWarp) { thread.peekStackFrame().warpMode = true; - } else { + } else if (isRecursive) { // In normal-mode threads, yield any time we have a recursive call. - if (isRecursive) { - thread.status = Thread.STATUS_YIELD; - } + thread.status = Thread.STATUS_YIELD; } } } diff --git a/src/io/mouse.js b/src/io/mouse.js index 4c6647172..fb4f299ae 100644 --- a/src/io/mouse.js +++ b/src/io/mouse.js @@ -40,10 +40,10 @@ class Mouse { */ postData (data) { if (data.x) { - this._x = data.x - data.canvasWidth / 2; + this._x = data.x - (data.canvasWidth / 2); } if (data.y) { - this._y = data.y - data.canvasHeight / 2; + this._y = data.y - (data.canvasHeight / 2); } if (typeof data.isDown !== 'undefined') { this._isDown = data.isDown; diff --git a/src/util/cast.js b/src/util/cast.js index d859d32ec..152ff4330 100644 --- a/src/util/cast.js +++ b/src/util/cast.js @@ -95,7 +95,7 @@ class Cast { * @return {boolean} True if the argument is all white spaces or null / empty. */ static isWhiteSpace (val) { - return val === null || typeof val === 'string' && val.trim().length === 0; + return val === null || (typeof val === 'string' && val.trim().length === 0); } /** diff --git a/test/unit/blocks_control.js b/test/unit/blocks_control.js index ddb4d9f17..e2eb383a4 100644 --- a/test/unit/blocks_control.js +++ b/test/unit/blocks_control.js @@ -16,7 +16,7 @@ test('repeat', t => { // Test harness (mocks `util`) let i = 0; const repeat = 10; - var util = { + const util = { stackFrame: Object.create(null), startBranch: function () { i++; @@ -38,7 +38,7 @@ test('repeatUntil', t => { // Test harness (mocks `util`) let i = 0; const repeat = 10; - var util = { + const util = { stackFrame: Object.create(null), startBranch: function () { i++; diff --git a/test/unit/sprites_rendered-target.js b/test/unit/sprites_rendered-target.js index 470afbfd9..1592c357f 100644 --- a/test/unit/sprites_rendered-target.js +++ b/test/unit/sprites_rendered-target.js @@ -75,12 +75,12 @@ test('set and clear effects', t => { const a = new RenderedTarget(s, r); const renderer = new FakeRenderer(); a.renderer = renderer; - for (var effect in a.effects) { + for (const effect in a.effects) { a.setEffect(effect, 1); t.equals(a.effects[effect], 1); } a.clearEffects(); - for (effect in a.effects) { + for (const effect in a.effects) { t.equals(a.effects[effect], 0); } t.end();