From b7a9cc21a90bfaa4467c880ba914ab2956ad098a Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Thu, 2 Jan 2025 22:18:21 +0100 Subject: [PATCH] Fix some sonar complaints --- src/js/msp/MSPHelper.js | 14 ++++--- src/js/protocols/webusbdfu.js | 6 ++- src/js/tabs/gps.js | 5 +-- src/js/tabs/led_strip.js | 68 +++++++++++++++++++++++++--------- src/js/tabs/logging.js | 4 +- src/js/tabs/motors.js | 12 +++--- src/js/tabs/onboard_logging.js | 4 +- src/js/tabs/pid_tuning.js | 7 +++- src/js/tabs/vtx.js | 5 +-- src/js/workers/hex_parser.js | 4 +- 10 files changed, 84 insertions(+), 45 deletions(-) diff --git a/src/js/msp/MSPHelper.js b/src/js/msp/MSPHelper.js index 6719fcc608..cd43b4055a 100644 --- a/src/js/msp/MSPHelper.js +++ b/src/js/msp/MSPHelper.js @@ -199,7 +199,7 @@ MspHelper.prototype.process_data = function (dataHandler) { let flags = 0; if (!crcError) { - if (!dataHandler.unsupported) + if (!dataHandler.unsupported) { switch (code) { case MSPCodes.MSP_STATUS: FC.CONFIG.cycleTime = data.readU16(); @@ -773,13 +773,14 @@ MspHelper.prototype.process_data = function (dataHandler) { FC.CONFIG.apiVersion = `${data.readU8()}.${data.readU8()}.0`; break; - case MSPCodes.MSP_FC_VARIANT: + case MSPCodes.MSP_FC_VARIANT: { let fcVariantIdentifier = ""; for (let i = 0; i < 4; i++) { fcVariantIdentifier += String.fromCharCode(data.readU8()); } FC.CONFIG.flightControllerIdentifier = fcVariantIdentifier; break; + } case MSPCodes.MSP_FC_VERSION: FC.CONFIG.flightControllerVersion = `${data.readU8()}.${data.readU8()}.${data.readU8()}`; @@ -898,7 +899,7 @@ MspHelper.prototype.process_data = function (dataHandler) { console.log("Channel forwarding saved"); break; - case MSPCodes.MSP_CF_SERIAL_CONFIG: + case MSPCodes.MSP_CF_SERIAL_CONFIG: { FC.SERIAL_CONFIG.ports = []; const bytesPerPort = 1 + 2 + 1 * 4; @@ -916,6 +917,7 @@ MspHelper.prototype.process_data = function (dataHandler) { FC.SERIAL_CONFIG.ports.push(serialPort); } break; + } case MSPCodes.MSP2_COMMON_SERIAL_CONFIG: FC.SERIAL_CONFIG.ports = []; @@ -1701,7 +1703,7 @@ MspHelper.prototype.process_data = function (dataHandler) { default: console.log(`Unknown code detected: ${code} (${getMSPCodeName(code)})`); } - else { + } else { console.log(`FC reports unsupported message error: ${code} (${getMSPCodeName(code)})`); if (code === MSPCodes.MSP_SET_REBOOT) { @@ -1726,7 +1728,9 @@ MspHelper.prototype.process_data = function (dataHandler) { dataHandler.callbacks.splice(i, 1); if (!crcError || callbackOnError) { // fire callback - if (callback) callback({ command: code, data: data, length: data.byteLength, crcError: crcError }); + if (callback) { + callback({ command: code, data: data, length: data.byteLength, crcError: crcError }); + } } else { console.warn(`code: ${code} - crc failed. No callback`); } diff --git a/src/js/protocols/webusbdfu.js b/src/js/protocols/webusbdfu.js index 4938d4ebea..c96155185d 100644 --- a/src/js/protocols/webusbdfu.js +++ b/src/js/protocols/webusbdfu.js @@ -439,7 +439,7 @@ class WEBUSBDFU_protocol extends EventTarget { switch (unit) { case "M": - page_size *= 1024; // fall through to K as well + // fall through to K as well case "K": page_size *= 1024; break; @@ -891,7 +891,9 @@ class WEBUSBDFU_protocol extends EventTarget { const idx = erase_pages.findIndex((element, index, array) => { return element.sector === i && element.page === j; }); - if (idx === -1) erase_pages.push({ sector: i, page: j }); + if (idx === -1) { + erase_pages.push({ sector: i, page: j }); + } } } } diff --git a/src/js/tabs/gps.js b/src/js/tabs/gps.js index 2722406153..a48e327be0 100644 --- a/src/js/tabs/gps.js +++ b/src/js/tabs/gps.js @@ -284,11 +284,10 @@ gps.initialize = async function (callback) { const used = i18n.getMessage(usedArray[(FC.GPS_DATA.quality[i] & 0x8) >> 3]); // Add color to the text + const lockedOrLow = quality.startsWith(i18n.getMessage("gnssQualityLocked")) ? "locked" : "low"; const qualityColor = quality.startsWith(i18n.getMessage("gnssQualityFullyLocked")) ? "ready" - : quality.startsWith(i18n.getMessage("gnssQualityLocked")) - ? "locked" - : "low"; + : lockedOrLow; const qualityHtml = `${quality}`; const usedColor = used.startsWith(i18n.getMessage("gnssUsedUsed")) ? "ready" : "low"; diff --git a/src/js/tabs/led_strip.js b/src/js/tabs/led_strip.js index 0fe9aa7957..ad32f9e410 100644 --- a/src/js/tabs/led_strip.js +++ b/src/js/tabs/led_strip.js @@ -232,11 +232,15 @@ led_strip.initialize = function (callback, scrollPosition) { for (let colorIndex = 0; colorIndex < 16; colorIndex++) { colorButtons.removeClass("btnOn"); - if (selectedModeColor == undefined) $(".ui-selected").removeClass(`color-${colorIndex}`); + if (selectedModeColor == undefined) { + $(".ui-selected").removeClass(`color-${colorIndex}`); + } if ($(that).is(`.color-${colorIndex}`)) { selectedColorIndex = colorIndex; - if (selectedModeColor == undefined) $(".ui-selected").addClass(`color-${colorIndex}`); + if (selectedModeColor == undefined) { + $(".ui-selected").addClass(`color-${colorIndex}`); + } } } @@ -480,22 +484,32 @@ led_strip.initialize = function (callback, scrollPosition) { case "y": case "o": case "s": - if (areModifiersActive(`function-${f}`)) p.addClass(`function-${letter}`); + if (areModifiersActive(`function-${f}`)) { + p.addClass(`function-${letter}`); + } break; case "b": case "i": case "p": case "e": case "u": - if (areOverlaysActive(`function-${f}`)) p.addClass(`function-${letter}`); + if (areOverlaysActive(`function-${f}`)) { + p.addClass(`function-${letter}`); + } break; case "w": - if (areOverlaysActive(`function-${f}`)) - if (isWarningActive(`function-${f}`)) p.addClass(`function-${letter}`); + if (areOverlaysActive(`function-${f}`)) { + if (isWarningActive(`function-${f}`)) { + p.addClass(`function-${letter}`); + } + } break; case "v": - if (areOverlaysActive(`function-${f}`)) - if (isVtxActive(`function-${f}`)) p.addClass(`function-${letter}`); + if (areOverlaysActive(`function-${f}`)) { + if (isVtxActive(`function-${f}`)) { + p.addClass(`function-${letter}`); + } + } break; } } @@ -982,7 +996,9 @@ led_strip.initialize = function (callback, scrollPosition) { $(".ui-selected") .find(".wire") .each(function () { - if ($(this).text() != "") $(this).parent().addClass(`function-${letter}`); + if ($(this).text() != "") { + $(this).parent().addClass(`function-${letter}`); + } }); unselectOverlays(letter); @@ -1074,7 +1090,9 @@ led_strip.initialize = function (callback, scrollPosition) { setModeBackgroundColor($(this)); }); - if (change) updateBulkCmd(); + if (change) { + updateBulkCmd(); + } } function drawColorBoxesInColorLedPoints() { @@ -1092,9 +1110,8 @@ led_strip.initialize = function (callback, scrollPosition) { if ($(this).is(`.${className}`)) { $(this).find(".overlay-color").addClass(className); $(this).find(".overlay-color").css("background-color", HsvToColor(FC.LED_COLORS[colorIndex])); - } else { - if ($(this).find(".overlay-color").is(`.${className}`)) - $(this).find(".overlay-color").removeClass(className); + } else if ($(this).find(".overlay-color").is(`.${className}`)) { + $(this).find(".overlay-color").removeClass(className); } } } else { @@ -1107,7 +1124,9 @@ led_strip.initialize = function (callback, scrollPosition) { const sliders = $("div.colorDefineSliders input"); let change = false; - if (!FC.LED_COLORS[colorIndex]) return; + if (!FC.LED_COLORS[colorIndex]) { + return; + } if (FC.LED_COLORS[colorIndex].h != Number(sliders.eq(0).val())) { sliders.eq(0).val(FC.LED_COLORS[colorIndex].h); @@ -1128,22 +1147,35 @@ led_strip.initialize = function (callback, scrollPosition) { } // only fire events when all values are set - if (change) sliders.trigger("input"); + if (change) { + sliders.trigger("input"); + } } function HsvToColor(input) { - if (input == undefined) return ""; + if (input == undefined) { + return ""; + } let HSV = { h: Number(input.h), s: Number(input.s), v: Number(input.v) }; - if (HSV.s == 0 && HSV.v == 0) return ""; + if (HSV.s == 0 && HSV.v == 0) { + return ""; + } HSV = { h: HSV.h, s: 1 - HSV.s / 255, v: HSV.v / 255 }; const HSL = { h: 0, s: 0, v: 0 }; HSL.h = HSV.h; HSL.l = ((2 - HSV.s) * HSV.v) / 2; - HSL.s = HSL.l && HSL.l < 1 ? (HSV.s * HSV.v) / (HSL.l < 0.5 ? HSL.l * 2 : 2 - HSL.l * 2) : HSL.s; + + if (HSL.l && HSL.l < 1) { + if (HSL.l < 0.5) { + HSL.s = (HSV.s * HSV.v) / (HSL.l * 2); + } else { + HSL.s = (HSV.s * HSV.v) / (2 - HSL.l * 2); + } + } return `hsl(${HSL.h},${HSL.s * 100}%,${HSL.l * 100}%)`; } diff --git a/src/js/tabs/logging.js b/src/js/tabs/logging.js index cb4316ae87..c00aa45f57 100644 --- a/src/js/tabs/logging.js +++ b/src/js/tabs/logging.js @@ -77,8 +77,8 @@ logging.initialize = function (callback) { if (logBuffer.length) { // only execute when there is actual data to write append_to_file(logBuffer.join("\n").concat("\n")); - - $(".samples").text((samples += logBuffer.length)); + samples += logBuffer.length; + $(".samples").text(samples); logBuffer = []; } diff --git a/src/js/tabs/motors.js b/src/js/tabs/motors.js index e12bc5ab59..410ab70e35 100644 --- a/src/js/tabs/motors.js +++ b/src/js/tabs/motors.js @@ -1019,13 +1019,11 @@ motors.initialize = async function (callback) { if (FC.MOTOR_DATA[i] > zeroThrottleValue) { motorsRunning = true; } - } else { - if ( - FC.MOTOR_DATA[i] < FC.MOTOR_3D_CONFIG.deadband3d_low || - FC.MOTOR_DATA[i] > FC.MOTOR_3D_CONFIG.deadband3d_high - ) { - motorsRunning = true; - } + } else if ( + FC.MOTOR_DATA[i] < FC.MOTOR_3D_CONFIG.deadband3d_low || + FC.MOTOR_DATA[i] > FC.MOTOR_3D_CONFIG.deadband3d_high + ) { + motorsRunning = true; } } diff --git a/src/js/tabs/onboard_logging.js b/src/js/tabs/onboard_logging.js index a8eabaebb1..7ca6bbea31 100644 --- a/src/js/tabs/onboard_logging.js +++ b/src/js/tabs/onboard_logging.js @@ -58,7 +58,9 @@ onboard_logging.initialize = function (callback) { } function gcd(a, b) { - if (b === 0) return a; + if (b === 0) { + return a; + } return gcd(b, a % b); } diff --git a/src/js/tabs/pid_tuning.js b/src/js/tabs/pid_tuning.js index a7496b5a5e..7a37cbd4f9 100644 --- a/src/js/tabs/pid_tuning.js +++ b/src/js/tabs/pid_tuning.js @@ -2344,8 +2344,11 @@ pid_tuning.updateRatesLabels = function () { // adjust the coordinates for determine where the balloon background should be drawn x += (align == "right" ? -(width + DEFAULT_OFFSET) : 0) + (align == "left" ? DEFAULT_OFFSET : 0); y -= height / 2; - if (y < 0) y = 0; - else if (y > context.height) y = context.height; // prevent balloon from going out of canvas + if (y < 0) { + y = 0; + } else if (y > context.height) { + y = context.height; // prevent balloon from going out of canvas + } // check that the balloon does not already overlap for (let i = 0; i < dirty.length; i++) { diff --git a/src/js/tabs/vtx.js b/src/js/tabs/vtx.js index cf71b564ef..4416062b3b 100644 --- a/src/js/tabs/vtx.js +++ b/src/js/tabs/vtx.js @@ -783,10 +783,7 @@ vtx.initialize = function (callback) { } if (save_vtx_powerlevels.counter < FC.VTX_CONFIG.vtx_table_powerlevels) { - FC.VTXTABLE_POWERLEVEL = Object.assign( - {}, - TABS.vtx.VTXTABLE_POWERLEVEL_LIST[save_vtx_powerlevels.counter], - ); + FC.VTXTABLE_POWERLEVEL = { ...TABS.vtx.VTXTABLE_POWERLEVEL_LIST[save_vtx_powerlevels.counter] }; MSP.send_message( MSPCodes.MSP_SET_VTXTABLE_POWERLEVEL, mspHelper.crunch(MSPCodes.MSP_SET_VTXTABLE_POWERLEVEL), diff --git a/src/js/workers/hex_parser.js b/src/js/workers/hex_parser.js index c94a90269b..50af8dd69d 100644 --- a/src/js/workers/hex_parser.js +++ b/src/js/workers/hex_parser.js @@ -34,7 +34,8 @@ export default async function read_hex_file(data) { const checksum = parseInt(data[i].substr(9 + byte_count * 2, 2), 16); // (this is a 2's complement value) switch (record_type) { - case 0x00: // data record + case 0x00: { + // data record if (address !== next_address || next_address === 0) { result.data.push({ address: extended_linear_address + address, bytes: 0, data: [] }); } @@ -65,6 +66,7 @@ export default async function read_hex_file(data) { hexfile_valid = false; } break; + } case 0x01: // end of file record result.end_of_file = true; break;