Skip to content

Commit

Permalink
Add fixable status to verbose and github formatters (#6183)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jun 29, 2022
1 parent 3f47439 commit e70c93d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/formatters/__tests__/githubFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ test('githubFormatter', () => {
const returnValue = {
ruleMetadata: {
foo: { url: 'https://stylelint.io/rules/foo' },
bar: {},
bar: { fixable: true },
},
};

expect(githubFormatter(results, returnValue))
.toBe(`::error file=path/to/file.css,line=1,col=2,endLine=1,endColumn=5,title=Stylelint problem::Unexpected "foo" (foo) - https://stylelint.io/rules/foo
::warning file=a.css,line=10,col=20,title=Stylelint problem::Unexpected "bar" (bar)`);
::warning file=a.css,line=10,col=20,title=Stylelint problem::Unexpected "bar" (bar) [maybe fixable]`);
});
4 changes: 2 additions & 2 deletions lib/formatters/__tests__/prepareFormatterOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ symbolConversions.set('✔', '√');
symbolConversions.set('⚠', '‼');
symbolConversions.set('✖', '×');

module.exports = function (results, formatter) {
const returnValue = {
module.exports = function (results, formatter, returnValue) {
returnValue = returnValue || {
ruleMetadata: {},
};
let output = stripAnsi(formatter(results, returnValue)).trim();
Expand Down
18 changes: 9 additions & 9 deletions lib/formatters/__tests__/verboseFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,15 @@ describe('verboseFormatter', () => {
],
deprecations: [],
invalidOptionWarnings: [],
_postcssResult: {
stylelint: {
ruleMetadata: {
'no-foo': { url: 'https://stylelint.io' },
},
},
},
},
];
const returnValue = {
ruleMetadata: {
'no-foo': { url: 'https://stylelint.io', fixable: true },
},
};

const output = prepareFormatterOutput(results, verboseFormatter);
const output = prepareFormatterOutput(results, verboseFormatter, returnValue);

expect(output).toBe(stripIndent`
file.css
Expand All @@ -362,6 +360,8 @@ describe('verboseFormatter', () => {
file.css
1 error found
no-foo: 1`);
no-foo: 1 (maybe fixable)
You may fix some problems with the "--fix" option.`);
});
});
5 changes: 3 additions & 2 deletions lib/formatters/githubFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports = function githubFormatter(results, returnValue) {
function buildMessage(msg, metadata) {
if (!metadata) return msg;

if (!metadata.url) return msg;
const url = metadata.url ? ` - ${metadata.url}` : '';
const fixable = metadata.fixable ? ' [maybe fixable]' : '';

return `${msg} - ${metadata.url}`;
return `${msg}${fixable}${url}`;
}
14 changes: 13 additions & 1 deletion lib/formatters/verboseFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = function verboseFormatter(results, returnValue) {
output += '\n0 problems found\n';
} else {
const warningsBySeverity = groupBy(warnings, (w) => w.severity);
let fixableProblemsFound = false;

/**
* @param {Severity} severity
Expand All @@ -71,12 +72,23 @@ module.exports = function verboseFormatter(results, returnValue) {
const metadata = returnValue.ruleMetadata;

for (const [rule, list] of Object.entries(problemsByRule)) {
output += dim(` ${ruleLink(rule, metadata[rule])}: ${list.length}\n`);
const meta = metadata[rule];
const fixable = meta && meta.fixable ? ' (maybe fixable)' : '';

output += dim(` ${ruleLink(rule, meta)}: ${list.length}${fixable}\n`);

if (!fixableProblemsFound && meta && meta.fixable) {
fixableProblemsFound = true;
}
}
};

printProblems('error');
printProblems('warning');

if (fixableProblemsFound) {
output += yellow('\nYou may fix some problems with the "--fix" option.\n');
}
}

return `${output}\n`;
Expand Down
3 changes: 2 additions & 1 deletion scripts/visual-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"message": "Expected custom property name to be kebab-case",
"severity": "warning"
}
]
],
"import-notation": "string"
}
}
2 changes: 2 additions & 0 deletions scripts/visual.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url("foo.css");

.foo {
color:#4f;
}
Expand Down

0 comments on commit e70c93d

Please sign in to comment.