Skip to content

Commit

Permalink
Merge 1c7a067 into 5b5571c
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcgrtz committed Apr 1, 2023
2 parents 5b5571c + 1c7a067 commit 37b27db
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
22 changes: 20 additions & 2 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ const logSymbols = require('log-symbols');
const plur = require('plur');
const stringWidth = require('string-width');
const ansiEscapes = require('ansi-escapes');
const {supportsHyperlink} = require('supports-hyperlinks');

/**
* @param {string|undefined} rule Stylelint rule
* @param {import('stylelint').LinterResult} linterResult Linter result
* @returns {string|undefined} URL
*/
function getRuleUrl(rule, linterResult) {
let ruleUrl;

try {
ruleUrl = linterResult.ruleMetadata[rule].url;
} catch {}

return ruleUrl;
}

/**
* @type {import('stylelint').Formatter}
*/
function formatter(results) {
function formatter(results, returnValue) {
if (Array.isArray(results) && results.length > 0) {
const lines = [];
let errorCount = 0;
Expand Down Expand Up @@ -120,6 +136,7 @@ function formatter(results) {
message,
messageWidth,
ruleId: x.rule || '',
ruleUrl: getRuleUrl(x.rule, returnValue),
});
});
});
Expand Down Expand Up @@ -147,12 +164,13 @@ function formatter(results) {
}

if (x.type === 'message') {
const rule = (x.ruleUrl && supportsHyperlink(process.stdout) ? ansiEscapes.link(x.ruleId, x.ruleUrl) : x.ruleId);
const line = [
'',
x.severity === 'warning' ? logSymbols.warning : logSymbols.error,
' '.repeat(maxLineWidth - x.lineWidth) + pico.dim(x.line + pico.gray(':') + x.column),
' '.repeat(maxColumnWidth - x.columnWidth) + x.message,
' '.repeat(maxMessageWidth - x.messageWidth) + pico.gray(pico.dim(x.ruleId)),
' '.repeat(maxMessageWidth - x.messageWidth) + pico.gray(pico.dim(rule)),
];

if (!showLineNumbers) {
Expand Down
44 changes: 34 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"log-symbols": "~4.1.0",
"picocolors": "^1.0.0",
"plur": "~4.0.0",
"string-width": "~4.2.2"
"string-width": "~4.2.2",
"supports-hyperlinks": "^3.0.0"
},
"devDependencies": {
"ava": "^5.0.1",
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const fixture = [
rule: 'declaration-block-no-shorthand-property-overrides',
severity: 'error',
text: 'Unexpected shorthand "padding" after "padding-left" (declaration-block-no-shorthand-property-overrides)',
}, {
line: 8,
column: 13,
rule: undefined,
severity: 'error',
text: 'An undefined rule just for the fixture',
},
],
deprecations: [],
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/linter-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fixture = {
errored: true,
results: [],
output: '',
reportedDisables: [],
ruleMetadata: {
'number-leading-zero': {
url: 'https://stylelint.io/user-guide/rules/number-leading-zero',
fixable: true,
},
},
};

export default fixture;
29 changes: 29 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import process from 'node:process';
import test from 'ava';
import stripAnsi from 'strip-ansi';
import ansiEscapes from 'ansi-escapes';

import m from '../index.js';

import defaultFixture from './fixtures/default.js';
import deprecationsFixture from './fixtures/deprecations.js';
import invalidOptionsFixture from './fixtures/invalid-options.js';
import parseErrorFixture from './fixtures/parse-error.js';
import linterResultFixture from './fixtures/linter-result.js';

const enableHyperlinks = () => {
process.env.FORCE_HYPERLINK = '1';
};

const disableHyperlinks = () => {
process.env.FORCE_HYPERLINK = '0';
};

test('output', t => {
disableHyperlinks();
const output = m(defaultFixture);
t.regex(stripAnsi(output), /first\.css:3:12\n/);
t.regex(stripAnsi(output), /✖ {3}3:12 {2}Unexpected leading zero {26}number-leading-zero/);
});

test('deprecations', t => {
disableHyperlinks();
const output = m(deprecationsFixture);
t.regex(stripAnsi(output), /Stylelint Configuration\n/);
t.regex(stripAnsi(output), /ℹ time-no-imperceptible has been deprecated and in 8.0 will be removed. Instead use time-min-milliseconds with 100 as its primary option./);
Expand All @@ -23,6 +36,7 @@ test('deprecations', t => {
});

test('invalid options', t => {
disableHyperlinks();
const output = m(invalidOptionsFixture);
t.regex(stripAnsi(output), /Stylelint Configuration\n/);
t.regex(stripAnsi(output), /✖ Invalid option value snakeCase for rule value-keyword-case/);
Expand All @@ -31,18 +45,33 @@ test('invalid options', t => {
});

test('parse errors', t => {
disableHyperlinks();
const output = m(parseErrorFixture);
t.regex(stripAnsi(output), /first\.css:7:12\n/);
t.regex(stripAnsi(output), /✖ {2}7:12 {2}Unexpected token {2}parseError/);
t.regex(stripAnsi(output), /1 error/);
});

test('empty results', t => {
disableHyperlinks();
const output = m([]);
t.is(stripAnsi(output), '');
});

test('invalid Stylelint output', t => {
disableHyperlinks();
const output = m('foobar');
t.is(stripAnsi(output), '');
});

test('linked rules', t => {
enableHyperlinks();
const output = m(defaultFixture, linterResultFixture);
t.true(output.includes(ansiEscapes.link('number-leading-zero', 'https://stylelint.io/user-guide/rules/number-leading-zero')));
});

test('no errors when no URL is found to link a rule', t => {
enableHyperlinks();
const output = m(defaultFixture, linterResultFixture);
t.true(output.includes('number-no-trailing-zeros'));
});

0 comments on commit 37b27db

Please sign in to comment.