Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-chong committed Nov 11, 2023
1 parent 4c5857c commit 3592e58
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
56 changes: 36 additions & 20 deletions src/components/HandImporter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<div class="mt-1 w-[52rem]">
<textarea
v-model="importText"
:class='"block w-full h-[36rem] px-2 py-1 rounded-lg textarea text-sm font-mono " + (importTextError ? "textarea-error" : "")'
:class="
'block w-full h-[36rem] px-2 py-1 rounded-lg textarea text-sm font-mono ' +
(importTextError ? 'textarea-error' : '')
"
@change="onImportTextChanged"
spellcheck="false"
/>
Expand All @@ -13,7 +16,7 @@
</button>
<span v-if="importDoneText" class="mt-1">{{ importDoneText }}</span>
<span v-if="importTextError" class="mt-1 text-red-500">
{{ 'Error: ' + importTextError }}
{{ "Error: " + importTextError }}
</span>
</div>

Expand All @@ -26,11 +29,18 @@
<script setup lang="ts">
import { ref, watch } from "vue";
import { Config, configKeys, useConfigStore, useStore } from "../store";
import { cardText, parseCardString, Position, Result } from "../utils";
import {
cardText,
getExpectedBoardLength,
parseCardString,
Position,
Result,
} from "../utils";
import { setRange, validateRange } from "../range-utils";
import * as invokes from "../invokes";
type NonValidatedHandJSON = Record<string, any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type NonValidatedHandJSON = any;
type HandJSON = {
oopRange: string;
ipRange: string;
Expand All @@ -39,7 +49,9 @@ type HandJSON = {
const INVALID_BOARD_ERROR = `Invalid '.config.board'. Set board cards manually for an example.`;
const CONFIG_INPUT_KEYS = configKeys.filter(k => ['expectedBoardLength'].indexOf(k) === -1);
const CONFIG_INPUT_KEYS = configKeys.filter(
(k) => ["expectedBoardLength"].indexOf(k) === -1
);
const config = useConfigStore();
const store = useStore();
Expand Down Expand Up @@ -79,25 +91,26 @@ const generateImportText = async () => {
};
importText.value = JSON.stringify(importObj, null, 2);
importTextError.value = '';
importDoneText.value = '';
importTextError.value = "";
importDoneText.value = "";
};
const onImportTextChanged = () => {
importDoneText.value = "";
validateImportTextAndDisplayError();
};
const validateConfigPrimitives = (importConfig: any): Result => {
if (typeof importConfig !== "object")
const validateConfigPrimitives = (importConfig: unknown): Result => {
if (typeof importConfig !== "object" || !importConfig)
return {
success: false,
error: `Expected '.config' to be an object but got ${typeof importConfig}`,
};
for (const key of CONFIG_INPUT_KEYS.concat(Object.keys(importConfig))) {
const newValue = importConfig[key];
const existingValue = (config as any)[key];
const newValue = (importConfig as Record<string, unknown>)[key];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const existingValue = (config as unknown as Record<string, unknown>)[key];
if (existingValue === undefined) {
return { success: false, error: `Unexpected key '.config.${key}'` };
Expand All @@ -116,7 +129,7 @@ const validateConfigPrimitives = (importConfig: any): Result => {
return { success: true };
};
const validateBoard = (board: any): Result => {
const validateBoard = (board: unknown): Result => {
if (!Array.isArray(board))
return { success: false, error: INVALID_BOARD_ERROR };
Expand All @@ -133,15 +146,13 @@ const validateBoard = (board: any): Result => {
}
if (board.length > 5) {
return { success: false, error: 'Board cannot have more than 5 cards' };
return { success: false, error: "Board cannot have more than 5 cards" };
}
return { success: true };
};
const parseJson = (
json: string
): Result<{ json?: NonValidatedHandJSON }> => {
const parseJson = (json: string): Result<{ json?: NonValidatedHandJSON }> => {
try {
return { success: true, json: JSON.parse(json) };
} catch (e) {
Expand Down Expand Up @@ -181,8 +192,13 @@ const validateImportText = (): Result<{ json?: HandJSON }> => {
if (!validation.success) return validation;
}
importJson.config.board = importJson.config.board.map(parseCardString);
importJson.config.expectedBoardLength = importJson.config.board.length;
const cfg = importJson.config;
cfg.board = cfg.board.map(parseCardString);
cfg.expectedBoardLength = getExpectedBoardLength(
cfg.board.length,
cfg.addedLines,
cfg.removedLines
);
return { success: true, json: importJson as HandJSON };
};
Expand All @@ -193,8 +209,8 @@ const importHand = async () => {
const importJson = validation.json as HandJSON;
for (const key in importJson.config) {
const newValue = (importJson.config as Record<string, any>)[key];
(config as any)[key] = newValue;
const newValue = (importJson.config as Record<string, unknown>)[key];
(config as unknown as Record<string, unknown>)[key] = newValue;
}
await setRange(Position.OOP, importJson.oopRange, store);
Expand Down
9 changes: 6 additions & 3 deletions src/components/TreeConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ import {
ROOT_LINE_STRING,
INVALID_LINE_STRING,
readableLineString,
getExpectedBoardLength,
} from "../utils";
import DbItemPicker from "./DbItemPicker.vue";
Expand Down Expand Up @@ -1067,9 +1068,11 @@ const saveEdit = (addedLines: string, removedLines: string) => {
isEditMode.value = false;
config.addedLines = addedLines;
config.removedLines = removedLines;
if (config.addedLines === "" && config.removedLines === "") {
config.expectedBoardLength = 0;
}
config.expectedBoardLength = getExpectedBoardLength(
config.board.length,
addedLines,
removedLines
);
store.headers["tree-config"].pop();
};
Expand Down
2 changes: 1 addition & 1 deletion src/range-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const trimRange = (rangeString: string) =>
rangeString.replace(trimRegex, "$1").trim();

export const validateRange = (
rangeString: any,
rangeString: unknown | string,
keyForError = "range"
): Result => {
if (typeof rangeString !== "string")
Expand Down
11 changes: 10 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Result<Contents = {}> =
export type Result<Contents = object> =
| ({ success: true } & Contents)
| { success: false; error: string };

Expand Down Expand Up @@ -346,3 +346,12 @@ export const readableLineString = (s: string): string => {

return ret;
};

export const getExpectedBoardLength = (
boardLength: number,
addedLines: string,
removedLines: string
) => {
if (addedLines === "" && removedLines === "") return 0;
return Math.max(boardLength, 3);
};

0 comments on commit 3592e58

Please sign in to comment.