Skip to content

Commit 7606f98

Browse files
Vadim Demedessindresorhus
Vadim Demedes
authored andcommitted
Linkify rules to ESLint documentation (#33)
Fixes #28
1 parent d686d07 commit 7606f98

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const logSymbols = require('log-symbols');
55
const plur = require('plur');
66
const stringWidth = require('string-width');
77
const ansiEscapes = require('ansi-escapes');
8+
const {supportsHyperlink} = require('supports-hyperlinks');
9+
const getRuleUrl = require('eslint-rule-docs');
810

911
module.exports = results => {
1012
const lines = [];
@@ -107,7 +109,8 @@ module.exports = results => {
107109
x.severity === 'warning' ? logSymbols.warning : logSymbols.error,
108110
' '.repeat(maxLineWidth - x.lineWidth) + chalk.dim(x.line + chalk.gray(':') + x.column),
109111
' '.repeat(maxColumnWidth - x.columnWidth) + x.message,
110-
' '.repeat(maxMessageWidth - x.messageWidth) + chalk.gray.dim(x.ruleId)
112+
' '.repeat(maxMessageWidth - x.messageWidth) +
113+
(supportsHyperlink(process.stdout) ? ansiEscapes.link(chalk.dim(x.ruleId), getRuleUrl(x.ruleId).url) : chalk.gray.dim(x.ruleId))
111114
];
112115

113116
if (!showLineNumbers) {

package.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@
2929
"dependencies": {
3030
"ansi-escapes": "^3.0.0",
3131
"chalk": "^2.1.0",
32+
"eslint-rule-docs": "^1.1.5",
3233
"log-symbols": "^2.0.0",
3334
"plur": "^2.1.2",
34-
"string-width": "^2.0.0"
35+
"string-width": "^2.0.0",
36+
"supports-hyperlinks": "^1.0.1"
3537
},
3638
"devDependencies": {
3739
"ava": "*",
3840
"strip-ansi": "^4.0.0",
3941
"xo": "*"
42+
},
43+
"ava": {
44+
"serial": true
45+
},
46+
"xo": {
47+
"overrides": [
48+
{
49+
"files": "index.js",
50+
"rules": {
51+
"prefer-destructuring": 0,
52+
"no-else-return": 1
53+
}
54+
}
55+
]
4056
}
4157
}

test/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
11
import test from 'ava';
22
import stripAnsi from 'strip-ansi';
3+
import ansiEscapes from 'ansi-escapes';
4+
import chalk from 'chalk';
35
import m from '..';
46
import defaultFixture from './fixtures/default';
57
import noLineNumbers from './fixtures/no-line-numbers';
68
import lineNumbers from './fixtures/line-numbers';
79
import sortOrder from './fixtures/sort-by-severity-then-line-then-column';
810

11+
const enableHyperlinks = () => {
12+
process.env.FORCE_HYPERLINK = '1';
13+
};
14+
15+
const disableHyperlinks = () => {
16+
process.env.FORCE_HYPERLINK = '0';
17+
};
18+
919
test('output', t => {
20+
disableHyperlinks();
1021
const output = m(defaultFixture);
1122
console.log(output);
1223
t.regex(stripAnsi(output), /index\.js:8:2\n/);
1324
t.regex(stripAnsi(output), /[ ]{3}1:1[ ]{2}AVA should be imported as test.[ ]{6}ava\/use-test/);
1425
});
1526

1627
test('no line numbers', t => {
28+
disableHyperlinks();
1729
const output = m(noLineNumbers);
1830
console.log(output);
1931
t.regex(stripAnsi(output), /index\.js\n/);
2032
t.regex(stripAnsi(output), /[ ]{2}AVA should be imported as test.[ ]{6}ava\/use-test/);
2133
});
2234

2335
test('show line numbers', t => {
36+
disableHyperlinks();
2437
const output = m(lineNumbers);
2538
console.log(output);
2639
t.regex(stripAnsi(output), /[ ]{3}0:0[ ]{2}Unexpected todo comment.[ ]{13}no-warning-comments/);
2740
t.regex(stripAnsi(output), /[ ]{3}1:1[ ]{2}AVA should be imported as test.[ ]{6}ava\/use-test/);
2841
});
2942

43+
test('link rules to documentation when terminal supports links', t => {
44+
enableHyperlinks();
45+
const output = m(defaultFixture);
46+
console.log(output);
47+
t.true(output.includes(ansiEscapes.link(chalk.dim('no-warning-comments'), 'https://eslint.org/docs/rules/no-warning-comments')));
48+
});
49+
3050
test('sort by severity, then line number, then column number', t => {
51+
disableHyperlinks();
3152
const output = m(sortOrder);
3253
const sanitized = stripAnsi(output);
3354
const indexes = [
@@ -43,6 +64,7 @@ test('sort by severity, then line number, then column number', t => {
4364
});
4465

4566
test('display warning total before error total', t => {
67+
disableHyperlinks();
4668
const output = m(sortOrder);
4769
const sanitized = stripAnsi(output);
4870
const indexes = [

0 commit comments

Comments
 (0)