Skip to content

Commit

Permalink
Merge pull request #115 from reviewdog/stylelint-format-message-text
Browse files Browse the repository at this point in the history
Stylelint Format Message Text
  • Loading branch information
haya14busa authored Feb 6, 2024
2 parents a263144 + 2a5cce8 commit 5a62a0c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion stylelint-formatter-rdjson/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
// https://github.com/reviewdog/reviewdog/tree/7ab09a1158ed4abd0eb0395ab0f1af6cfcdf513e/proto/rdf#rdjson
// https://stylelint.io/developer-guide/formatters

/**
* @param {import('stylelint').Warning} message
* @returns string
* @see https://github.com/stylelint/stylelint/blob/224b280135c6188a061061d0f9ee1042f5cd345a/lib/formatters/stringFormatter.cjs#L173-L188
*/
function formatMessageText(warning) {
let result = warning.text;

// Remove all control characters (newline, tab and etc)
result = result
.replace(/[\u0001-\u001A]+/g, ' ')
.replace(/\.$/, '');

const ruleString = ` (${warning.rule})`;

if (result.endsWith(ruleString)) {
result = result.slice(0, result.lastIndexOf(ruleString));
}

return result;
}

/**
* @type {import('stylelint').Formatter}
*/
module.exports = function (results, returnValue) {
const rdjson = {
source: {
Expand All @@ -16,7 +41,7 @@ module.exports = function (results, returnValue) {

result.warnings.forEach(warning => {
const diagnostic = {
message: warning.text,
message: formatMessageText(warning),
location: {
path: filePath,
range: {
Expand Down

0 comments on commit 5a62a0c

Please sign in to comment.