Skip to content

Commit 37956c9

Browse files
committed
Refactor ESLint configuration for improved global variable management
- Updated eslint.config.js to structure global variables more effectively, ensuring they are only declared in files that use them. - Added specific global variables for various JavaScript files related to chess game logic, enhancing linting accuracy. - Removed the 'no-redeclare' rule to allow intentional redeclaration of globals across different files.
1 parent 61b7d7c commit 37956c9

1 file changed

Lines changed: 100 additions & 33 deletions

File tree

eslint.config.js

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// ESLint v9 flat config
2+
// Structured to avoid redeclare errors by only adding globals to files that USE them, not DECLARE them
23
import js from '@eslint/js';
34
import globals from 'globals';
45

@@ -7,17 +8,72 @@ export default [
78
{
89
languageOptions: {
910
ecmaVersion: 'latest',
10-
sourceType: 'script', // Vanilla JS files are scripts
11+
sourceType: 'script',
1112
globals: {
1213
...globals.browser,
13-
...globals.node,
14-
// JSDOM/Jest globals (tests)
14+
...globals.node
15+
}
16+
},
17+
rules: {
18+
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
19+
'no-console': 'off'
20+
}
21+
},
22+
// Test files
23+
{
24+
files: ['tests/**/*.js'],
25+
languageOptions: {
26+
globals: {
27+
...globals.jest,
1528
describe: 'readonly',
1629
test: 'readonly',
1730
expect: 'readonly',
18-
beforeEach: 'readonly',
19-
// Cross-file dependencies (used but not declared in some files)
20-
// These are declared in their respective files but used elsewhere
31+
beforeEach: 'readonly'
32+
}
33+
}
34+
},
35+
// config.js - declares CONFIG, uses nothing
36+
{
37+
files: ['js/config.js'],
38+
languageOptions: {
39+
globals: {}
40+
}
41+
},
42+
// utils.js - declares utilities, uses CONFIG
43+
{
44+
files: ['js/utils.js'],
45+
languageOptions: {
46+
globals: {
47+
CONFIG: 'readonly'
48+
}
49+
}
50+
},
51+
// pieces.js - declares constants/Piece/getInitialPieces, uses isWithinBounds
52+
{
53+
files: ['js/pieces.js'],
54+
languageOptions: {
55+
globals: {
56+
isWithinBounds: 'readonly'
57+
}
58+
}
59+
},
60+
// board.js - declares selectedSquare, possibleMoves, boardElement, and board functions
61+
// Uses: coordsToAlgebraic, moveHistory (from game.js)
62+
{
63+
files: ['js/board.js'],
64+
languageOptions: {
65+
globals: {
66+
coordsToAlgebraic: 'readonly',
67+
moveHistory: 'readonly'
68+
}
69+
}
70+
},
71+
// game.js - declares boardState, currentPlayer, moveHistory, and game functions
72+
// Uses: everything from other files, but doesn't redeclare them
73+
{
74+
files: ['js/game.js'],
75+
languageOptions: {
76+
globals: {
2177
CONFIG: 'readonly',
2278
debugLog: 'readonly',
2379
PAWN: 'readonly',
@@ -31,9 +87,7 @@ export default [
3187
isWithinBounds: 'readonly',
3288
isPseudoLegalMove: 'readonly',
3389
coordsToAlgebraic: 'readonly',
34-
algebraicToCoords: 'readonly',
3590
deepClone: 'readonly',
36-
getOppositeColor: 'readonly',
3791
playSound: 'readonly',
3892
Piece: 'readonly',
3993
getInitialPieces: 'readonly',
@@ -46,41 +100,54 @@ export default [
46100
updateBoardVisuals: 'readonly',
47101
createBoard: 'readonly',
48102
boardElement: 'readonly',
103+
selectedSquare: 'writable',
104+
possibleMoves: 'writable'
105+
}
106+
}
107+
},
108+
// main.js - uses CONFIG, debugLog, initializeGame, initDragAndDrop, initPromotionUI, ChessAI
109+
{
110+
files: ['js/main.js'],
111+
languageOptions: {
112+
globals: {
113+
CONFIG: 'readonly',
114+
debugLog: 'readonly',
49115
initializeGame: 'readonly',
50-
handleBoardClick: 'readonly',
51-
makeMove: 'readonly',
52-
resetGame: 'readonly',
53-
generateLegalMovesForPiece: 'readonly',
54-
generateAllLegalMoves: 'readonly',
55-
undoLastMove: 'readonly',
56-
findKingInCheck: 'readonly',
57-
isKingInCheck: 'readonly',
58116
initDragAndDrop: 'readonly',
59117
initPromotionUI: 'readonly',
60118
ChessAI: 'readonly'
61119
}
62-
},
63-
rules: {
64-
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
65-
'no-console': 'off',
66-
'no-redeclare': 'off' // Allow redeclaring globals since we use global scope intentionally
67120
}
68121
},
122+
// drag.js - uses CONFIG, debugLog, handleBoardClick
69123
{
70-
files: ['js/**/*.js'],
124+
files: ['js/drag.js'],
71125
languageOptions: {
72126
globals: {
73-
// State variables that are writable
74-
selectedSquare: 'writable',
75-
possibleMoves: 'writable',
76-
boardState: 'writable',
77-
currentPlayer: 'writable',
78-
moveHistory: 'writable',
79-
isGameOver: 'writable',
80-
kingPositions: 'writable',
81-
currentStatus: 'writable',
82-
enPassantTarget: 'writable',
83-
positionHistory: 'writable'
127+
CONFIG: 'readonly',
128+
debugLog: 'readonly',
129+
handleBoardClick: 'readonly'
130+
}
131+
}
132+
},
133+
// promotion.js - uses CONFIG, debugLog
134+
{
135+
files: ['js/promotion.js'],
136+
languageOptions: {
137+
globals: {
138+
CONFIG: 'readonly',
139+
debugLog: 'readonly'
140+
}
141+
}
142+
},
143+
// ai.js - uses CONFIG, debugLog, deepClone
144+
{
145+
files: ['js/ai.js'],
146+
languageOptions: {
147+
globals: {
148+
CONFIG: 'readonly',
149+
debugLog: 'readonly',
150+
deepClone: 'readonly'
84151
}
85152
}
86153
},

0 commit comments

Comments
 (0)