Skip to content

Commit

Permalink
Add support of eslint plugin (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Dec 27, 2017
1 parent fda762e commit b2df38d
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 913 deletions.
11 changes: 10 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ jobs:
when: always
- store_artifacts:
path: coverage
prefix: coverage
destination: coverage reports
- run:
name: Creating Artifacts
command: |
mkdir /tmp/json_artifacts;
cp lib/*.json /tmp/json_artifacts/;
when: always
- store_artifacts:
path: /tmp/json_artifacts
destination: short url
- store_test_results:
path: ~/lint-reports
37 changes: 36 additions & 1 deletion lib/eslint-error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
'use strict';
const LintError = require('./lint-error');
const locale = require('./locale');
const pluginDocBaseUrl = {
import: rule => `https://github.com/benmosher/eslint-plugin-import/blob/HEAD/docs/rules/${rule}.md#readme`,
node: rule => `https://github.com/mysticatea/eslint-plugin-node/blob/HEAD/docs/rules/${rule}.md#readme`,
promise: 'https://www.npmjs.com/package/eslint-plugin-promise#',
standard: 'https://www.npmjs.com/package/eslint-plugin-standard#rules-explanations',
compat: (rule, error) => (
rule === 'compat' && error.message.replace(
/^(?:[a-z]+\.)?(\w+).*$/,
(s, keyWord) => (
'https://www.caniuse.com/#search=' + keyWord.toLowerCase()
)
)
),
};

/**
* ESLint错误对象
*
Expand Down Expand Up @@ -39,12 +54,32 @@ class ESLintError extends LintError {
plugin: 'ESLint',

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

function docUrl (rule, error) {
if (!rule) {
return;
}
let baseUrl;
if (/^(.+?)\/(.+)$/.test(rule)) {
rule = RegExp.$2;
baseUrl = pluginDocBaseUrl[RegExp.$1];
} else {
return `https://${locale === 'zh_CN' ? 'cn.' : ''}eslint.org/docs/rules/${rule}`;
}
if (baseUrl) {
if (typeof baseUrl == 'function') {
return baseUrl(rule, error);
} else {
return baseUrl.replace(/(#)$/, '$1' + rule);
}
}
}

module.exports = ESLintError;
6 changes: 3 additions & 3 deletions lib/short-doc-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function shortDocUrl (error) {
}

function shortUrl (url) {
url = url.toLowerCase();
if (shorturlCache[url]) {
return Promise.resolve(shorturlCache[url]);
const urlLowerCase = url.toLowerCase();
if (shorturlCache[urlLowerCase]) {
return Promise.resolve(shorturlCache[urlLowerCase]);
}

if (!inGFW) {
Expand Down
681 changes: 231 additions & 450 deletions lib/shorturl.json

Large diffs are not rendered by default.

681 changes: 231 additions & 450 deletions lib/shorturl_cn.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"eclint": "^2.5.0",
"eslint": "^4.12.0",
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-plugin-compat": "^2.1.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
Expand Down
61 changes: 53 additions & 8 deletions scripts/shorturl.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

// process.env.HTTP_PROXY=http://127.0.0.1:1080/
const JSDOM = require('jsdom').JSDOM;
const stringify = require('json-stable-stringify');
const fs = require('fs-extra');
const got = require('got');
const googl = require('goo.gl');
const isCI = require('ci-info').isCI;
// Set a developer key (_required by Google_; see http://goo.gl/4DvFk for more info.)
googl.setKey('AIzaSyACqNSi3cybDvDfWMaPyXZEzQ6IeaPehLE');

Expand All @@ -21,6 +22,9 @@ function shortUrlCn (url) {
}

function get (url, selector) {
if (!isCI) {
return Promise.resolve([]);
}
return JSDOM.fromURL(url, {
referrer: url,
}).then(dom => {
Expand All @@ -40,14 +44,15 @@ function updateFile (file, urls, shortUrlFn) {
// hasChange = true;
// }
// }),
Promise.all(urls.map(url => (
shorturlCache[url] || shortUrlFn(url).then(shortUrl => {
Promise.all(urls.map(url => {
const urlLowerCase = url.toLowerCase();
return shorturlCache[urlLowerCase] || shortUrlFn(url).then(shortUrl => {
if (shortUrl) {
hasChange = true;
shorturlCache[url] = shortUrl;
shorturlCache[urlLowerCase] = shortUrl;
}
}).catch(console.error)
))).then(() => (
}).catch(console.error);
})).then(() => (
hasChange && fs.writeFile(
file,
stringify(
Expand All @@ -62,7 +67,17 @@ function updateFile (file, urls, shortUrlFn) {
));
}

const eslintRules = Object.keys(require('eslint/lib/load-rules')());
const eslintRules = Object.keys(
require('eslint/lib/load-rules')()
).map(
rule => rule.toLowerCase()
);

const EslintPluginDocBaseUrl = {
import: rule => `https://github.com/benmosher/eslint-plugin-import/blob/HEAD/docs/rules/${rule}.md#readme`,
node: rule => `https://github.com/mysticatea/eslint-plugin-node/blob/HEAD/docs/rules/${rule}.md#readme`,
promise: rule => `https://www.npmjs.com/package/eslint-plugin-promise#${rule}`,
};

Promise.all([
get('https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties', '.markdown-body h3 a[href^="#"]'),
Expand All @@ -88,6 +103,32 @@ Promise.all([
`https://eslint.org/docs/rules/${rule}`
)),

// eslint-plugin-standard
'https://www.npmjs.com/package/eslint-plugin-standard#rules-explanations',

// eslint-plugin-compat
[
'serviceworker',
'intersectionobserver',
'webassembly',
'paymentrequest',
'serviceworker',
'fetch',
'promise',
].map(s => 'https://www.caniuse.com/#search=' + s),

// ESLint plugins
fs.readJSON('package.json').then(
pkg => Object.keys(pkg.devDependencies).filter(
pkgName => /^eslint-plugin-/.test(pkgName)
).map(pkgName => {
const baseUrl = EslintPluginDocBaseUrl[pkgName.slice(14)];
if (baseUrl) {
return Object.keys(require(pkgName).rules).map(baseUrl);
}
})
),

// JSCS
fs.readdir('node_modules/jscs/lib/rules').then(files => (
files.filter(file => /\.js$/.test(file)).map(rule => (
Expand Down Expand Up @@ -119,7 +160,11 @@ Promise.all([
`https://github.com/yaniswang/HTMLHint/wiki/${rule}`
)),
]).then(urls => {
urls = [].concat.apply([], urls).filter(Boolean).map(url => url.toLowerCase());
do {
urls = [].concat.apply([], urls);
} while (urls.some(Array.isArray));

urls = urls.filter(Boolean);

return Promise.all([
updateFile(
Expand Down
31 changes: 31 additions & 0 deletions test/eslint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,35 @@ describe('ESLint', () => {
done();
});
});
it('eslint-config-standard', done => {
return vfs.src('test/fixtures/eslint/standard.js', {
base: process.cwd(),
})
.pipe(eslint({
configFile: path.resolve('test/fixtures/eslint/standard.json'),
}))
.pipe(reporter({
output: false,
blame: false,
}))
.on('data', file => {
try {
file.report.errors.forEach(error => {
if (/^(.+?)\//.test(error.rule)) {
if (RegExp.$1 === 'compat') {
assert.ok(error.doc.startsWith('https://www.caniuse.com/#search='));
assert.ok(/search=[a-z]+$/.test(error.doc));
} else {
const packageName = 'eslint-plugin-' + RegExp.$1;
assert.ok(error.doc.indexOf(packageName) > 1);
}
}
});
} catch (ex) {
done(ex);
return;
}
done();
});
});
});
25 changes: 25 additions & 0 deletions test/fixtures/eslint/standard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as path from 'path'
path.join('a', 'b')
import * as fs from 'fs'

export function readFile (file) {
return new Promise(function (ok, fail) {
fs.readFile(file, function (error, data) {
if (error) {
fail(error)
} else {
ok(data)
}
})
})
}

export function exists (file) {
return new Promise(function (resolve) {
fs.exists('./foo.js', resolve)
})
}

function test (cb) {
cb(undefined, 'snork')
}
20 changes: 20 additions & 0 deletions test/fixtures/eslint/standard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"root": true,
"env": {
"browser": true
},
"extends": [
"standard"
],
"plugins": ["compat"],
"rules": {
"compat/compat": [
"error"
]
},
"settings": {
"browsers": [
"ie 8"
]
}
}

0 comments on commit b2df38d

Please sign in to comment.