Skip to content

Commit f76ba54

Browse files
committed
style: add lint
1 parent eccfc98 commit f76ba54

19 files changed

+1056
-475
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ root = true
55
[*]
66
charset = utf-8
77
indent_style = space
8-
indent_size = 4
8+
indent_size = 2
99
end_of_line = lf
1010
insert_final_newline = true
1111
trim_trailing_whitespace = true

.eslintrc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": [
3+
"plugin:@typescript-eslint/recommended"
4+
],
5+
"parser": "@typescript-eslint/parser",
6+
"parserOptions": {
7+
"project": "tsconfig.json"
8+
},
9+
"plugins": [
10+
"@typescript-eslint",
11+
"eslint-plugin-import",
12+
"eslint-plugin-eslint-comments",
13+
"eslint-plugin-jsdoc"
14+
],
15+
"env": {
16+
"node": true,
17+
"jest": true
18+
},
19+
"rules": {}
20+
}

.github/workflows/lint-and-test-code.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- master
77
- release
8+
- 2.0
89

910
jobs:
1011
lint-code:

.prettierrc.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
trailingComma: "all"
2-
tabWidth: 4
3-
semi: false
4-
singleQuote: true
1+
trailingComma: "none"
2+
tabWidth: 2
3+
semi: true
4+
doubleQuote: true

import-sorter.json

-6
This file was deleted.

lib/core/__test__/add.test.ts

-10
This file was deleted.
+21-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
// import logger from '../../logger/index'
2-
import { consoleHack } from '../../runtime/console.hack'
1+
import { consoleHack } from "../../runtime/console.hack";
32

43
// jest.mock('../../logger/index')
54

65
beforeAll(() => {
7-
consoleHack()
8-
})
6+
consoleHack();
7+
});
98

10-
const writeLog = jest.fn((level, info) => {
11-
return `${level}: ${info}`
12-
})
9+
describe("console hack test", () => {
10+
test("ensure console contains all origin functions", () => {
11+
expect(typeof console.originDebug).toBe("function");
12+
expect(typeof console.originLog).toBe("function");
13+
expect(typeof console.originInfo).toBe("function");
14+
expect(typeof console.originDir).toBe("function");
15+
expect(typeof console.originWarn).toBe("function");
16+
expect(typeof console.originError).toBe("function");
17+
});
1318

14-
describe('console hack test', () => {
15-
test('ensure console contains all origin functions', () => {
16-
expect(typeof console.originDebug).toBe('function')
17-
expect(typeof console.originLog).toBe('function')
18-
expect(typeof console.originInfo).toBe('function')
19-
expect(typeof console.originDir).toBe('function')
20-
expect(typeof console.originWarn).toBe('function')
21-
expect(typeof console.originError).toBe('function')
22-
})
23-
test('console.debug should be logged by logger', () => {
24-
console.debug('test_log')
25-
console.log('test_log')
26-
console.info('test_log')
27-
console.dir('test_log')
28-
console.warn('test_log')
29-
console.error('test_log')
30-
})
31-
})
19+
test("console.debug should be logged by logger", () => {
20+
console.debug("test_log");
21+
console.log("test_log");
22+
console.info("test_log");
23+
console.dir("test_log");
24+
console.warn("test_log");
25+
console.error("test_log");
26+
});
27+
});
+64-64
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
1-
import { lookup, promises as dnsPromises } from 'dns'
2-
import { isIP } from 'net'
1+
import { lookup } from "dns";
2+
import { isIP } from "net";
33

4-
import { eventBus } from '../../bus'
5-
import { dnsHack } from '../../runtime/dns.hack'
4+
import { eventBus } from "../../bus";
5+
import { dnsHack } from "../../runtime/dns.hack";
66

77
beforeAll(() => {
8-
dnsHack()
9-
})
8+
dnsHack();
9+
});
1010

11-
describe('dns hack test', () => {
12-
test('dns could work normally', async () => {
13-
await new Promise((resolve, reject) => {
14-
lookup('qq.com', (err, address, family) => {
15-
expect(err).toBeNull()
16-
expect(isIP(address)).toBeTruthy()
17-
expect(family).toBeTruthy()
18-
resolve()
19-
})
20-
})
21-
})
11+
describe("dns hack test", () => {
12+
test("dns could work normally", async () => {
13+
await new Promise((resolve) => {
14+
lookup("qq.com", (err, address, family) => {
15+
expect(err).toBeNull();
16+
expect(isIP(address)).toBeTruthy();
17+
expect(family).toBeTruthy();
18+
resolve();
19+
});
20+
});
21+
});
2222

23-
test('eventBus was informed', async () => {
24-
await new Promise((resolve, reject) => {
25-
eventBus.on('DNS_LOOKUP_SUCCESS', data => {
26-
resolve(data)
27-
})
23+
test("eventBus was informed", async () => {
24+
await new Promise((resolve, reject) => {
25+
eventBus.on("DNS_LOOKUP_SUCCESS", data => {
26+
resolve(data);
27+
});
2828

29-
eventBus.on('DNS_LOOKUP_ERROR', err => {
30-
reject(err)
31-
})
29+
eventBus.on("DNS_LOOKUP_ERROR", err => {
30+
reject(err);
31+
});
3232

33-
lookup('qq.com', () => {
34-
// nothing
35-
})
36-
})
37-
})
33+
lookup("qq.com", () => {
34+
// nothing
35+
});
36+
});
37+
});
3838

39-
test('ipv4 should return immediately', async () => {
40-
await new Promise((resolve, reject) => {
41-
const ip = '1.2.3.4'
42-
lookup(ip, (err, address, family) => {
43-
expect(err).toBeNull()
44-
expect(address).toEqual(ip)
45-
expect(family).toBeTruthy()
46-
resolve()
47-
})
48-
})
49-
})
39+
test("ipv4 should return immediately", async () => {
40+
await new Promise((resolve) => {
41+
const ip = "1.2.3.4";
42+
lookup(ip, (err, address, family) => {
43+
expect(err).toBeNull();
44+
expect(address).toEqual(ip);
45+
expect(family).toBeTruthy();
46+
resolve();
47+
});
48+
});
49+
});
5050

51-
test('ipv6 should return immediately', async () => {
52-
await new Promise((resolve, reject) => {
53-
const ip = '::ffff:192.0.2.128'
54-
lookup(ip, (err, address, family) => {
55-
expect(err).toBeNull()
56-
expect(address).toEqual(ip)
57-
expect(family).toBeTruthy()
58-
resolve()
59-
})
60-
})
61-
})
51+
test("ipv6 should return immediately", async () => {
52+
await new Promise((resolve) => {
53+
const ip = "::ffff:192.0.2.128";
54+
lookup(ip, (err, address, family) => {
55+
expect(err).toBeNull();
56+
expect(address).toEqual(ip);
57+
expect(family).toBeTruthy();
58+
resolve();
59+
});
60+
});
61+
});
6262

63-
test('a wrong domain should fail', async () => {
64-
await new Promise((resolve, reject) => {
65-
const nullDomain = 'this.is.not.a.domain'
66-
lookup(nullDomain, (err, address, family) => {
67-
// error could be "Dns Lookup Timeout"
68-
// or "getaddrinfo ENOTFOUND this.is.not.a.domain"
69-
expect(err).toBeTruthy()
70-
resolve()
71-
})
72-
})
73-
})
74-
})
63+
test("a wrong domain should fail", async () => {
64+
await new Promise((resolve) => {
65+
const nullDomain = "this.is.not.a.domain";
66+
lookup(nullDomain, (err) => {
67+
// error could be "Dns Lookup Timeout"
68+
// or "getaddrinfo ENOTFOUND this.is.not.a.domain"
69+
expect(err).toBeTruthy();
70+
resolve();
71+
});
72+
});
73+
});
74+
});

lib/core/add.ts

-3
This file was deleted.

lib/core/bus.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { EventEmitter } from 'events'
1+
import { EventEmitter } from "events";
22

33
export enum EVENT_LIST {
4-
DNS_LOOKUP_SUCCESS = 'DNS_LOOKUP_SUCCESS',
5-
DNS_LOOKUP_ERROR = 'DNS_LOOKUP_ERROR',
4+
DNS_LOOKUP_SUCCESS = "DNS_LOOKUP_SUCCESS",
5+
DNS_LOOKUP_ERROR = "DNS_LOOKUP_ERROR"
66
}
77

8-
export interface IEventPayload {
9-
error: Error | null
10-
code: number
11-
msg: string
12-
success: boolean
13-
data: any | null
8+
export interface EventPayload {
9+
error: Error | null;
10+
code: number;
11+
msg: string;
12+
success: boolean;
13+
data: any | null;
1414
}
1515

16-
let bus: EventEmitter
16+
let bus: EventEmitter | undefined;
1717

18-
export const eventBus = bus ? bus : (bus = new EventEmitter())
18+
export const eventBus = bus ? bus : (bus = new EventEmitter());

lib/core/config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const config = {
2-
timeout: {
3-
dns: 3000,
4-
},
5-
}
2+
timeout: {
3+
dns: 3000
4+
}
5+
};

lib/core/logger/index.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
class Logger {
2-
writeLog(level: any, info: any) {}
3-
debug(str: string) {
4-
console.debug(str)
5-
}
6-
error(str: string) {
7-
console.error(str)
8-
}
2+
writeLog(level: any, info: any) {
3+
// do nothing
4+
}
5+
debug(str: string) {
6+
console.debug(str);
7+
}
8+
9+
error(str: string) {
10+
console.error(str);
11+
}
912
}
1013

11-
let logger
14+
let logger: Logger;
1215

1316
if (!logger) {
14-
logger = new Logger()
17+
logger = new Logger();
1518
}
1619

17-
export default logger
20+
export default logger;

0 commit comments

Comments
 (0)