Skip to content

Commit

Permalink
[CI] Type check in CI for liberica using eslint (#34)
Browse files Browse the repository at this point in the history
* [CI] Build liberica
* Type check in lint stage
* Fix lints

---------

Co-authored-by: Aaron Geiger <[email protected]>
  • Loading branch information
konsumlamm and Comeza authored May 23, 2024
1 parent 763c94e commit 15a0f0c
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 52 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
- pull_request
- push

env:
CARGO_TERM_COLOR: always

Expand All @@ -14,12 +14,12 @@ jobs:
steps:
- name: Checkout sources
uses: actions/checkout@v4

- uses: actions/cache@v3
with:
path: target
key: ${{ runner.os }}-robusta-mister-x

- name: 🔬 Rust fmt
run: cargo fmt --check --all

Expand All @@ -30,20 +30,20 @@ jobs:
liberica:
name: ⚡ liberica Check
runs-on: ubuntu-latest

defaults:
run:
working-directory: liberica

steps:
- name: Checkout sources
uses: actions/checkout@v4

- uses: actions/cache@v3
with:
path: liberica/node_modules
key: ${{ runner.os }}-liberica-mister-x

- name: 🚧 Setup bun
uses: oven-sh/setup-bun@v1

Expand Down
19 changes: 17 additions & 2 deletions liberica/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@ import eslintConfigPrettier from "eslint-config-prettier"
/** @type {import("eslint").Config} */
export default [
{ ignores: ["src/lib/bindings.ts", "node_modules", "dist"] },
{ languageOptions: { globals: globals.browser } },
{
languageOptions: {
globals: globals.browser,
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname
}
}
},

eslintJs.configs.recommended,

eslintPluginPrettier,
eslintConfigPrettier, // disables some rules that cause conflicts

...tseslint.configs.strict, // strict is a superset of recommended
...tseslint.configs.strictTypeChecked, // strict is a superset of recommended
...tseslint.configs.stylistic,


{
rules: {
"@typescript-eslint/no-confusing-void-expression": "off"
}
}
];
9 changes: 5 additions & 4 deletions liberica/src/components/InputElements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ export const TextInput = ({
placeholder="Lila Pause"
onChange={(e) => {
const fn = {
end: String.prototype.trimEnd,
start: String.prototype.trimStart,
all: String.prototype.trim,
end: (s: string) => s.trimEnd(),
start: (s: string) => s.trimStart(),
all: (s: string) => s.trim(),
};
const value = trim && fn[trim].call(e.target.value);

const value = trim && fn[trim](e.target.value);
onTextChange(value || e.target.value);
}}
{...props}
Expand Down
4 changes: 2 additions & 2 deletions liberica/src/components/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export function Spinner(props: { className?: string; color?: string }) {
export function Spinner(props: { color?: string }) {
const color = props.color ?? "white";
return (
<svg
className={`mr-3 h-5 w-5 animate-spin text-white ${props.className}`}
className="mr-3 h-5 w-5 animate-spin text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
Expand Down
18 changes: 15 additions & 3 deletions liberica/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import axios, { AxiosError } from "axios";
import { Team, Stop, CreateTeam } from "lib/bindings";
import { WebSocketApi } from "./websockets";

Expand Down Expand Up @@ -62,10 +62,10 @@ export const postCreateTeam = (team: CreateTeam): Promise<Team> =>
AXIOS.post(ENDPOINTS.POST_CREATE_TEAM, team);

export const getTeams = (): Promise<Team[]> =>
AXIOS.get(ENDPOINTS.GET_TEAMS).then((data) => data.data);
AXIOS.get(ENDPOINTS.GET_TEAMS).then((data) => data.data as Team[]);

export const getStops = (): Promise<Stop[]> =>
AXIOS.get(ENDPOINTS.GET_STOPS).then((data) => data.data);
AXIOS.get(ENDPOINTS.GET_STOPS).then((data) => data.data as Stop[]);

export const serverAlive = (): Promise<boolean> =>
AXIOS.get(ENDPOINTS.GET_PING)
Expand All @@ -74,3 +74,15 @@ export const serverAlive = (): Promise<boolean> =>

export const createWebSocketConnection = () =>
new WebSocketApi(BASE_URLS.WEBSOCKET + ENDPOINTS.GET_WS);

export const defaultErrorHandler = (err: unknown) => {
if (err instanceof AxiosError) {
if (err.response) console.error("Request Error", err.response);
return;
}

if (err instanceof Error) {
console.error(err.message);
return;
}
};
5 changes: 3 additions & 2 deletions liberica/src/lib/websockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class WebSocketApi {

public reconnect(force = false) {
// Don't try to reconnect if there is a connection already
if (this.connection?.readyState === this.connection.OPEN && force)
if (this.connection.readyState === this.connection.OPEN && force)
return;

this.disconnect();
Expand All @@ -60,6 +60,7 @@ export class WebSocketApi {
this.connection.onclose = (e) => this.metaHandlers["Disconnect"]?.(e);
this.connection.onopen = () => this.metaHandlers["Connect"]?.();
this.connection.onmessage = (e) => {
if (typeof e.data !== "string") return;
const res = this.parseMsg(e.data);
if (res) this.handleMessage(res);
};
Expand Down Expand Up @@ -90,7 +91,7 @@ export class WebSocketApi {
public register<T extends Keys<ServerMessage>>(
type: T,
handler: WSHandlerMap<ServerMessage>[T],
): WebSocketApi {
): this {
this.handlers[type] = handler;
return this;
}
Expand Down
6 changes: 5 additions & 1 deletion liberica/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ i18n.use(LanguageDetector)
en: { translation: en_translation },
de: { translation: de_translation },
},
});
})
.catch(
(e: unknown) =>
e instanceof Error && console.error("i18n init error", e.message),
);

const rootElement = document.getElementById("root") as HTMLElement;
ReactDOM.createRoot(rootElement).render(
Expand Down
13 changes: 4 additions & 9 deletions liberica/src/page/CreateTeam.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, DropDown, TextInput } from "components/InputElements";
import { postCreateTeam } from "lib/api";
import { defaultErrorHandler, postCreateTeam } from "lib/api";
import { TeamKind } from "lib/bindings";
import { FormEvent, useState } from "react";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -28,14 +28,9 @@ export function CreateTeam() {

setLoading(true);
postCreateTeam({ color, name, kind })
.then(() => {
setLoading(false);
navigate("/");
})
.catch((err) => {
setLoading(false);
alert(t(err.response.data));
});
.then(() => navigate("/"))
.catch(defaultErrorHandler)
.finally(() => setLoading(false));
};

return (
Expand Down
23 changes: 11 additions & 12 deletions liberica/src/page/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ export function Game() {
const [ws, setWS] = useState<WebSocketApi>();
const [gs, setGameState] = useState<GameState>({ teams: [], trains: [] });
const [embarkedTrain, setEmbarkedTrain] = useState<Train>();
const team: Team = useLocation().state; // this is how Home passes the team
const team = useLocation().state as Team | undefined; // this is how Home passes the team
const { t } = useTranslation();

function disembark() {
if (team) {
setEmbarkedTrain(undefined);
ws?.send("DisembarkTrain");
}
if (!team) return;

setEmbarkedTrain(undefined);
ws?.send("DisembarkTrain");
}

function embark(train: Train) {
if (team) {
setEmbarkedTrain(train);
ws?.send({ EmbarkTrain: { train_id: train.line_id } });
}
if (!team) return;

setEmbarkedTrain(train);
ws?.send({ EmbarkTrain: { train_id: train.line_id } });
}

useEffect(() => {
Expand All @@ -49,9 +49,8 @@ export function Game() {
}, []);

useEffect(() => {
if (team) {
ws?.send({ JoinTeam: { team_id: team.id } });
}
if (!team) return;
ws?.send({ JoinTeam: { team_id: team.id } });
}, [ws, team]);

useEffect(() => {
Expand Down
5 changes: 3 additions & 2 deletions liberica/src/page/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ export function Home() {
const { t } = useTranslation();

useEffect(() => {
const updateTeams = () => getTeams().then(setTeams);
const updateTeams = () => void getTeams().then(setTeams);

updateTeams();

const interval = setInterval(updateTeams, 500);
return () => clearInterval(interval);
}, []);

const process = async () => {
const process = () => {
if (selected === undefined) {
return;
}
Expand Down
15 changes: 7 additions & 8 deletions liberica/src/page/Replay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,18 @@ export function Replay() {
if (file.size >= max_replay_size) {
alert(t("ReplayTooBig"));
}
file.text().then((data) => {
try {
file.text()
.then((data) => {
const state = parseCSV(data);
if (state.length > 0) {
startReplay(state);
}
} catch (e) {
alert(t("FailedParseReplay"));
})
.catch(() =>
console.error(
`failed to parse replay file: ${e}`,
);
}
});
`failed to parse replay file`,
),
);
}
// invalid replay file
reset();
Expand Down

0 comments on commit 15a0f0c

Please sign in to comment.