Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(eslint): ๐ŸŽจ๋ช…์‹œ์ ์ธ null check, ๊ฐ•ํ™”๋œ eslint #236

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
eqeqeq: ['error', 'always'],
'no-implicit-coercion': ['error', { allow: ['!!'] }],
},
};
7 changes: 4 additions & 3 deletions server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SessionAuthGuard } from './auth/auth.guard';
import User from './entities/user.entity';
import { RoomService } from './room/room.service';
import { isUserSession, UserSession } from './types/userSession';
import { isNil } from '@nestjs/common/utils/shared.utils';

@UseGuards(SessionAuthGuard)
@Controller()
Expand All @@ -32,7 +33,7 @@ export class AppController {
async getSession(@Req() req: Request): Promise<UserSession> {
const user = req.user as User;

if (user && isUserSession(user)) {
if (!isNil(user) && isUserSession(user)) {
const userSession: UserSession = {
provider: user.provider,
providerId: user.providerId,
Expand All @@ -42,12 +43,12 @@ export class AppController {
};

const joinedRooms = await user.joinedRooms;
if (joinedRooms != null && joinedRooms.length > 0) {
if (joinedRooms !== undefined && joinedRooms.length > 0) {
const room = joinedRooms[0].room;
userSession.participatingRoomCode = room.code;
const host = await room.host;

if (host != null && host.id === user.id) {
if (host !== undefined && host.id === user.id) {
userSession.isHost = true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { UserService } from '../user/user.service';
import User from '../entities/user.entity';
import { ProviderInfo } from 'src/types/user';
import { isNil } from '@nestjs/common/utils/shared.utils';

@Injectable()
export class AuthService {
Expand All @@ -17,7 +18,7 @@ export class AuthService {
const user =
await this.userService.findUserByProviderInfo(mockProviderInfo);

if (user == null) {
if (isNil(user)) {
this.logger.debug('์ž˜๋ชป๋œ ๋กœ๊ทธ์ธ ์š”์ฒญ์ž…๋‹ˆ๋‹ค!');
throw new BadRequestException('์ž˜๋ชป๋œ ๋กœ๊ทธ์ธ ์š”์ฒญ์ž…๋‹ˆ๋‹ค!');
}
Expand Down
6 changes: 3 additions & 3 deletions server/src/problem/dto/random.problem.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class RandomProblemDto {
required: true,
minLength: 1,
})
@Transform(({ value }) => value.split(',').map((v: string) => +v))
@Transform(({ value }) => value.split(',').map((v: string) => Number(v)))
@ArrayUnique()
@IsNumber({}, { each: true })
tagIds!: number[];
Expand All @@ -21,7 +21,7 @@ export class RandomProblemDto {
required: true,
minLength: 1,
})
@Transform(({ value }) => value.split(',').map((v: string) => +v))
@Transform(({ value }) => value.split(',').map((v: string) => Number(v)))
@ArrayUnique()
@IsNumber({}, { each: true })
levels!: number[];
Expand All @@ -30,7 +30,7 @@ export class RandomProblemDto {
example: '5',
required: true,
})
@Transform(({ value }) => +value)
@Transform(({ value }) => Number(value))
@IsNumber()
@IsNotEmpty()
@Min(1)
Expand Down
3 changes: 2 additions & 1 deletion server/src/problem/problem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Problem from 'src/entities/problem.entity';
import { ILike, In, Raw, Repository } from 'typeorm';
import { RandomProblemDto } from './dto/random.problem.dto';
import { SearchProblemDto } from './dto/search.problem.dto';
import { isNil } from '@nestjs/common/utils/shared.utils';

@Injectable()
export class ProblemService {
Expand Down Expand Up @@ -57,7 +58,7 @@ export class ProblemService {
const problemEntities: Problem[] = [];
for (const bojProblemId of bojProblemIds) {
const problem = await this.getProblemByBojProblemId(bojProblemId);
if (problem == null) {
if (isNil(problem)) {
throw new Error(`bojProblemId ${bojProblemId} not found`);
}
problemEntities.push(problem);
Expand Down
15 changes: 8 additions & 7 deletions server/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UserService } from 'src/user/user.service';
import { Repository } from 'typeorm';
import RoomUser from '../entities/roomUser.entity';
import { SocketService } from '../socket/socket.service';
import { isNil } from '@nestjs/common/utils/shared.utils';

@Injectable()
export class RoomService {
Expand All @@ -38,7 +39,7 @@ export class RoomService {
throw new BadRequestException('์ด๋ฏธ ์ฐธ๊ฐ€ํ•œ ๋ฐฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค.');
}

if (user.username == null)
if (isNil(user.username))
throw new BadRequestException('username์ด ์—†์Šต๋‹ˆ๋‹ค.');
const code = await this.createRoomCode(user.username);

Expand All @@ -65,7 +66,7 @@ export class RoomService {
async joinRoom(user: User, roomCode: string) {
const joinedRooms = await user.joinedRooms;

if (joinedRooms != null && joinedRooms.length !== 0) {
if (joinedRooms !== undefined && joinedRooms.length > 0) {
throw new BadRequestException('์ด๋ฏธ ์ฐธ๊ฐ€ํ•œ ๋ฐฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค.');
}

Expand All @@ -76,7 +77,7 @@ export class RoomService {
throw new BadRequestException('์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์ž…๋‹ˆ๋‹ค.');
}
const room = roomUsers[0].room;
if (room == null) {
if (isNil(room)) {
throw new InternalServerErrorException('๋ฐฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
}
this.logger.debug(`user ${user.username} joining room ${room.code}...`);
Expand All @@ -91,7 +92,7 @@ export class RoomService {

async exitRoom(user: User) {
const joinedRooms = await user.joinedRooms;
if (joinedRooms == null) {
if (joinedRooms === undefined) {
throw new InternalServerErrorException(
'์ฐธ๊ฐ€ ์ค‘์ธ ๋ฐฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.',
);
Expand All @@ -105,21 +106,21 @@ export class RoomService {
await this.roomUserRepository.remove(roomUser);

const room = roomUser.room;
if (room == null) {
if (isNil(room)) {
throw new InternalServerErrorException('๋ฐฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
}

await this.socketService.notifyExit(user.username, room);

const numberOfJoinedUsers = (await room.joinedUsers)?.length;
if (numberOfJoinedUsers == null || numberOfJoinedUsers === 0) {
if (isNil(numberOfJoinedUsers) || numberOfJoinedUsers === 0) {
await this.destroyRoom(room);
}
}

async findRoomByCode(code: string) {
const room = await this.roomRepository.findOne({ where: { code } });
if (!room) {
if (isNil(room)) {
throw new BadRequestException('์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์ž…๋‹ˆ๋‹ค.');
}
return room;
Expand Down
9 changes: 5 additions & 4 deletions server/src/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { SocketService } from './socket.service';
import { ProblemService } from 'src/problem/problem.service';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { isNil } from '@nestjs/common/utils/shared.utils';

@WebSocketGateway({
cors: {
Expand Down Expand Up @@ -91,7 +92,7 @@ export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect {
provider,
providerId,
});
if (user == null) throw new WsException('user is null');
if (isNil(user)) throw new WsException('user is null');

await this.socketService.gameStart(user, roomInfo);
}
Expand All @@ -109,7 +110,7 @@ export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect {
message,
);

if (this.server == null) throw new WsException('server is null');
if (isNil(this.server)) throw new WsException('server is null');

this.logger.debug(`--> ws: chat-message ${message.body}`);
this.server.to(room.code).emit('chat-message', message);
Expand Down Expand Up @@ -142,9 +143,9 @@ export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
getUser(client: Socket): User {
const request = client.request as any;
if (request == null) throw new WsException('request is null');
if (isNil(request)) throw new WsException('request is null');
const user = request.user;
if (user == null) throw new WsException('user is null');
if (isNil(user)) throw new WsException('user is null');
return user as User;
}
}
12 changes: 6 additions & 6 deletions server/src/socket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export class SocketService {
const host = await room.host;
const problems = await room.problems;

if (roomUsers == null) throw new WsException('roomUsers is null');
if (host == null) throw new WsException('host is null');
if (problems == null || problems.length === 0)
if (roomUsers === undefined) throw new WsException('roomUsers is null');
if (host === undefined) throw new WsException('host is null');
if (problems === undefined || problems.length === 0)
throw new WsException('problems is null');

const problemTypes: ProblemType[] = problems.map((problem) => {
Expand Down Expand Up @@ -144,7 +144,7 @@ export class SocketService {
// check if the user is the host of the room

const host = await roomUser.room.host;
if (host == null) throw new WsException('host is null');
if (host === undefined) throw new WsException('host is null');
if (host.id !== user.id) {
throw new WsException('๋ฐฉ์žฅ์ด ์•„๋‹™๋‹ˆ๋‹ค.');
}
Expand All @@ -154,8 +154,8 @@ export class SocketService {
// update room entity properties: problems, isStarted, endAt

const { problems, duration } = startingRoomInfo;
if (problems == null) throw new WsException('problems is null');
if (duration == null) throw new WsException('duration is null');
if (problems === undefined) throw new WsException('problems is null');
if (duration === undefined) throw new WsException('duration is null');
const bojProblemIds = problems.map((problem) => problem.bojProblemId);
const problemEntities =
await this.problemService.getProblemsByBojProblemIds(bojProblemIds);
Expand Down
11 changes: 6 additions & 5 deletions server/src/submission/submission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserService } from 'src/user/user.service';
import { Repository } from 'typeorm';
import { SubmissionDto } from './dto/submission.dto';
import { SocketService } from '../socket/socket.service';
import { isNil } from '@nestjs/common/utils/shared.utils';

@Injectable()
export class SubmissionService {
Expand Down Expand Up @@ -44,7 +45,7 @@ export class SubmissionService {
if (!user) throw new BadRequestException('์กด์žฌํ•˜์ง€ ์•Š๋Š” ์œ ์ €์ž…๋‹ˆ๋‹ค.');

const roomUsers = await user.joinedRooms;
if (roomUsers == null || roomUsers.length === 0)
if (roomUsers === undefined || roomUsers.length === 0)
throw new BadRequestException('์ฐธ์—ฌ์ค‘์ธ ๋ฐฉ์ด ์—†์Šต๋‹ˆ๋‹ค.');

const roomUser = roomUsers[0];
Expand All @@ -56,17 +57,17 @@ export class SubmissionService {
}

const endAt = room.endAt;
if (endAt == null || endAt < new Date()) {
if (endAt === undefined || endAt < new Date()) {
throw new BadRequestException('์ด๋ฏธ ์ข…๋ฃŒ๋œ ๋ฐฉ์ž…๋‹ˆ๋‹ค.');
}

const problems = await room.problems;
if (problems == null || problems.length === 0)
if (problems === undefined || problems.length === 0)
throw new BadRequestException('๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๋ฐฉ์ž…๋‹ˆ๋‹ค.');

const problem =
await this.problemService.getProblemByBojProblemId(bojProblemId);
if (problem == null)
if (isNil(problem))
throw new BadRequestException('์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.');

if (
Expand All @@ -75,7 +76,7 @@ export class SubmissionService {
throw new BadRequestException('์ฐธ์—ฌ์ค‘์ธ ๋ฐฉ์— ์—†๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.');
}

if (user.username == null)
if (isNil(user.username))
throw new BadRequestException('username์ด ์—†์Šต๋‹ˆ๋‹ค.');

await this.socketService.submitCode(
Expand Down
4 changes: 2 additions & 2 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export class UserService {

async findJoinedRooms(user: User) {
const joinedRooms = await user.joinedRooms;
if (joinedRooms == null) {
if (joinedRooms === undefined) {
throw new BadRequestException('joinedRooms is null');
}
return joinedRooms;
}

async getSingleJoinedRoom(user: User) {
const joinedRooms = await user.joinedRooms;
if (joinedRooms == null) {
if (joinedRooms === undefined) {
throw new InternalServerErrorException('joinedRooms is null');
}
if (joinedRooms.length !== 1) {
Expand Down