Skip to content

Commit 873e6f0

Browse files
committed
linting
1 parent e1a7ab9 commit 873e6f0

11 files changed

+499
-174
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.eslintrc.js

+249
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
module.exports = {
2+
"env": {
3+
"browser": true,
4+
"node": true,
5+
"es6": true,
6+
"mocha": true
7+
},
8+
"extends": "eslint:recommended",
9+
"rules": {
10+
"accessor-pairs": "error",
11+
"array-bracket-spacing": "error",
12+
"array-callback-return": "error",
13+
"arrow-body-style": "error",
14+
"arrow-parens": "error",
15+
"arrow-spacing": "error",
16+
"block-scoped-var": "error",
17+
"block-spacing": "error",
18+
"brace-style": "error",
19+
"callback-return": "error",
20+
"camelcase": "error",
21+
"capitalized-comments": "error",
22+
"class-methods-use-this": "error",
23+
"comma-dangle": "off",
24+
"comma-spacing": [
25+
"error",
26+
{
27+
"after": true,
28+
"before": false
29+
}
30+
],
31+
"comma-style": "error",
32+
"complexity": "error",
33+
"computed-property-spacing": [
34+
"error",
35+
"never"
36+
],
37+
"consistent-return": "error",
38+
"consistent-this": "error",
39+
"curly": "error",
40+
"default-case": "error",
41+
"dot-location": [
42+
"error",
43+
"property"
44+
],
45+
"dot-notation": "error",
46+
"eol-last": "error",
47+
"eqeqeq": "off",
48+
"func-call-spacing": "error",
49+
"func-name-matching": "error",
50+
"func-names": [
51+
"error",
52+
"never"
53+
],
54+
"func-style": [
55+
"error",
56+
"declaration"
57+
],
58+
"generator-star-spacing": "error",
59+
"global-require": "off",
60+
"guard-for-in": "error",
61+
"handle-callback-err": "error",
62+
"id-blacklist": "error",
63+
"id-length": ["error", {"exceptions": ["i", "j", "e"]}],
64+
"id-match": "error",
65+
"indent": "off",
66+
"init-declarations": "off",
67+
"jsx-quotes": "error",
68+
"key-spacing": "error",
69+
"keyword-spacing": "off",
70+
"line-comment-position": "error",
71+
"linebreak-style": [
72+
"error",
73+
"unix"
74+
],
75+
"lines-around-comment": "off",
76+
"lines-around-directive": "error",
77+
"max-depth": "error",
78+
"max-len": ["error", {
79+
"ignoreUrls": true,
80+
"ignoreStrings": true,
81+
"ignoreTemplateLiterals": true,
82+
"ignoreRegExpLiterals": true
83+
}],
84+
"max-nested-callbacks": "error",
85+
"max-params": "error",
86+
"max-statements": "off",
87+
"max-statements-per-line": "error",
88+
"multiline-ternary": "off",
89+
"new-cap": ["error", { "capIsNewExceptionPattern": "^SearchIndex" }],
90+
"new-parens": "error",
91+
"newline-after-var": "off",
92+
"newline-before-return": "off",
93+
"newline-per-chained-call": "error",
94+
"no-alert": "error",
95+
"no-array-constructor": "error",
96+
"no-await-in-loop": "error",
97+
"no-bitwise": "error",
98+
"no-caller": "error",
99+
"no-catch-shadow": "error",
100+
"no-confusing-arrow": "error",
101+
"no-continue": "error",
102+
"no-div-regex": "error",
103+
"no-duplicate-imports": "error",
104+
"no-else-return": "error",
105+
"no-empty-function": "error",
106+
"no-eq-null": "error",
107+
"no-eval": "error",
108+
"no-extend-native": "error",
109+
"no-extra-bind": "error",
110+
"no-extra-label": "error",
111+
"no-extra-parens": "error",
112+
"no-floating-decimal": "error",
113+
"no-implicit-coercion": "error",
114+
"no-implicit-globals": "error",
115+
"no-implied-eval": "error",
116+
"no-inline-comments": "error",
117+
"no-inner-declarations": [
118+
"error",
119+
"functions"
120+
],
121+
"no-invalid-this": "error",
122+
"no-iterator": "error",
123+
"no-label-var": "error",
124+
"no-labels": "error",
125+
"no-lone-blocks": "error",
126+
"no-lonely-if": "error",
127+
"no-loop-func": "error",
128+
"no-magic-numbers": "off",
129+
"no-mixed-operators": "error",
130+
"no-mixed-requires": "error",
131+
"no-multi-spaces": "error",
132+
"no-multi-str": "error",
133+
"no-multiple-empty-lines": "error",
134+
"no-native-reassign": "error",
135+
"no-negated-condition": "error",
136+
"no-negated-in-lhs": "error",
137+
"no-nested-ternary": "error",
138+
"no-new": "error",
139+
"no-new-func": "error",
140+
"no-new-object": "error",
141+
"no-new-require": "error",
142+
"no-new-wrappers": "error",
143+
"no-octal-escape": "error",
144+
"no-param-reassign": "error",
145+
"no-path-concat": "error",
146+
"no-plusplus": "off",
147+
"no-process-env": "error",
148+
"no-process-exit": "off",
149+
"no-proto": "error",
150+
"no-prototype-builtins": "error",
151+
"no-restricted-globals": "error",
152+
"no-restricted-imports": "error",
153+
"no-restricted-modules": "error",
154+
"no-restricted-properties": "error",
155+
"no-restricted-syntax": "error",
156+
"no-return-assign": "error",
157+
"no-return-await": "error",
158+
"no-script-url": "error",
159+
"no-self-compare": "error",
160+
"no-sequences": "error",
161+
"no-shadow": "error",
162+
"no-shadow-restricted-names": "error",
163+
"no-spaced-func": "error",
164+
"no-sync": "error",
165+
"no-tabs": "off",
166+
"no-template-curly-in-string": "error",
167+
"no-ternary": "off",
168+
"no-throw-literal": "error",
169+
"no-trailing-spaces": "error",
170+
"no-undef-init": "error",
171+
"no-undefined": "error",
172+
"no-underscore-dangle": "error",
173+
"no-unmodified-loop-condition": "error",
174+
"no-unneeded-ternary": "error",
175+
"no-unused-expressions": "error",
176+
"no-unused-vars": ["error", {
177+
"argsIgnorePattern": "^_",
178+
"varsIgnorePattern": "^_",
179+
}],
180+
"no-use-before-define": ["error", { "functions": false, "classes": true }],
181+
"no-useless-call": "error",
182+
"no-useless-computed-key": "error",
183+
"no-useless-concat": "error",
184+
"no-useless-constructor": "error",
185+
"no-useless-escape": "error",
186+
"no-useless-rename": "error",
187+
"no-useless-return": "error",
188+
"no-var": "off",
189+
"no-void": "error",
190+
"no-warning-comments": "error",
191+
"no-whitespace-before-property": "error",
192+
"no-with": "error",
193+
"object-curly-newline": "off",
194+
"object-curly-spacing": "error",
195+
"object-property-newline": "error",
196+
"object-shorthand": "off",
197+
"one-var": "off",
198+
"one-var-declaration-per-line": "error",
199+
"operator-assignment": "error",
200+
"operator-linebreak": "error",
201+
"padded-blocks": "off",
202+
"prefer-arrow-callback": "off",
203+
"prefer-const": "error",
204+
"prefer-numeric-literals": "error",
205+
"prefer-reflect": "off",
206+
"prefer-rest-params": "error",
207+
"prefer-spread": "error",
208+
"prefer-template": "error",
209+
"quote-props": "off",
210+
"quotes": [
211+
"error",
212+
"single"
213+
],
214+
"radix": "error",
215+
"require-await": "error",
216+
"require-jsdoc": "off",
217+
"rest-spread-spacing": "error",
218+
"semi": "off",
219+
"semi-spacing": "error",
220+
"sort-imports": "error",
221+
"sort-keys": "error",
222+
"sort-vars": "error",
223+
"space-before-blocks": "error",
224+
"space-before-function-paren": "off",
225+
"space-in-parens": [
226+
"error",
227+
"never"
228+
],
229+
"space-infix-ops": "error",
230+
"space-unary-ops": "error",
231+
"spaced-comment": "error",
232+
"strict": "error",
233+
"symbol-description": "error",
234+
"template-curly-spacing": "error",
235+
"unicode-bom": [
236+
"error",
237+
"never"
238+
],
239+
"valid-jsdoc": "off",
240+
"vars-on-top": "off",
241+
"wrap-iife": "error",
242+
"wrap-regex": "error",
243+
"yield-star-spacing": "error",
244+
"yoda": [
245+
"error",
246+
"never"
247+
]
248+
}
249+
};

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ tickets.json
77
**.swp
88
.v8flags.*
99
*.txt
10+
out/**

bot.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
/* eslint-disable no-console */
2+
13
'use strict'
24

3-
const { Wechaty } = require('wechaty')
5+
const {Wechaty} = require('wechaty')
46

57
const reload = require('./reloader')
68

79
const data = {}
810

9-
Wechaty.instance() // Singleton
10-
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))
11-
.on('login', user => console.log(`User ${user} logined`))
12-
.on('message', message => {
11+
Wechaty.instance()
12+
.on('scan', (url, code) => {
13+
console.log(`Scan QR Code to login: ${code}\n${url}`)
14+
})
15+
.on('login', (user) => {
16+
console.log(`User ${user} logined`)
17+
})
18+
.on('message', (message) => {
1319
message.ready().then(() => {
14-
reload('./handler.js')(data, message)
20+
var config = reload('./config.json')
21+
reload('./handler.js')(config, data, message)
1522
})
1623
})
17-
.init()
24+
.init()

handler.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,42 @@ function content(message) {
1515
}
1616

1717
function cleanContent(prefix, message) {
18-
return content(message).substr(prefix.length).trim()
18+
return content(message)
19+
.substr(prefix.length)
20+
.trim()
1921
}
2022

21-
function messageString(message) {
22-
return `${roomName(message)}/${userName(message)}: ${content(message)}`
23-
}
24-
25-
function mine(content, prefix) {
26-
return content.substr(0, prefix.length).toLowerCase().startsWith(prefix)
23+
function mine(str, prefix) {
24+
return str
25+
.substr(0, prefix.length)
26+
.toLowerCase()
27+
.startsWith(prefix)
2728
}
2829

2930
function destination(message) {
30-
return message.room()? message.room() : message.from()
31+
return message.room() ? message.room() : message.from()
3132
}
3233

33-
function handler(data, message) {
34-
console.log(messageString(message))
34+
function handler(config, data, message) {
3535
try {
36-
var config = reload('./config.json')
37-
if(mine(content(message), config.prefix) && config.whitelisted.indexOf(roomName(message)) != -1) {
38-
console.log(`Captured message: ${message}`)
39-
40-
const processor = reload('./support.js')
36+
if(mine(content(message), config.prefix) &&
37+
config.whitelisted.indexOf(roomName(message)) != -1) {
38+
const processor = reload(config.processor)
4139
if(!data.tickets) {
4240
data.tickets = processor.load(data.store)
43-
}
41+
}
4442
const reply = processor.process(data.tickets, {
43+
content: cleanContent(config.prefix, message),
4544
prefix: config.prefix,
46-
roomName: roomName(message),
47-
userName: userName(message),
48-
content: cleanContent(config.prefix, message)
45+
roomName: roomName(message),
46+
userName: userName(message),
4947
})
5048
processor.store(config.store, data.tickets)
5149
destination(message).say(reply)
5250
}
53-
} catch(e) {
54-
destination(message).say(e)
51+
} catch(err) {
52+
destination(message).say(err)
5553
}
5654
}
5755

58-
module.exports = handler
56+
module.exports = handler

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99
},
1010
"devDependencies": {
1111
"chai": "^3.5.0",
12+
"eslint": "^3.14.1",
1213
"mocha": "^3.2.0",
14+
"precommit-hook": "^3.0.0",
1315
"tmp": "0.0.31"
1416
},
1517
"scripts": {
1618
"test": "node_modules/.bin/mocha",
17-
"start": "node bot.js"
19+
"start": "node bot.js",
20+
"lint": "node_modules/eslint/bin/eslint.js *.js",
21+
"validate": "npm ls"
1822
},
23+
"pre-commit": [
24+
"lint",
25+
"test"
26+
],
1927
"author": "Ricky Ng-Adam <rngadam@gmail.com>",
2028
"license": "ISC"
2129
}

reloader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const updates = {}
77
const modules = {}
88

99
function reloadIfUpdated(name) {
10+
/* eslint-disable no-sync */
1011
const mtime = fs.statSync(name).mtime.getTime()
1112
if(!updates[name] || updates[name] < mtime) {
12-
console.log(`RELOADING ${name}`)
1313
updates[name] = mtime
1414
modules[name] = reload(name)
1515
}
1616
return modules[name]
1717
}
1818

19-
module.exports = reloadIfUpdated
19+
module.exports = reloadIfUpdated

0 commit comments

Comments
 (0)