Skip to content

Commit 5e52612

Browse files
committed
🚨 improve lint config
1 parent 474c61b commit 5e52612

File tree

8 files changed

+8088
-19
lines changed

8 files changed

+8088
-19
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"parserOptions": {
44
"sourceType": "module"
55
},
6-
"extends": ["google", "eslint:recommended", "plugin:react/recommended"],
6+
"extends": ["alloy", "alloy/react", "alloy/typescript"],
77
"plugins": ["react", "@typescript-eslint"],
88
"rules": {
99
"no-console": 1,

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
parserOptions: {
44
sourceType: 'module'
55
},
6-
extends: ['google', 'eslint:recommended', 'plugin:react/recommended'],
6+
extends: ['alloy', 'alloy/react', 'alloy/typescript'],
77
plugins: ['react', '@typescript-eslint'],
88
rules: {
99
'no-console': 1,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"babel-preset-react": "^6.22.0",
4444
"clean-webpack-plugin": "^2.0.1",
4545
"css-loader": "^3.2.0",
46+
"eslint-config-alloy": "^3.5.0",
4647
"eslint-plugin-react": "^7.11.1",
4748
"express": "^4.14.1",
4849
"file-loader": "^3.0.1",

src/components/ChatInput.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import React, { Component } from 'react';
22

3-
interface MyProps { socket: any; myId: string; myName: string }
4-
interface MyState { socket: any; message: string; myId: string; myName: string }
3+
interface MyProps {
4+
socket: any;
5+
myId: string;
6+
myName: string;
7+
}
8+
interface MyState {
9+
socket: any;
10+
message: string;
11+
myId: string;
12+
myName: string;
13+
}
514
export default class ChatInput extends Component<MyProps, MyState> {
6-
constructor(props) {
15+
public constructor(props: MyProps) {
716
super(props);
817
this.state = {
918
socket: props.socket,
@@ -14,25 +23,26 @@ export default class ChatInput extends Component<MyProps, MyState> {
1423
}
1524

1625
// 监控input变化
17-
handleChange(e) {
26+
public handleChange(e: React.ChangeEvent<HTMLInputElement>) {
1827
this.setState({ message: e.target.value });
1928
}
2029

2130
// 点击提交或按回车
2231

23-
handleClick(e) {
32+
public handleClick(e: MouseEvent) {
2433
e.preventDefault();
2534
this.sendMessage();
2635
}
27-
handleKeyPress(e) {
28-
if (e.key == 'Enter') {
36+
37+
public handleKeyPress(e: KeyboardEvent): boolean {
38+
if (e.key === 'Enter') {
2939
this.sendMessage();
3040
}
3141
return false;
3242
}
3343

3444
// 发送聊天信息
35-
sendMessage() {
45+
public sendMessage(): boolean {
3646
const message = this.state.message;
3747
const socket = this.state.socket;
3848
if (message) {
@@ -46,7 +56,7 @@ export default class ChatInput extends Component<MyProps, MyState> {
4656
}
4757
return false;
4858
}
49-
render() {
59+
public render(): JSX.Element {
5060
return (
5161
<div className="bottom-area">
5262
<div className="input-box">

src/components/ChatRoom.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { Context } from '../context';
55

66
// 生成消息id
77
const generateMsgId = () => {
8-
return new Date().getTime() + '' + Math.floor(Math.random() * 899 + 100);
8+
return String(new Date().getTime()) + Math.floor(Math.random() * 899 + 100);
99
};
1010

1111
// 时间格式
1212
const generateTime = () => {
1313
const hour = new Date().getHours();
1414
const minute = new Date().getMinutes();
15-
const hourText = hour === 0 ? '00' : hour + '';
16-
const minuteText = minute < 10 ? '0' + minute : minute + '';
15+
const hourText = hour === 0 ? '00' : String(hour);
16+
const minuteText = minute < 10 ? '0' + minute : String(minute);
1717
return hourText + ':' + minuteText;
1818
};
1919

src/components/Messages.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React, { useContext, useEffect, useRef } from 'react';
22
import { Context } from '../context';
33

44
const Message = (props) => {
5-
if (props.msgType == 'system') {
5+
if (props.msgType === 'system') {
66
return (
77
<div className="one-message system-message">
8-
{props.msgUser} {props.action == 'login' ? '进入了聊天室' : '离开了聊天室'} <span className="time">&nbsp;{props.time}</span>
8+
{props.msgUser} {props.action === 'login' ? '进入了聊天室' : '离开了聊天室'} <span className="time">&nbsp;{props.time}</span>
99
</div>
1010
);
1111
} else {
@@ -33,7 +33,7 @@ const Messages = (props) => {
3333
return (
3434
<div className="messages" ref={messageList}>
3535
{messages.map((message) => (
36-
<Message key={message.msgId} msgType={message.type} msgUser={message.username} action={message.action} isMe={uid == message.uid ? true : false} time={message.time} />
36+
<Message key={message.msgId} msgType={message.type} msgUser={message.username} action={message.action} isMe={uid === message.uid ? true : false} time={message.time} />
3737
))}
3838
</div>
3939
);

src/container/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const userState = (username) => {
99
};
1010

1111
const generateUid = () => {
12-
return new Date().getTime() + '' + Math.floor(Math.random() * 999 + 1);
12+
return String(new Date().getTime()) + Math.floor(Math.random() * 999 + 1);
1313
};
1414

1515
const App = (props) => {
@@ -24,7 +24,7 @@ const App = (props) => {
2424
state.socket.emit('login', { uid, username });
2525
};
2626
const handleKeyPress = (e) => {
27-
if (e.key == 'Enter') {
27+
if (e.key === 'Enter') {
2828
handleLogin();
2929
}
3030
return false;

0 commit comments

Comments
 (0)