From 601b97708d5cfd637e02e39103cc941e7b10cfd5 Mon Sep 17 00:00:00 2001 From: Wes Souza Date: Sun, 24 Dec 2023 14:19:38 -0500 Subject: [PATCH] Enforce curly braces with ESLint Enabled https://eslint.org/docs/latest/rules/curly as error --- .config/.eslintrc | 3 +- packages/lgtv-ip-control-cli/src/cli.ts | 17 ++- packages/lgtv-ip-control/src/classes/LGTV.ts | 19 ++- packages/lgtv-ip-control/test/LGTV.test.ts | 125 ++++++++++++++----- 4 files changed, 118 insertions(+), 46 deletions(-) diff --git a/.config/.eslintrc b/.config/.eslintrc index 20f90d1..055a1cd 100644 --- a/.config/.eslintrc +++ b/.config/.eslintrc @@ -15,7 +15,8 @@ { "argsIgnorePattern": "^_\\d*$" } - ] + ], + "curly": "error" }, "ignorePatterns": ["**/dist/**"] } diff --git a/packages/lgtv-ip-control-cli/src/cli.ts b/packages/lgtv-ip-control-cli/src/cli.ts index 7d917b9..7113cc8 100644 --- a/packages/lgtv-ip-control-cli/src/cli.ts +++ b/packages/lgtv-ip-control-cli/src/cli.ts @@ -20,7 +20,9 @@ function createCommand(name: string, description: string) { function positiveNumber(value: string) { const parsed = parseFloat(value); - if (parsed < 0) throw new InvalidArgumentError(`Must be a positive number.`); + if (parsed < 0) { + throw new InvalidArgumentError(`Must be a positive number.`); + } return parsed; } @@ -28,10 +30,11 @@ function positiveNumber(value: string) { function rangeInt(min: number, max: number) { return (value: string) => { const parsed = parseFloat(value); - if (!Number.isSafeInteger(parsed) || parsed < min || parsed > max) + if (!Number.isSafeInteger(parsed) || parsed < min || parsed > max) { throw new InvalidArgumentError( `Must be an integer between ${min} and ${max}.`, ); + } return parsed; }; } @@ -46,7 +49,9 @@ export function makeProgram() { const tv = new LGTV(opts.host, opts.mac ?? null, opts.keycode); return await action(tv, ...args); } catch (err) { - if (err instanceof Error) program.error(err.message); + if (err instanceof Error) { + program.error(err.message); + } throw err; } }; @@ -79,9 +84,11 @@ export function makeProgram() { .action( wrapTVAction(async (tv, level) => { await tv.connect(); - if (level === undefined) + if (level === undefined) { process.stdout.write(String(await tv.getCurrentVolume()) + '\n'); - else await tv.setVolume(level); + } else { + await tv.setVolume(level); + } await tv.disconnect(); }), ); diff --git a/packages/lgtv-ip-control/src/classes/LGTV.ts b/packages/lgtv-ip-control/src/classes/LGTV.ts index 53d304f..b884f49 100644 --- a/packages/lgtv-ip-control/src/classes/LGTV.ts +++ b/packages/lgtv-ip-control/src/classes/LGTV.ts @@ -15,8 +15,9 @@ import { TinySocket } from './TinySocket.js'; export class ResponseParseError extends Error {} function throwIfNotOK(response: string) { - if (response != 'OK') + if (response != 'OK') { throw new ResponseParseError(`response not 'OK': ${response}`); + } } export class LGTV { @@ -52,23 +53,26 @@ export class LGTV { async getCurrentApp(): Promise { const response = await this.sendCommand('CURRENT_APP'); const match = response.match(/^APP:([\w.]+)$/); - if (!match) + if (!match) { throw new ResponseParseError(`failed to parse response: ${response}`); + } return match[1]; } async getCurrentVolume(): Promise { const response = await this.sendCommand('CURRENT_VOL'); const match = response.match(/^VOL:(\d+)$/); - if (!match) + if (!match) { throw new ResponseParseError(`failed to parse response: ${response}`); + } return parseInt(match[1], 10); } async getIpControlState(): Promise { const response = await this.sendCommand('GET_IPCONTROL_STATE'); - if (response != 'ON') + if (response != 'ON') { throw new ResponseParseError(`failed to parse response: ${response}`); + } return true; } @@ -79,9 +83,12 @@ export class LGTV { async getMuteState(): Promise { const response = await this.sendCommand('MUTE_STATE'); const match = response.match(/^MUTE:(on|off)$/); - if (!match) + if (!match) { throw new ResponseParseError(`failed to parse response: ${response}`); - if (match[1] == 'on') return true; + } + if (match[1] == 'on') { + return true; + } return false; } diff --git a/packages/lgtv-ip-control/test/LGTV.test.ts b/packages/lgtv-ip-control/test/LGTV.test.ts index 29c7683..0ab0720 100644 --- a/packages/lgtv-ip-control/test/LGTV.test.ts +++ b/packages/lgtv-ip-control/test/LGTV.test.ts @@ -121,8 +121,11 @@ describe.each([ await testTV.connect(); const actual = testTV.getCurrentApp(); await expect(mocking).resolves.not.toThrow(); - if (expected === null) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.toBe(expected); + if (expected === null) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.toBe(expected); + } }); it.each([ @@ -135,8 +138,11 @@ describe.each([ await testTV.connect(); const actual = testTV.getCurrentVolume(); await expect(mocking).resolves.not.toThrow(); - if (expected === null) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.toBe(expected); + if (expected === null) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.toBe(expected); + } }); it.each([ @@ -148,8 +154,11 @@ describe.each([ await testTV.connect(); const actual = testTV.getMuteState(); await expect(mocking).resolves.not.toThrow(); - if (expected === null) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.toBe(expected); + if (expected === null) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.toBe(expected); + } }); it.each([ @@ -160,8 +169,11 @@ describe.each([ await testTV.connect(); const actual = testTV.getIpControlState(); await expect(mocking).resolves.not.toThrow(); - if (expected === null) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.toBe(expected); + if (expected === null) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.toBe(expected); + } }); it.each([ @@ -172,8 +184,11 @@ describe.each([ await testTV.connect(); const actual = testTV.powerOff(); await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([ @@ -181,12 +196,19 @@ describe.each([ { mode: 'foobar', error: true }, ])('sets picture mode: $mode', async ({ mode, error }) => { let mocking: Promise | null = null; - if (!error) mocking = mockResponse(`PICTURE_MODE ${mode}`, 'OK'); + if (!error) { + mocking = mockResponse(`PICTURE_MODE ${mode}`, 'OK'); + } await testTV.connect(); const actual = testTV.setPictureMode(mode as PictureModes); - if (!error) await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (!error) { + await expect(mocking).resolves.not.toThrow(); + } + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([ @@ -194,12 +216,19 @@ describe.each([ { key: 'foobar', error: true }, ])('sends key: $key', async ({ key, error }) => { let mocking: Promise | null = null; - if (!error) mocking = mockResponse(`KEY_ACTION ${key}`, 'OK'); + if (!error) { + mocking = mockResponse(`KEY_ACTION ${key}`, 'OK'); + } await testTV.connect(); const actual = testTV.sendKey(key as Keys); - if (!error) await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (!error) { + await expect(mocking).resolves.not.toThrow(); + } + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([ @@ -207,12 +236,19 @@ describe.each([ { mode: 'foobar', error: true }, ])('sets picture mode: $mode', async ({ mode, error }) => { let mocking: Promise | null = null; - if (!error) mocking = mockResponse(`SCREEN_MUTE ${mode}`, 'OK'); + if (!error) { + mocking = mockResponse(`SCREEN_MUTE ${mode}`, 'OK'); + } await testTV.connect(); const actual = testTV.setScreenMute(mode as ScreenMuteModes); - if (!error) await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (!error) { + await expect(mocking).resolves.not.toThrow(); + } + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([ @@ -220,12 +256,19 @@ describe.each([ { level: 'foobar', error: true }, ])('sets energy saving level: $level', async ({ level, error }) => { let mocking: Promise | null = null; - if (!error) mocking = mockResponse(`ENERGY_SAVING ${level}`, 'OK'); + if (!error) { + mocking = mockResponse(`ENERGY_SAVING ${level}`, 'OK'); + } await testTV.connect(); const actual = testTV.setEnergySaving(level as EnergySavingLevels); - if (!error) await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (!error) { + await expect(mocking).resolves.not.toThrow(); + } + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([ @@ -233,12 +276,19 @@ describe.each([ { input: 'foobar', error: true }, ])('sets input: $input', async ({ input, error }) => { let mocking: Promise | null = null; - if (!error) mocking = mockResponse(`INPUT_SELECT ${input}`, 'OK'); + if (!error) { + mocking = mockResponse(`INPUT_SELECT ${input}`, 'OK'); + } await testTV.connect(); const actual = testTV.setInput(input as Inputs); - if (!error) await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (!error) { + await expect(mocking).resolves.not.toThrow(); + } + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([ @@ -250,12 +300,19 @@ describe.each([ { level: 1.5, error: true }, ])('sets volume: $level', async ({ level, error }) => { let mocking: Promise | null = null; - if (!error) mocking = mockResponse(`VOLUME_CONTROL ${level}`, 'OK'); + if (!error) { + mocking = mockResponse(`VOLUME_CONTROL ${level}`, 'OK'); + } await testTV.connect(); const actual = testTV.setVolume(level); - if (!error) await expect(mocking).resolves.not.toThrow(); - if (error) await expect(actual).rejects.toThrow(); - else await expect(actual).resolves.not.toThrow(); + if (!error) { + await expect(mocking).resolves.not.toThrow(); + } + if (error) { + await expect(actual).rejects.toThrow(); + } else { + await expect(actual).resolves.not.toThrow(); + } }); it.each([