From 33a72cbf45b2e825ccdbe482c3c58fef85cd883c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 27 Nov 2022 12:55:19 +0000
Subject: [PATCH] feat: added eslint for code quality

---
 frontends/web/.eslintignore |  4 ++++
 frontends/web/.eslintrc.js  | 11 +++++++++++
 frontends/web/package.json  |  6 +++++-
 frontends/web/ts/gb.ts      | 25 ++++++++++++++++---------
 4 files changed, 36 insertions(+), 10 deletions(-)
 create mode 100644 frontends/web/.eslintignore
 create mode 100644 frontends/web/.eslintrc.js

diff --git a/frontends/web/.eslintignore b/frontends/web/.eslintignore
new file mode 100644
index 00000000..539aabc7
--- /dev/null
+++ b/frontends/web/.eslintignore
@@ -0,0 +1,4 @@
+/lib
+/dist
+**/lib
+**/dist
diff --git a/frontends/web/.eslintrc.js b/frontends/web/.eslintrc.js
new file mode 100644
index 00000000..85507cf6
--- /dev/null
+++ b/frontends/web/.eslintrc.js
@@ -0,0 +1,11 @@
+// eslint-disable-next-line no-undef
+module.exports = {
+    extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
+    parser: "@typescript-eslint/parser",
+    plugins: ["@typescript-eslint"],
+    rules: {
+        "no-constant-condition": ["error", { checkLoops: false }],
+        semi: ["error", "always"]
+    },
+    root: true
+};
diff --git a/frontends/web/package.json b/frontends/web/package.json
index f551130b..094d3b25 100644
--- a/frontends/web/package.json
+++ b/frontends/web/package.json
@@ -10,8 +10,9 @@
     "scripts": {
         "build": "parcel build index.html",
         "dev": "parcel index.html",
+        "eslint": "eslint .",
         "nodemon": "nodemon --exec \"parcel --no-cache index.html\"",
-        "pretty": "prettier --config .prettierrc \"./**/*.{ts,tsx,json}\" --write",
+        "pretty": "prettier --config .prettierrc \"./**/*.{js,ts,tsx,json}\" --write",
         "start": "npm run build",
         "watch": "parcel watch index.html"
     },
@@ -20,7 +21,10 @@
         "@parcel/transformer-typescript-tsc": "^2.8.0",
         "@types/react": "^18.0.25",
         "@types/react-dom": "^18.0.9",
+        "@typescript-eslint/eslint-plugin": "^5.44.0",
+        "@typescript-eslint/parser": "^5.44.0",
         "emukit": "^0.5.0",
+        "eslint": "^8.28.0",
         "nodemon": "^2.0.20",
         "parcel": "^2.8.0",
         "prettier": "^2.7.1",
diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts
index 7872b635..8f1a03b3 100644
--- a/frontends/web/ts/gb.ts
+++ b/frontends/web/ts/gb.ts
@@ -24,6 +24,7 @@ import {
 } from "../lib/boytacean";
 import info from "../package.json";
 
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
 declare const require: any;
 
 const LOGIC_HZ = 4194304;
@@ -79,17 +80,17 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
     private visualFrequency: number = VISUAL_HZ;
     private idleFrequency: number = IDLE_HZ;
 
-    private paused: boolean = false;
-    private nextTickTime: number = 0;
-    private fps: number = 0;
+    private paused = false;
+    private nextTickTime = 0;
+    private fps = 0;
     private frameStart: number = new Date().getTime();
-    private frameCount: number = 0;
-    private paletteIndex: number = 0;
+    private frameCount = 0;
+    private paletteIndex = 0;
     private storeCycles: number = LOGIC_HZ * STORE_RATE;
 
     private romName: string | null = null;
     private romData: Uint8Array | null = null;
-    private romSize: number = 0;
+    private romSize = 0;
     private cartridge: Cartridge | null = null;
 
     async main({ romUrl }: { romUrl?: string }) {
@@ -181,7 +182,7 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
         }
     }
 
-    tick(currentTime: number, pending: number, cycles: number = 70224) {
+    tick(currentTime: number, pending: number, cycles = 70224) {
         // in case the time to draw the next frame has not been
         // reached the flush of the "tick" logic is skipped
         if (currentTime < this.nextTickTime) return pending;
@@ -302,6 +303,12 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
             [romName, romData] = [this.romName, this.romData];
         }
 
+        // in case either the ROM's name or data is not available
+        // throws an error as the boot process is not possible
+        if (!romName || !romData) {
+            throw new Error("Unable to load initial ROM");
+        }
+
         // selects the proper engine for execution
         // and builds a new instance of it
         switch (engine) {
@@ -324,7 +331,7 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
         // a valid state ready to be used
         this.gameBoy.reset();
         this.gameBoy.load_boot_default();
-        const cartridge = this.gameBoy.load_rom_ws(romData!);
+        const cartridge = this.gameBoy.load_rom_ws(romData);
 
         // updates the name of the currently selected engine
         // to the one that has been provided (logic change)
@@ -336,7 +343,7 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
 
         // updates the complete set of global information that
         // is going to be displayed
-        this.setRom(romName!, romData!, cartridge);
+        this.setRom(romName, romData, cartridge);
 
         // in case there's a battery involved tries to load the
         // current RAM from the local storage
-- 
GitLab