Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Fix ESLint googlejs
Browse files Browse the repository at this point in the history
GoogleJS ESlint configuration was actually named "closure" and not
GoogleJS, as Closure is the public name for JSCompiler.

Signed-off-by: Nicolas Paul <[email protected]>
  • Loading branch information
ncs-pl committed Mar 5, 2024
1 parent df2ca3f commit a3cdbea
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 73 deletions.
27 changes: 7 additions & 20 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,28 @@
/* eslint-env node */

// Named constants for the numbers ESLint uses to indicate lint severity.
const OFF = 0;
const WARNING = 1;
const ERROR = 2;
// const OFF = 0;
// const WARNING = 1;
// const ERROR = 2;

// ESLint configuration object. Options are described at
// http://eslint.org/docs/user-guide/configuring.
const ESLINT_CONFIG = {
extends : ['eslint-config-googlejs-es6'],
extends : ['eslint-config-closure-es6'],
parserOptions : {
ecmaVersion : 6,
sourceType : 'module',
},
env : {
'node' : true,
node : true,
},
globals : {},
plugins : [
'googlejs',
'closure',
],
// The list of rules and options are available at
// http://eslint.org/docs/rules/.
rules : {
// Disallow opt_ prefix and var_args as identifiers.
'googlejs/camelcase' : [
ERROR, {
allowVarArgs : false,
// TODO: disallow opt_ prefix once closure compiler stops warning about
// it.
allowOptPrefix : true,
allowLeadingUnderscore : true,
allowTrailingUnderscore : true,
checkObjectProperties : true,
}
],
},
rules : {},
// ESLint supports adding shared settings into configuration file. The
// settings object will be supplied to every rule that will be executed.
settings : {},
Expand Down
34 changes: 16 additions & 18 deletions life2/simulator/src/l2sf.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/**
* @license Copyright 2024 The Life2 Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2024 The Life2 Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {CellStates} from './life2.js';

Expand All @@ -36,7 +34,7 @@ export const CellChar = {
/** A cell belonging to team B. */
TEAM_B: 'b',
/** A barrier cell. */
BARRIER: '#'
BARRIER: '#',
};

/**
Expand All @@ -57,7 +55,7 @@ export function cellStateToCellChar(cell) {
case CellStates.BARRIER:
return CellChar.BARRIER;
default:
throw new TypeError('Unknown cell state: ' + cell);
throw new TypeError(`Unknown cell state: ${cell}`);
}
}

Expand All @@ -79,7 +77,7 @@ export function cellCharToCellState(cell) {
case CellChar.BARRIER:
return CellStates.BARRIER;
default:
throw new TypeError('Unknown cell character: ' + cell);
throw new TypeError(`Unknown cell character: ${cell}`);
}
}

Expand Down
82 changes: 47 additions & 35 deletions life2/simulator/src/life2.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/**
* @license Copyright 2024 The Life2 Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2024 The Life2 Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Life2.
Expand All @@ -34,7 +32,7 @@ export const CellStates = {
/** A cell belonging to team B. */
TEAM_B: 2,
/** A barrier cell. */
BARRIER: 3
BARRIER: 3,
};

/**
Expand Down Expand Up @@ -82,7 +80,7 @@ export class World {
* Initializes an empty board filled with empty cells.
* @param {number} width The width of the board.
* @param {number} height The height of the board.
* @returns {!Array<!Board>} The empty board.
* @return {!Array<!Board>} The empty board.
* @throws {RangeError}
* @final @private
*/
Expand All @@ -96,13 +94,12 @@ export class World {
* Get the cell at a specific position.
* @param {number} x The x position of the cell.
* @param {number} y The y position of the cell.
* @returns {!CellStates} The cell at the position.
* @return {!CellStates} The cell at the position.
* @throws {RangeError}
* @final
*/
getCell(x, y) {
if (this.isOutOfBounds_(x, y))
throw new RangeError('position out of bounds');
if (this.isOutOfBounds_(x, y)) throw RangeError('position out of bounds');

return this.board_[x][y];
}
Expand All @@ -119,18 +116,20 @@ export class World {
* @final
*/
setCell(x, y, cell) {
if (this.isOutOfBounds_(x, y))
if (this.isOutOfBounds_(x, y)) {
throw new RangeError('position out of bounds');
}

if (cell < CellStates.EMPTY || cell > CellStates.BARRIER)
if (cell < CellStates.EMPTY || cell > CellStates.BARRIER) {
throw new TypeError('cell must be a valid Cell');
}

this.board_[x][y] = cell;
}

/**
* Get the board.
* @returns {!Board} The board.
* @return {!Board} The board.
* @final
*/
getBoard() {
Expand All @@ -141,7 +140,7 @@ export class World {
* Check if a position is out of bounds.
* @param {number} x The x position of the cell.
* @param {number} y The y position of the cell.
* @returns {boolean} True if the position is out of bounds, false otherwise.
* @return {boolean} True if the position is out of bounds, false otherwise.
* @final @private
*/
isOutOfBounds_(x, y) {
Expand All @@ -152,28 +151,37 @@ export class World {
* Get the adjacent neighbors of a cell.
* @param {number} x The x position of the cell.
* @param {number} y The y position of the cell.
* @returns {!Array<!CellStates>} The neighbors of the cell.
* @return {!Array<!CellStates>} The neighbors of the cell.
* @throws {RangeError}
* @final @private
*/
getNeighbors_(x, y) {
if (this.isOutOfBounds_(x, y))
if (this.isOutOfBounds_(x, y)) {
throw new RangeError('position out of bounds');
}

return [
(x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x - 1, y), (x + 1, y),
(x - 1, y + 1), (x, y + 1), (x + 1, y + 1)
(x - 1, y - 1),
(x, y - 1),
(x + 1, y - 1),
(x - 1, y),
(x + 1, y),
(x - 1, y + 1),
(x, y + 1),
(x + 1, y + 1),
].filter((x, y) => !this.isOutOfBounds_(x, y))
.map((x, y) => this.getCell(x, y));
}

/**
* Execute the rules of the game to determine the next state of a cell.
* @param {!CellStates} cell The current state of the cell.
* @returns {!CellStates} The next state of the cell.
* @param {number} x The x position of the cell.
* @param {number} y The y position of the cell.
* @return {!CellStates} The next state of the cell.
* @final @private
*/
nextCellState_(cell) {
nextCellState_(cell, x, y) {
// 1. Barrier cells don't change.
// 2. Empty cells become the team with the most neighbors.
// 3. Filled cells with less than 2 neighbors of the same team become empty.
Expand All @@ -189,19 +197,21 @@ export class World {

// (2)
if (cell === CellStates.EMPTY) {
if (neighborsA.length > neighborsB.length) return CellStates.TEAM_A
return CellStates.TEAM_B;
if (neighborsA.length > neighborsB.length) return CellStates.TEAM_A;
return CellStates.TEAM_B;
}

// (3)
if ((cell === CellStates.TEAM_A && neighborsA.length < 2) ||
(cell === CellStates.TEAM_B && neighborsB.length < 2))
(cell === CellStates.TEAM_B && neighborsB.length < 2)) {
return CellStates.EMPTY;
}

// (4)
if ((cell === CellStates.TEAM_A && neighborsA.length <= 3) ||
(cell === CellStates.TEAM_B && neighborsB.length <= 3))
(cell === CellStates.TEAM_B && neighborsB.length <= 3)) {
return cell;
}

// (5)
return CellStates.EMPTY;
Expand All @@ -214,7 +224,9 @@ export class World {
nextState() {
const nextBoard =
this.initializeBoard_(this.width, this.height).forEach((row, x) => {
row = row.map((cell) => {this.nextCellState_(cell)});
row.map((cell, y) => {
this.nextCellState_(cell, x, y);
});
});
this.board_ = nextBoard;
}
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"workspaces": [
"life2/simulator"
],
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"fmt": "clang-format --dry-run -i *.cjs *.js",
"fmt:fix": "clang-format -i *.cjs *.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"eslint": "8.57.0",
"eslint-config-closure-es6": "0.1.1"
Expand Down

0 comments on commit a3cdbea

Please sign in to comment.