fix: Add eslint config and fix warnings

This commit is contained in:
Ashhhleyyy 2022-07-07 10:13:43 +01:00
parent d58a84a7b1
commit d5ef78427c
Signed by: ash
GPG key ID: 83B789081A0878FB
10 changed files with 1330 additions and 41 deletions

34
.eslintignore Normal file
View file

@ -0,0 +1,34 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
# lockfile
pnpm-lock.yaml
# CI
.pnpm-store
.drone.yml
# Config
.eslintrc.js
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

27
.eslintrc.js Normal file
View file

@ -0,0 +1,27 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/jsx-runtime",
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true,
},
"ecmaVersion": "latest",
"sourceType": "module",
},
"plugins": [
"react",
"@typescript-eslint",
],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off",
},
}

View file

@ -7,7 +7,8 @@
"build": "tsc && vite build",
"preview": "vite preview",
"format": "prettier --write .",
"format:check": "prettier --check ."
"format:check": "prettier --check .",
"lint": "eslint ."
},
"dependencies": {
"@fontsource/noto-sans-symbols-2": "^4.5.9",
@ -16,9 +17,14 @@
"react-draggable": "^4.4.5"
},
"devDependencies": {
"@types/node": "^18.0.3",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"@vitejs/plugin-react": "^1.3.0",
"eslint": "^8.19.0",
"eslint-plugin-react": "^7.30.1",
"prettier": "^2.7.1",
"ts-node": "^10.8.2",
"typescript": "^4.6.3",

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,7 @@
import './App.css';
import Board from './components/Board';
import {
LEGAL_MOVE_TEST_FEN,
parseFEN,
PAWN_BUG_TEST_FEN,
POSITION_2,
POSITION_4,
POSITION_5,
START_FEN,
} from './utils/board';

View file

@ -1,13 +1,12 @@
import { FC, Fragment, useCallback, useEffect, useMemo, useState } from 'react';
import { FC, Fragment, useCallback, useMemo, useState } from 'react';
import { encodeFEN, eq, GameState, isChecked } from '../utils/board';
import {
filterMoves,
generateLegalMoves,
generatePseudolegalMoves,
makeMove,
Move,
} from '../utils/move';
import { BoardPiece, getType, otherSide, Side } from '../utils/piece';
import { getType, otherSide } from '../utils/piece';
import styles from './Board.module.css';
import Piece from './Piece';
@ -45,7 +44,7 @@ const Board: FC<Props> = (props) => {
const [enPassantTarget, setEnPassantTarget] = useState(
props.state.enPassantTarget
);
const [halfmoveClock, setHalfmoveClock] = useState(
const [halfmoveClock] = useState(
props.state.halfmoveClock
);
const [fullmoveNumber, setFullmoveNumber] = useState(

View file

@ -5,8 +5,11 @@ import './index.css';
import '@fontsource/noto-sans-symbols-2';
import { runAllPerfts, runPerft } from './test/perft';
/* eslint @typescript-eslint/no-explicit-any: off */
{
(window as any).runPerfts = runAllPerfts;
(window as any).runPerft = runPerft;
}
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>

View file

@ -1,12 +1,8 @@
import {
generateLegalMoves,
generatePseudolegalMoves,
isValidMove,
toAlgebraic,
} from './move';
import {
BoardPiece,
otherSide,
pieceFromChar,
Side,
toAlgebraicChar,
@ -107,6 +103,8 @@ export const POSITION_5 =
export const POSITION_6 =
'r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10';
/* eslint @typescript-eslint/no-explicit-any: off */
{
(window as any).START_FEN = START_FEN;
(window as any).TEST_FEN = TEST_FEN;
(window as any).PAWN_BUG_TEST_FEN = PAWN_BUG_TEST_FEN;
@ -117,6 +115,7 @@ export const POSITION_6 =
(window as any).POSITION_4 = POSITION_4;
(window as any).POSITION_5 = POSITION_5;
(window as any).POSITION_6 = POSITION_6;
}
export interface GameState {
board: Board;
@ -197,16 +196,16 @@ export function encodeFEN(state: GameState): string {
function parseFENPieces(fen: string): BoardPiece[][] {
const board = Array(8)
.fill(0)
.map((_) => Array(8).fill(null));
.map(() => Array(8).fill(null));
let rankIdx = 7;
for (const rank of fen.split('/')) {
let file = 0;
for (const c of rank) {
let skip = parseInt(c);
const skip = parseInt(c);
if (!isNaN(skip)) {
file += skip;
} else {
let piece = pieceFromChar(c);
const piece = pieceFromChar(c);
board[rankIdx][file] = piece;
file++;
}
@ -226,14 +225,15 @@ function encodeFENBoard(board: Board): string {
let emptyCount = 0;
let result = '';
while (file < 8) {
if (row[file] === null) {
const piece = row[file];
if (piece === null) {
emptyCount++;
} else {
if (emptyCount > 0) {
result += emptyCount.toString();
emptyCount = 0;
}
result += toAlgebraicChar(row[file]!);
result += toAlgebraicChar(piece);
}
file++;
}

View file

@ -1,6 +1,5 @@
import {
algebraicCoord,
Board,
coord,
Coordinate,
coordsBetween,
@ -147,7 +146,7 @@ function addIfCapture(
state: GameState,
moves: Move[],
move: Move,
enPassant: boolean = false
enPassant = false
): MoveResult {
if (
move.to.rank < 0 ||
@ -275,7 +274,7 @@ function generateLineMoves(
moves: Move[],
type: PieceType,
directions: Coordinate[],
distanceLimit: number = 8
distanceLimit = 8
) {
const validPiece: Piece = `${state.sideToMove}-${type}`;
@ -411,6 +410,7 @@ export function filterMoves(
}
function dbg<T>(t: T, m?: string): T {
m;
// m ? console.trace(m, t) : console.trace(t);
return t;
}
@ -422,7 +422,8 @@ export function makeMove(
): GameState {
const { board, sideToMove } = state;
const newBoard = board.map((rank) => rank.slice());
const piece = board[move.from.rank][move.from.file]!;
const piece = board[move.from.rank][move.from.file];
if (!piece) throw new Error();
const newCastling = { ...state.castling };
if (getType(piece) === 'king') {
newCastling[sideToMove] = {

View file

@ -5,4 +5,9 @@ import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
base: '',
server: {
hmr: {
clientPort: parseInt(process.env.CLIENT_PORT || '3000'),
}
}
});