Skip to content

Commit 576508a

Browse files
authored
Merge pull request #68 from alienzhou/lint
ci: add linter
2 parents f48b2a8 + 8082165 commit 576508a

26 files changed

+889
-561
lines changed

.eslintrc.js

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
sourceType: 'module',
6+
},
7+
plugins: [
8+
'@typescript-eslint/eslint-plugin',
9+
'prettier',
10+
'import',
11+
],
12+
extends: [
13+
"eslint:all",
14+
'plugin:@typescript-eslint/all',
15+
'prettier',
16+
'prettier/@typescript-eslint',
17+
],
18+
root: true,
19+
env: {
20+
es6: true,
21+
node: true,
22+
jest: true,
23+
},
24+
rules: {
25+
"array-bracket-newline": [
26+
"error",
27+
"consistent"
28+
],
29+
"array-element-newline": [
30+
"error",
31+
"consistent"
32+
],
33+
"arrow-parens": [
34+
"error",
35+
"as-needed"
36+
],
37+
"class-methods-use-this": "off",
38+
"capitalized-comments": "off",
39+
"comma-dangle": [
40+
"error",
41+
"always-multiline"
42+
],
43+
"dot-location": [
44+
"error",
45+
"property"
46+
],
47+
"func-names": [
48+
"error",
49+
"as-needed"
50+
],
51+
"id-length": "off",
52+
"implicit-arrow-linebreak": "off",
53+
"init-declarations": "off",
54+
"max-len": [
55+
"error",
56+
120
57+
],
58+
"max-lines": "off",
59+
"max-lines-per-function": "off",
60+
"max-params": "off",
61+
"max-statements": "off",
62+
"multiline-comment-style": "off",
63+
"multiline-ternary": [
64+
"error",
65+
"always-multiline"
66+
],
67+
"newline-per-chained-call": [
68+
"error",
69+
{
70+
"ignoreChainWithDepth": 2
71+
}
72+
],
73+
"no-await-in-loop": "off",
74+
"no-bitwise": "off",
75+
"no-confusing-arrow": "off",
76+
"no-constant-condition": [
77+
"error",
78+
{
79+
"checkLoops": false
80+
}
81+
],
82+
"no-continue": "off",
83+
"no-duplicate-imports": "off",
84+
"no-param-reassign": "off",
85+
"no-underscore-dangle": "off",
86+
"import/no-duplicates": [
87+
"error",
88+
{
89+
"considerQueryString": true
90+
}
91+
],
92+
"no-empty": "off",
93+
"no-implicit-coercion": "off",
94+
"no-invalid-this": "off",
95+
"no-mixed-operators": "off",
96+
"no-multiple-empty-lines": [
97+
"error",
98+
{
99+
"max": 2,
100+
"maxEOF": 1
101+
}
102+
],
103+
"no-plusplus": [
104+
"error",
105+
{
106+
"allowForLoopAfterthoughts": true
107+
}
108+
],
109+
"no-process-env": "off",
110+
"no-shadow": "off",
111+
"no-ternary": "off",
112+
"no-unused-expressions": "off",
113+
"no-warning-comments": "off",
114+
"new-cap": "off",
115+
"one-var": [
116+
"error",
117+
"never"
118+
],
119+
"padded-blocks": [
120+
"error",
121+
"never"
122+
],
123+
"padding-line-between-statements": [
124+
"error",
125+
{
126+
"blankLine": "always",
127+
"prev": [
128+
"const",
129+
"let",
130+
"var"
131+
],
132+
"next": "*"
133+
},
134+
{
135+
"blankLine": "always",
136+
"prev": "*",
137+
"next": [
138+
"const",
139+
"let",
140+
"var"
141+
]
142+
},
143+
{
144+
"blankLine": "any",
145+
"prev": [
146+
"const",
147+
"let",
148+
"var"
149+
],
150+
"next": [
151+
"const",
152+
"let",
153+
"var"
154+
]
155+
},
156+
{
157+
"blankLine": "always",
158+
"prev": "*",
159+
"next": "return"
160+
},
161+
{
162+
"blankLine": "always",
163+
"prev": "block-like",
164+
"next": "*"
165+
},
166+
{
167+
"blankLine": "always",
168+
"prev": "*",
169+
"next": "block-like"
170+
}
171+
],
172+
"prefer-destructuring": "off",
173+
"quote-props": [
174+
"error",
175+
"as-needed"
176+
],
177+
"quotes": [
178+
"error",
179+
"single",
180+
{
181+
"avoidEscape": true
182+
}
183+
],
184+
"object-curly-spacing": [
185+
"error",
186+
"always"
187+
],
188+
"require-atomic-updates": "off",
189+
"require-unicode-regexp": "off",
190+
"semi": "off",
191+
"sort-imports": "off",
192+
"sort-keys": "off",
193+
"wrap-regex": "off",
194+
"import/default": "off",
195+
196+
"@typescript-eslint/explicit-function-return-type": "off",
197+
"@typescript-eslint/explicit-member-accessibility": [
198+
"error",
199+
{
200+
"accessibility": "no-public"
201+
}
202+
],
203+
"@typescript-eslint/explicit-module-boundary-types": "off",
204+
"@typescript-eslint/generic-type-naming": "off",
205+
"@typescript-eslint/init-declarations": "off",
206+
"@typescript-eslint/naming-convention": "off",
207+
"@typescript-eslint/no-dynamic-delete": "off",
208+
"@typescript-eslint/no-empty-function": "off",
209+
"@typescript-eslint/no-explicit-any": "off",
210+
"@typescript-eslint/no-extraneous-class": [
211+
"error",
212+
{
213+
"allowWithDecorator": true
214+
}
215+
],
216+
"@typescript-eslint/no-invalid-this": "off",
217+
"@typescript-eslint/no-invalid-void-type": "off",
218+
"@typescript-eslint/no-magic-numbers": "off",
219+
"@typescript-eslint/no-misused-promises": "off",
220+
"@typescript-eslint/no-parameter-properties": "off",
221+
"@typescript-eslint/no-throw-literal": "off",
222+
"@typescript-eslint/no-type-alias": "off",
223+
"@typescript-eslint/no-unnecessary-condition": "off",
224+
"@typescript-eslint/no-unsafe-assignment": "off",
225+
"@typescript-eslint/no-unused-expressions": "off",
226+
"@typescript-eslint/no-unused-vars": [
227+
"error",
228+
{
229+
"argsIgnorePattern": "^_$",
230+
"varsIgnorePattern": "^_$",
231+
"ignoreRestSiblings": true
232+
}
233+
],
234+
"@typescript-eslint/prefer-nullish-coalescing": "off",
235+
"@typescript-eslint/prefer-readonly-parameter-types": "off",
236+
"@typescript-eslint/promise-function-async": "off",
237+
"@typescript-eslint/restrict-template-expressions": [
238+
"warn",
239+
{
240+
"allowNumber": true
241+
}
242+
],
243+
"@typescript-eslint/semi": "error",
244+
"@typescript-eslint/strict-boolean-expressions": "off",
245+
"@typescript-eslint/typedef": "off",
246+
"prettier/prettier": "error"
247+
},
248+
};

.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 120,
5+
"tabWidth": 4,
6+
"arrowParens": "avoid",
7+
"bracketSpacing": true,
8+
"parser": "typescript"
9+
}

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "dist/web-highlighter.min.js",
66
"browser": "dist/web-highlighter.min.js",
77
"scripts": {
8+
"lint": "eslint \"src/**/*.ts\" --fix",
89
"test": "mocha -r ts-node/register -r tsconfig-paths/register test/**.spec.ts",
910
"coverage": "nyc -r lcov -e .ts -x \"test/**/*.ts\" npm run test",
1011
"serve-example": "http-server example/static",
@@ -20,9 +21,15 @@
2021
},
2122
"husky": {
2223
"hooks": {
23-
"pre-commit": "npm run test"
24+
"pre-commit": "lint-staged && npm run test"
2425
}
2526
},
27+
"lint-staged": {
28+
"src/**/*.ts": [
29+
"prettier --write",
30+
"eslint --fix"
31+
]
32+
},
2633
"homepage": "https://alienzhou.github.io/web-highlighter",
2734
"repository": {
2835
"type": "git",
@@ -46,21 +53,29 @@
4653
"@types/jsdom-global": "^3.0.2",
4754
"@types/mocha": "^7.0.2",
4855
"@types/sinon": "^9.0.1",
56+
"@typescript-eslint/eslint-plugin": "^4.13.0",
57+
"@typescript-eslint/parser": "^4.13.0",
4958
"better-opn": "^1.0.0",
5059
"chai": "^4.2.0",
5160
"chalk": "^2.4.2",
5261
"clean-webpack-plugin": "^1.0.0",
5362
"coveralls": "^3.1.0",
5463
"css-loader": "^1.0.1",
64+
"eslint": "^7.18.0",
65+
"eslint-config-prettier": "^7.1.0",
66+
"eslint-plugin-import": "^2.22.1",
67+
"eslint-plugin-prettier": "^3.3.1",
5568
"fs-extra": "^7.0.1",
5669
"html-webpack-plugin": "^3.2.0",
5770
"http-server": "^0.11.1",
58-
"husky": "^4.2.5",
71+
"husky": "^4.3.8",
5972
"jsdom": "^16.2.2",
6073
"jsdom-global": "^3.0.2",
74+
"lint-staged": "^10.5.3",
6175
"mocha": "^7.1.2",
6276
"npm-run-all": "^4.1.5",
6377
"nyc": "^15.0.1",
78+
"prettier": "^2.2.1",
6479
"showdown": "^1.9.0",
6580
"sinon": "^9.0.2",
6681
"style-loader": "^0.23.1",
@@ -69,7 +84,7 @@
6984
"ts-node": "^8.10.1",
7085
"tsconfig-paths": "^3.9.0",
7186
"tscpaths": "0.0.9",
72-
"typescript": "^3.1.6",
87+
"typescript": "^4.1.3",
7388
"webpack": "^4.25.1",
7489
"webpack-cli": "^3.1.2",
7590
"webpack-dev-server": ">=3.1.11",

src/data/cache.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import EventEmitter from '@src/util/event.emitter';
2-
import HighlightSource from '../model/source';
3-
import {ERROR} from '../types'
2+
import type HighlightSource from '../model/source';
3+
import { ERROR } from '../types';
44

55
class Cache extends EventEmitter {
66
private _data: Map<string, HighlightSource> = new Map();
77

8-
constructor() {
9-
super();
10-
}
11-
128
get data() {
139
return this.getAll();
1410
}
@@ -20,8 +16,10 @@ class Cache extends EventEmitter {
2016
save(source: HighlightSource | HighlightSource[]): void {
2117
if (!Array.isArray(source)) {
2218
this._data.set(source.id, source);
19+
2320
return;
2421
}
22+
2523
source.forEach(s => this._data.set(s.id, s));
2624
}
2725

@@ -35,20 +33,25 @@ class Cache extends EventEmitter {
3533

3634
getAll(): HighlightSource[] {
3735
const list: HighlightSource[] = [];
38-
for (let pair of this._data) {
36+
37+
for (const pair of this._data) {
3938
list.push(pair[1]);
4039
}
40+
4141
return list;
4242
}
4343

4444
removeAll(): string[] {
4545
const ids: string[] = [];
46-
for (let pair of this._data) {
46+
47+
for (const pair of this._data) {
4748
ids.push(pair[0]);
4849
}
50+
4951
this._data = new Map();
52+
5053
return ids;
5154
}
5255
}
5356

54-
export default Cache;
57+
export default Cache;

0 commit comments

Comments
 (0)