Skip to content

Commit ebf7e7a

Browse files
authored
update eslint config (#176)
1 parent 3225540 commit ebf7e7a

39 files changed

+144
-156
lines changed

.eslintrc.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"node": true
66
},
77
"extends": [
8-
"eslint:recommended"
8+
"standard"
99
],
1010
"parserOptions": {
11+
"sourceType": "script",
1112
"ecmaVersion": 2017
1213
},
1314
"root": true,
@@ -61,6 +62,9 @@
6162
"no-console": [
6263
"error"
6364
],
65+
"no-tabs": [
66+
"off"
67+
],
6468
"no-var": [
6569
"error"
6670
],

lib/add-postcss-source.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const get = require('lodash.get');
33

4-
function addPostcssSource(file, errors) {
4+
function addPostcssSource (file, errors) {
55
const source = get(file, 'postcss.root.source.input.css');
66
if (source && errors.length) {
77
const doc = source && source.match(/^.*$/gm);

lib/browser-reporter.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const jsBrowserReporter = require('./js-browser-reporter');
99
* @param {Buffer} [buf=buffile.contents] 文件内容buffer, 默认从file.contents获取
1010
* @return {Buffer} 新的文件内容
1111
*/
12-
function appendReporter(file, errors, buf) {
13-
12+
function appendReporter (file, errors, buf) {
1413
buf = buf || file.contents;
1514

1615
// 在buffer中的代码中注入报错语句
@@ -19,15 +18,14 @@ function appendReporter(file, errors, buf) {
1918
if (file.postcss || file.csslint) {
2019
contentReporter = cssBrowserReporter(errors);
2120
} else if (file.jshint || file.eslint || file.jscs) {
22-
contentReporter = `\n;\n(${ jsBrowserReporter })(${ JSON.stringify(errors, circular()) });`;
21+
contentReporter = `\n;\n(${jsBrowserReporter})(${JSON.stringify(errors, circular())});`;
2322
}
2423

2524
if (contentReporter) {
26-
return Buffer.concat([buf, new Buffer(contentReporter)]);
25+
return Buffer.concat([buf, Buffer.from(contentReporter)]);
2726
} else {
2827
return buf;
2928
}
30-
3129
}
3230

3331
/**
@@ -36,7 +34,7 @@ function appendReporter(file, errors, buf) {
3634
* @param {Array} [errors] 错误信息
3735
* @return {undefined}
3836
*/
39-
function browserReporter(file, errors) {
37+
function browserReporter (file, errors) {
4038
errors = errors || file.report.errors;
4139
if (file.isStream()) {
4240
file.contents = file.contents.pipe(new BufferStreams((err, buf, done) => {
@@ -66,5 +64,4 @@ function circular () {
6664
};
6765
}
6866

69-
7067
module.exports = browserReporter;

lib/ci-reporter.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ const severity = {
2929
info: 'information',
3030
};
3131

32-
function noop() {
32+
function noop () {
3333
return Promise.resolve();
3434
}
3535

36-
function appveyor(errors) {
36+
function appveyor (errors) {
3737
errors = errors.map(error => {
3838
got.post(appveyorApiUrl, {
3939
body: {
@@ -51,7 +51,7 @@ function appveyor(errors) {
5151
return Promise.all(errors);
5252
}
5353

54-
function getEnv(names) {
54+
function getEnv (names) {
5555
names = Array.from(arguments);
5656
let name;
5757
let value;
@@ -63,7 +63,7 @@ function getEnv(names) {
6363
}
6464
}
6565

66-
function junit(errors) {
66+
function junit (errors) {
6767
const builder = reportBuilder.newBuilder();
6868
Object.keys(errors).forEach(fileName => {
6969
const suite = builder.testSuite().name(fileName);
@@ -83,7 +83,7 @@ function junit(errors) {
8383
return fs.outputFile(reportPath(String(Date.now()), 'lint-result.xml'), builder.build());
8484
}
8585

86-
function checkstyle(errors) {
86+
function checkstyle (errors) {
8787
const results = [];
8888

8989
Object.keys(errors).forEach(filename => {
@@ -105,9 +105,9 @@ function checkstyle(errors) {
105105
);
106106
}
107107

108-
function getErrorsByFile(errors) {
108+
function getErrorsByFile (errors) {
109109
const result = {};
110-
errors.forEach(function(error) {
110+
errors.forEach(function (error) {
111111
if (result[error.fileName]) {
112112
result[error.fileName].push(error);
113113
} else {
@@ -117,7 +117,7 @@ function getErrorsByFile(errors) {
117117
return result;
118118
}
119119

120-
function reduceErrors(files) {
120+
function reduceErrors (files) {
121121
return files.reduce((errors, file) => (
122122
errors.concat(file.report.errors)
123123
), []).filter(Boolean);
@@ -132,7 +132,7 @@ if (ci.isCI && (ci.APPVEYOR || ci.CIRCLE || (ci.JENKINS && hasCheckstyle))) {
132132
appveyorApiUrl = url.resolve(appveyorApiUrl, 'api/build/compilationmessages');
133133
}
134134
}
135-
module.exports = function(files) {
135+
module.exports = function (files) {
136136
const errors = reduceErrors(files);
137137
if (!errors.length) {
138138
return noop();
@@ -154,15 +154,15 @@ if (ci.isCI && (ci.APPVEYOR || ci.CIRCLE || (ci.JENKINS && hasCheckstyle))) {
154154
module.exports = noop;
155155
}
156156

157-
function resolveHomePath(strPath) {
157+
function resolveHomePath (strPath) {
158158
return strPath.replace(
159159
/^~(?=\/)/,
160160
os.homedir
161161
);
162162
}
163163

164164
// https://circleci.com/docs/2.0/collect-test-data/
165-
function circleReportPath() {
165+
function circleReportPath () {
166166
const CIRCLE_JOB = process.env.CIRCLE_JOB;
167167
const CIRCLE_WORKING_DIRECTORY = process.env.CIRCLE_WORKING_DIRECTORY;
168168
if (!(ci.CIRCLE && CIRCLE_JOB && CIRCLE_WORKING_DIRECTORY)) {

lib/css-browser-reporter.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const styles = {
2525
/* background */
2626
'color': 'white',
2727
'background-color': '#df4f5e',
28-
'background': `url("data:image/svg+xml;charset=utf-8,${ warnIcon }") .5em 1.5em no-repeat, #df4f5e linear-gradient(#df4f5e, #ce3741)`,
28+
'background': `url("data:image/svg+xml;charset=utf-8,${warnIcon}") .5em 1.5em no-repeat, #df4f5e linear-gradient(#df4f5e, #ce3741)`,
2929

3030
/* sugar */
3131
'border': '1px solid #c64f4b',
@@ -42,18 +42,17 @@ const escMap = {
4242
'\\': '\\\\',
4343
};
4444

45-
function escFunc(m) {
45+
function escFunc (m) {
4646
return escMap[m] || '\\' + (m.charCodeAt(0) + 0x100000).toString(16).substr(1);
4747
}
4848

4949
const escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
5050

51-
function cssBrowserReporter(errors) {
52-
51+
function cssBrowserReporter (errors) {
5352
const content = [];
5453
let currFile;
5554
errors.forEach(error => {
56-
const pos = `[${ error.lineNumber || 1 }:${ error.columnNumber || 1 }]`;
55+
const pos = `[${error.lineNumber || 1}:${error.columnNumber || 1}]`;
5756
const subMsg = [
5857
error.plugin,
5958
error.rule,
@@ -65,13 +64,13 @@ function cssBrowserReporter(errors) {
6564
content.push(error.fileName);
6665
}
6766

68-
const icon = !error.demote && error.severity && icons[error.severity] || icons.success;
67+
const icon = (!error.demote && error.severity && icons[error.severity]) || icons.success;
6968

70-
content.push(`\t${ pos }${icon}${ error.message } (${ subMsg.join(' ') })`);
69+
content.push(`\t${pos}${icon}${error.message} (${subMsg.join(' ')})`);
7170
});
7271

7372
const css = ['', 'html::before {'];
74-
css.push.apply(css, Object.keys(styles).map(key => `\t${ key }: ${ styles[key] };`));
73+
css.push.apply(css, Object.keys(styles).map(key => `\t${key}: ${styles[key]};`));
7574
css.push('\tposition: sticky;', `\tcontent: "${
7675
content.join('\n').replace(escRE, escFunc)
7776
}";`, '}');

lib/csslint-error.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CSSLintError extends LintError {
1919
*
2020
* @memberOf CSSLintError
2121
*/
22-
constructor(error) {
22+
constructor (error) {
2323
const rule = error.rule || {};
2424
super({
2525
// 文件名
@@ -46,7 +46,6 @@ class CSSLintError extends LintError {
4646
// 错误ID
4747
rule: rule.id,
4848
});
49-
5049
}
5150
}
5251

lib/demote-errors.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @param {Object} options 配置
77
* @returns {void}
88
*/
9-
function demoteErrors(errors, options) {
9+
function demoteErrors (errors, options) {
1010
errors = errors.filter(error => error.blame && !/^0+$/.test(error.blame.hash));
1111
if (options._expiresTime) {
1212
errors = errors.filter(error => {
@@ -18,7 +18,7 @@ function demoteErrors(errors, options) {
1818
});
1919
}
2020

21-
function downMultiline(filter) {
21+
function downMultiline (filter) {
2222
errors.filter(filter).forEach(demoteError);
2323
}
2424

@@ -40,7 +40,7 @@ function demoteErrors(errors, options) {
4040
}
4141
}
4242

43-
function demoteError(error) {
43+
function demoteError (error) {
4444
error.demote = true;
4545
}
4646

lib/editorconfig-error.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class EditorConfigError extends LintError {
1414
*
1515
* @memberOf EditorConfigError
1616
*/
17-
constructor(error) {
17+
constructor (error) {
1818
super({
1919
// EditorConfig无警告,错误等级全部算错误
2020
severity: 'error',
@@ -23,7 +23,7 @@ class EditorConfigError extends LintError {
2323
plugin: 'EditorConfig',
2424

2525
// 文档
26-
doc: error.rule && `https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#${ error.rule }`,
26+
doc: error.rule && `https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#${error.rule}`,
2727
}, error);
2828
}
2929
}

lib/eslint-error.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ESLintError extends LintError {
1515
*
1616
* @memberOf ESLintError
1717
*/
18-
constructor(error) {
18+
constructor (error) {
1919
super({
2020
// 错误等级默认error,后面会覆盖
2121
severity: 'error',
@@ -39,7 +39,7 @@ class ESLintError extends LintError {
3939
plugin: 'ESLint',
4040

4141
// 文档
42-
doc: error.ruleId && `https://${ locale === 'zh_CN' ? 'cn.' : '' }eslint.org/docs/rules/${ error.ruleId }`,
42+
doc: error.ruleId && `https://${locale === 'zh_CN' ? 'cn.' : ''}eslint.org/docs/rules/${error.ruleId}`,
4343
}, error, {
4444
// 错误等级
4545
severity: (error.fatal || error.severity === 2) ? 'error' : 'warn',

lib/formatter.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ if (String.prototype.padStart) {
4444
};
4545
}
4646

47-
function justify(left, right, width) {
47+
function justify (left, right, width) {
4848
if (left) {
4949
width -= stringWidth(left);
5050
} else {
@@ -57,7 +57,7 @@ function justify(left, right, width) {
5757
}
5858
return left + ' '.repeat(Math.max(width, left && right ? 1 : 0)) + right;
5959
}
60-
function timeFormat(timestamp) {
60+
function timeFormat (timestamp) {
6161
timestamp = new Date(timestamp * 1000);
6262
return [
6363
timestamp.getFullYear(),
@@ -70,7 +70,7 @@ function timeFormat(timestamp) {
7070
].join(':');
7171
}
7272

73-
function highlight(error) {
73+
function highlight (error) {
7474
let source = error.source.replace(/\t/g, ' ');
7575
if (chalk.enabled) {
7676
const extname = path.extname(error.fileName).slice(1);
@@ -86,8 +86,8 @@ function highlight(error) {
8686
return source;
8787
}
8888

89-
function addLineNumbers(code, line, fmtOpts) {
90-
function add(s) {
89+
function addLineNumbers (code, line, fmtOpts) {
90+
function add (s) {
9191
let prefix = ' '.repeat(fmtOpts.columnNumberWidth + 1);
9292
prefix += chalk.yellow(padNum(line, fmtOpts.lineNumberWidth) + ' | ');
9393
line++;
@@ -96,7 +96,7 @@ function addLineNumbers(code, line, fmtOpts) {
9696
return code.replace(/^.*$/gm, add);
9797
}
9898

99-
function formatError(error, fmtOpts) {
99+
function formatError (error, fmtOpts) {
100100
const message = [];
101101
let severity = error.severity;
102102
if (error.demote || !severity || !icons[severity]) {
@@ -128,7 +128,7 @@ function formatError(error, fmtOpts) {
128128
subMsg.push(chalk.underline(link));
129129
}
130130
if (subMsg.length) {
131-
subMsg = `(${ subMsg.join(' ').trim() })`;
131+
subMsg = `(${subMsg.join(' ').trim()})`;
132132
if (stringWidth(mainMessage) + stringWidth(subMsg) >= fmtOpts.termColumns) {
133133
message.push(mainMessage, justify(
134134
null,
@@ -174,7 +174,7 @@ function formatError(error, fmtOpts) {
174174
)
175175
} ${
176176
justify(
177-
`<${ error.blame.author.mail }>`,
177+
`<${error.blame.author.mail}>`,
178178
null,
179179
fmtOpts.maxEmailWidth + 2
180180
)
@@ -198,7 +198,7 @@ function formatError(error, fmtOpts) {
198198
return message.join('\n');
199199
}
200200

201-
function formatter(file, options) {
201+
function formatter (file, options) {
202202
const sourceCache = {};
203203
let maxLineNumber = 0;
204204
let maxColumnNumber = 0;

lib/get-errors.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const linterMap = {
2020
function getErrors (file, options) {
2121
// 开始收集错误
2222
let errors = [];
23-
function addErrors(path, hook) {
23+
function addErrors (path, hook) {
2424
let errs = get(file, path);
25-
if (!errs || (hook && !(errs = hook(errs)) || !errs.length) ) {
25+
if (!errs || ((hook && !(errs = hook(errs))) || !errs.length)) {
2626
return;
2727
}
2828
path = path.replace(/^(\w+).*$/, (s, linter) => {
@@ -51,10 +51,10 @@ function getErrors (file, options) {
5151
// 获取git blame信息
5252

5353
return (options.blame ? gitBlame(file).catch(() => {
54-
return;
54+
5555
}) : Promise.resolve()).then(blames => {
5656
// blame信息写入error对象
57-
errors.forEach(function(error) {
57+
errors.forEach(function (error) {
5858
if (!error.fileName) {
5959
error.fileName = file.path;
6060
}

0 commit comments

Comments
 (0)