Skip to content

Commit d91f163

Browse files
authored
Merge pull request #11 from ymyqwe/feature/improve_eslint_config
Feature/improve eslint config
2 parents 474c61b + e289cb8 commit d91f163

File tree

9 files changed

+2960
-2869
lines changed

9 files changed

+2960
-2869
lines changed

.eslintrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
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"],
8+
"settings": {
9+
"react": {
10+
"version": "detect"
11+
}
12+
},
813
"rules": {
914
"no-console": 1,
1015
"react/prop-types": 0,

.eslintrc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ 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'],
8+
settings: {
9+
react: {
10+
version: 'detect'
11+
}
12+
},
813
rules: {
914
'no-console': 1,
1015
'react/prop-types': 0,

package-lock.json

Lines changed: 2900 additions & 2846 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
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",
@@ -66,7 +67,7 @@
6667
"@typescript-eslint/eslint-plugin": "^2.7.0",
6768
"@typescript-eslint/parser": "^2.7.0",
6869
"cross-env": "^6.0.3",
69-
"eslint": "^6.6.0",
70+
"eslint": "^6.8.0",
7071
"eslint-config-google": "^0.11.0",
7172
"open-browsers": "^1.1.1",
7273
"webpack-cli": "^3.3.0",

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;

src/context/index.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import io from 'socket.io-client';
33

44
const Context = createContext(null);
55

6-
const initValue = {
6+
interface StateType {
7+
username: string;
8+
uid: string;
9+
socket: any;
10+
messages: [];
11+
onlineUsers: { [key: string]: string };
12+
onlineCount: number;
13+
userhtml: string;
14+
}
15+
16+
const initValue: StateType = {
717
username: '',
818
uid: '',
919
socket: io(),
@@ -51,7 +61,13 @@ const userMessage = (usrMsg: UserMessage, state): object => {
5161
messages: state.messages.concat(usrMsg.message)
5262
};
5363
};
54-
function reducer(state, action) {
64+
interface Payload extends UserMessage, SystemMessage, Login {}
65+
interface ActionType {
66+
type: string;
67+
payload: Payload;
68+
}
69+
70+
const reducer = (state: StateType, action: ActionType): StateType => {
5571
// console.log(state, action);
5672
switch (action.type) {
5773
case 'login':
@@ -63,7 +79,7 @@ function reducer(state, action) {
6379
default:
6480
return state;
6581
}
66-
}
82+
};
6783

6884
const ContextProvider = (props) => {
6985
const [state, dispatch] = useReducer(reducer, initValue);

0 commit comments

Comments
 (0)