Skip to content

Commit

Permalink
Enforce curly braces with ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
WesSouza committed Dec 24, 2023
1 parent f932872 commit 601b977
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .config/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
{
"argsIgnorePattern": "^_\\d*$"
}
]
],
"curly": "error"
},
"ignorePatterns": ["**/dist/**"]
}
17 changes: 12 additions & 5 deletions packages/lgtv-ip-control-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ 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;
}

// Returns a parsing function
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;
};
}
Expand All @@ -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;
}
};
Expand Down Expand Up @@ -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();
}),
);
Expand Down
19 changes: 13 additions & 6 deletions packages/lgtv-ip-control/src/classes/LGTV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,23 +53,26 @@ export class LGTV {
async getCurrentApp(): Promise<Apps | string> {
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<number> {
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<boolean> {
const response = await this.sendCommand('GET_IPCONTROL_STATE');
if (response != 'ON')
if (response != 'ON') {
throw new ResponseParseError(`failed to parse response: ${response}`);
}
return true;
}

Expand All @@ -79,9 +83,12 @@ export class LGTV {
async getMuteState(): Promise<boolean> {
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;
}

Expand Down
125 changes: 91 additions & 34 deletions packages/lgtv-ip-control/test/LGTV.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -172,73 +184,111 @@ 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([
{ mode: PictureModes.cinema, error: false },
{ mode: 'foobar', error: true },
])('sets picture mode: $mode', async ({ mode, error }) => {
let mocking: Promise<void> | 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([
{ key: Keys.number5, error: false },
{ key: 'foobar', error: true },
])('sends key: $key', async ({ key, error }) => {
let mocking: Promise<void> | 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([
{ mode: ScreenMuteModes.screenMuteOn, error: false },
{ mode: 'foobar', error: true },
])('sets picture mode: $mode', async ({ mode, error }) => {
let mocking: Promise<void> | 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([
{ level: EnergySavingLevels.maximum, error: false },
{ level: 'foobar', error: true },
])('sets energy saving level: $level', async ({ level, error }) => {
let mocking: Promise<void> | 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([
{ input: Inputs.hdmi3, error: false },
{ input: 'foobar', error: true },
])('sets input: $input', async ({ input, error }) => {
let mocking: Promise<void> | 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([
Expand All @@ -250,12 +300,19 @@ describe.each([
{ level: 1.5, error: true },
])('sets volume: $level', async ({ level, error }) => {
let mocking: Promise<void> | 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([
Expand Down

0 comments on commit 601b977

Please sign in to comment.