mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 05:54:36 +00:00
Enable @typescript eslint/prefer nullish coalescing eslint rule (#1420)
## Description: Fixes #952 Enabled @typescript-eslint/prefer-nullish-coalescing rule and worked through every error, introducing ?? and ??= operators or disabling errors with inline comments where appropriate, to the best of my ability. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors ## Please put your Discord username so you can be contacted if a bug or regression is found: g_santos_m
This commit is contained in:
+1
-1
@@ -119,7 +119,7 @@ export function getMode(list: Set<number>): number {
|
||||
// Count occurrences
|
||||
const counts = new Map<number, number>();
|
||||
for (const item of list) {
|
||||
counts.set(item, (counts.get(item) || 0) + 1);
|
||||
counts.set(item, (counts.get(item) ?? 0) + 1);
|
||||
}
|
||||
|
||||
// Find the item with the highest count
|
||||
|
||||
@@ -97,11 +97,9 @@ export class AttackExecution implements Execution {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.startTroops === null) {
|
||||
this.startTroops = this.mg
|
||||
.config()
|
||||
.attackAmount(this._owner, this.target);
|
||||
}
|
||||
this.startTroops ??= this.mg
|
||||
.config()
|
||||
.attackAmount(this._owner, this.target);
|
||||
if (this.removeTroops) {
|
||||
this.startTroops = Math.min(this._owner.troops(), this.startTroops);
|
||||
this._owner.removeTroops(this.startTroops);
|
||||
|
||||
@@ -19,9 +19,7 @@ export class DonateGoldExecution implements Execution {
|
||||
}
|
||||
|
||||
this.recipient = mg.player(this.recipientID);
|
||||
if (this.gold === null) {
|
||||
this.gold = this.sender.gold() / 3n;
|
||||
}
|
||||
this.gold ??= this.sender.gold() / 3n;
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
|
||||
@@ -19,9 +19,7 @@ export class DonateTroopsExecution implements Execution {
|
||||
}
|
||||
|
||||
this.recipient = mg.player(this.recipientID);
|
||||
if (this.troops === null) {
|
||||
this.troops = mg.config().defaultDonationAmount(this.sender);
|
||||
}
|
||||
this.troops ??= mg.config().defaultDonationAmount(this.sender);
|
||||
const maxDonation =
|
||||
mg.config().maxPopulation(this.recipient) - this.recipient.population();
|
||||
this.troops = Math.min(this.troops, maxDonation);
|
||||
|
||||
@@ -109,9 +109,7 @@ export class SAMLauncherExecution implements Execution {
|
||||
this.player = this.sam.owner();
|
||||
}
|
||||
|
||||
if (this.pseudoRandom === undefined) {
|
||||
this.pseudoRandom = new PseudoRandom(this.sam.id());
|
||||
}
|
||||
this.pseudoRandom ??= new PseudoRandom(this.sam.id());
|
||||
|
||||
const mirvWarheadTargets = this.mg.nearbyUnits(
|
||||
this.sam.tile(),
|
||||
|
||||
@@ -31,13 +31,11 @@ export class SAMMissileExecution implements Execution {
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
if (this.SAMMissile === undefined) {
|
||||
this.SAMMissile = this._owner.buildUnit(
|
||||
UnitType.SAMMissile,
|
||||
this.spawn,
|
||||
{},
|
||||
);
|
||||
}
|
||||
this.SAMMissile ??= this._owner.buildUnit(
|
||||
UnitType.SAMMissile,
|
||||
this.spawn,
|
||||
{},
|
||||
);
|
||||
if (!this.SAMMissile.isActive()) {
|
||||
this.active = false;
|
||||
return;
|
||||
|
||||
@@ -23,9 +23,7 @@ export class ShellExecution implements Execution {
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
if (this.shell === undefined) {
|
||||
this.shell = this._owner.buildUnit(UnitType.Shell, this.spawn, {});
|
||||
}
|
||||
this.shell ??= this._owner.buildUnit(UnitType.Shell, this.spawn, {});
|
||||
if (!this.shell.isActive()) {
|
||||
this.active = false;
|
||||
return;
|
||||
|
||||
@@ -89,11 +89,9 @@ export class TransportShipExecution implements Execution {
|
||||
this.target = mg.player(this.targetID);
|
||||
}
|
||||
|
||||
if (this.troops === null) {
|
||||
this.troops = this.mg
|
||||
.config()
|
||||
.boatAttackAmount(this.attacker, this.target);
|
||||
}
|
||||
this.troops ??= this.mg
|
||||
.config()
|
||||
.boatAttackAmount(this.attacker, this.target);
|
||||
|
||||
this.troops = Math.min(this.troops, this.attacker.troops());
|
||||
|
||||
|
||||
@@ -507,9 +507,7 @@ export class GameView implements GameMap {
|
||||
}
|
||||
|
||||
myPlayer(): PlayerView | null {
|
||||
if (this._myPlayer === null) {
|
||||
this._myPlayer = this.playerByClientID(this._myClientID);
|
||||
}
|
||||
this._myPlayer ??= this.playerByClientID(this._myClientID);
|
||||
return this._myPlayer;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ export class StatsImpl implements Stats {
|
||||
private _addAttack(player: Player, index: number, value: BigIntLike) {
|
||||
const p = this._makePlayerStats(player);
|
||||
if (p === undefined) return;
|
||||
if (p.attacks === undefined) p.attacks = [0n];
|
||||
p.attacks ??= [0n];
|
||||
while (p.attacks.length <= index) p.attacks.push(0n);
|
||||
p.attacks[index] += _bigint(value);
|
||||
}
|
||||
@@ -89,8 +89,8 @@ export class StatsImpl implements Stats {
|
||||
) {
|
||||
const p = this._makePlayerStats(player);
|
||||
if (p === undefined) return;
|
||||
if (p.boats === undefined) p.boats = { [type]: [0n] };
|
||||
if (p.boats[type] === undefined) p.boats[type] = [0n];
|
||||
p.boats ??= { [type]: [0n] };
|
||||
p.boats[type] ??= [0n];
|
||||
while (p.boats[type].length <= index) p.boats[type].push(0n);
|
||||
p.boats[type][index] += _bigint(value);
|
||||
}
|
||||
@@ -104,8 +104,8 @@ export class StatsImpl implements Stats {
|
||||
const type = unitTypeToBombUnit[nukeType];
|
||||
const p = this._makePlayerStats(player);
|
||||
if (p === undefined) return;
|
||||
if (p.bombs === undefined) p.bombs = { [type]: [0n] };
|
||||
if (p.bombs[type] === undefined) p.bombs[type] = [0n];
|
||||
p.bombs ??= { [type]: [0n] };
|
||||
p.bombs[type] ??= [0n];
|
||||
while (p.bombs[type].length <= index) p.bombs[type].push(0n);
|
||||
p.bombs[type][index] += _bigint(value);
|
||||
}
|
||||
@@ -113,7 +113,7 @@ export class StatsImpl implements Stats {
|
||||
private _addGold(player: Player, index: number, value: BigIntLike) {
|
||||
const p = this._makePlayerStats(player);
|
||||
if (p === undefined) return;
|
||||
if (p.gold === undefined) p.gold = [0n];
|
||||
p.gold ??= [0n];
|
||||
while (p.gold.length <= index) p.gold.push(0n);
|
||||
p.gold[index] += _bigint(value);
|
||||
}
|
||||
@@ -127,8 +127,8 @@ export class StatsImpl implements Stats {
|
||||
const type = unitTypeToOtherUnit[otherUnitType];
|
||||
const p = this._makePlayerStats(player);
|
||||
if (p === undefined) return;
|
||||
if (p.units === undefined) p.units = { [type]: [0n] };
|
||||
if (p.units[type] === undefined) p.units[type] = [0n];
|
||||
p.units ??= { [type]: [0n] };
|
||||
p.units[type] ??= [0n];
|
||||
while (p.units[type].length <= index) p.units[type].push(0n);
|
||||
p.units[type][index] += _bigint(value);
|
||||
}
|
||||
|
||||
@@ -26,9 +26,7 @@ class GameMapLoader {
|
||||
private createLazyLoader<T>(importFn: () => Promise<T>): () => Promise<T> {
|
||||
let cache: Promise<T> | null = null;
|
||||
return () => {
|
||||
if (!cache) {
|
||||
cache = importFn();
|
||||
}
|
||||
cache ??= importFn();
|
||||
return cache;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user