diff --git a/lib/csslint-error.js b/lib/csslint-error.js index 6199488..15b69b3 100644 --- a/lib/csslint-error.js +++ b/lib/csslint-error.js @@ -39,7 +39,6 @@ class CssLintError extends LintError { // 报错插件 plugin: 'CssLint', - name: 'CssLint', // 文档 doc: rule.url, diff --git a/lib/editorconfig-error.js b/lib/editorconfig-error.js index c813bfd..31590ac 100644 --- a/lib/editorconfig-error.js +++ b/lib/editorconfig-error.js @@ -21,7 +21,6 @@ class EditorConfigError extends LintError { // 报错插件 plugin: 'EditorConfig', - name: 'EditorConfig', // 文档 doc: error.rule && `https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#${ error.rule }` diff --git a/lib/editorconfig_zh_CN.js b/lib/editorconfig_zh_CN.js new file mode 100644 index 0000000..a91e387 --- /dev/null +++ b/lib/editorconfig_zh_CN.js @@ -0,0 +1,37 @@ +'use strict'; + +const messages = new Map([ + [/^invalid charset: (.+?), expected: (.+?)$/, '错误的文件编码:$1,期望:$2'], + [/^expected charset: (.+?)$/, '期望的文件编码:$1'], + [/^character out of latin1 range: (.+?)$/, '超出latin1编码范围的字符:$1'], + + [/^invalid newline: (.+?), expected: (.+?)$/, '错误的缩进尺寸:$1,期望:$2'], + + [/^invalid indent size: (\d+), expected: (\d+)$/, '错误的缩进尺寸:$1,期望:$2'], + + ['invalid indent style: found a leading space, expected: tab', '错误的缩进风格:使用了空格,期望:tab'], + ['invalid indent style: found a leading tab, expected: space', '错误的缩进风格:使用了tab,期望:空格'], + [/^invalid indent style: found (\d+) soft tab\(s\)$/, '错误的缩进风格:发现$1个软缩进'], + [/^invalid indent style: found (\d+) hard tab\(s\)$/, '错误的缩进风格:发现$1个硬缩进'], + + ['expected final newline', '期望结尾换行'], + ['unexpected final newline', '不许结尾换行'], + + [/^invalid line length: (\d+), expected: (\d+)$/, '错误的行长度:$1,期望:$2以内'], + + ['trailing whitespace found', '行尾禁用空白'], +]); +module.exports = function(error) { + for (const [en, cn] of messages) { + if (en instanceof RegExp) { + if (en.test(error.message)) { + error.message = cn.replace(/\$\d+/, key => en[key]); + return error; + } + } else if (error.message === en) { + error.message = cn; + return error; + } + } + return error; +}; diff --git a/lib/eslint-error.js b/lib/eslint-error.js index 68f4190..c9851ca 100644 --- a/lib/eslint-error.js +++ b/lib/eslint-error.js @@ -41,7 +41,6 @@ class ESLintError extends LintError { // 报错插件 plugin: 'ESLint', - name: 'ESLint', // 文档 doc: error.ruleId && `http://${ locale === 'zh_CN' ? 'cn.' : '' }eslint.org/docs/rules/${ error.ruleId }` diff --git a/lib/jscs-error.js b/lib/jscs-error.js index a531870..06ca664 100644 --- a/lib/jscs-error.js +++ b/lib/jscs-error.js @@ -41,7 +41,6 @@ class JSCSError extends LintError { // 报错插件 plugin: 'JSCS', - name: 'JSCS', // 文档 doc: error.rule && `http://jscs.info/rule/${ error.rule }` diff --git a/lib/jshint-error.js b/lib/jshint-error.js index ef86a27..59326f3 100644 --- a/lib/jshint-error.js +++ b/lib/jshint-error.js @@ -39,7 +39,6 @@ class JSHintError extends LintError { // 报错插件 plugin: 'JSHint', - name: 'JSHint', }, error); } diff --git a/lib/lint-error.js b/lib/lint-error.js index 97d5ac9..9692feb 100644 --- a/lib/lint-error.js +++ b/lib/lint-error.js @@ -17,7 +17,7 @@ function i18n(error) { if (locale === 'en_US') { return error; } - const key = error.name.toLowerCase(); + const key = error.plugin.toLowerCase(); if (!(key in I18N_CACHE)) { try { I18N_CACHE[key] = require(`./${ key }_${ locale }`); diff --git a/lib/postcss-error.js b/lib/postcss-error.js index 329870f..897a5de 100644 --- a/lib/postcss-error.js +++ b/lib/postcss-error.js @@ -45,7 +45,6 @@ class PostCSSError extends LintError { // 报错插件 plugin: 'PostCSS', - name: 'PostCSS', doc: error.rule && error.plugin === 'stylelint' && `https://stylelint.io/user-guide/rules/${ error.rule }/` }, error, { // 错误等级 diff --git a/lib/tslint-error.js b/lib/tslint-error.js index 562ee47..ebc8c93 100644 --- a/lib/tslint-error.js +++ b/lib/tslint-error.js @@ -33,7 +33,6 @@ class TSLintError extends LintError { // 报错插件 plugin: 'TSLint', - name: 'TSLint', // 文档 doc: error.ruleName && `https://palantir.github.io/tslint/rules/${ error.ruleName }/` diff --git a/package.json b/package.json index 131c716..9d64503 100644 --- a/package.json +++ b/package.json @@ -68,5 +68,5 @@ "pretest": "node-version-gte-4 && eslint lib test/*.test.js || echo \"ESLint not supported\"", "test": "istanbul cover node_modules/mocha/bin/_mocha test/*.test.js" }, - "version": "1.7.0" + "version": "1.8.0" } diff --git a/test/eclint.test.js b/test/eclint.test.js index 945d8ca..d7c8177 100644 --- a/test/eclint.test.js +++ b/test/eclint.test.js @@ -22,11 +22,8 @@ describe('ECLint', function() { assert.equal(ex.plugin, 'gulp-reporter'); assert.equal(ex.message, 'Lint failed for: test/fixtures/eclint/invalid.js'); - assert.ok(gutil.log.lastCall.args[0].indexOf('invalid charset: utf-8-bom, expected: utf-8 (EditorConfig charset') >= 0); - assert.ok(gutil.log.lastCall.args[0].indexOf('https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#charset)') >= 0); - - assert.ok(gutil.log.lastCall.args[0].indexOf('invalid indentation: found a leading space, expected: tab') >= 0); - assert.ok(gutil.log.lastCall.args[0].indexOf('EditorConfig indent_style https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#indent_style') >= 0); + assert.ok(gutil.log.lastCall.args[0].indexOf('(EditorConfig charset https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#charset)') >= 0); + assert.ok(gutil.log.lastCall.args[0].indexOf('(EditorConfig indent_style https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#indent_style') >= 0); done(); });