From 2a5cce89e30e1b2c445131e8e08db11dc40a160c Mon Sep 17 00:00:00 2001 From: Bert McCutchen Date: Tue, 6 Feb 2024 08:15:04 -0500 Subject: [PATCH] Added formatMessageText from Stylelint source --- stylelint-formatter-rdjson/index.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/stylelint-formatter-rdjson/index.js b/stylelint-formatter-rdjson/index.js index 4a404aa..cb18c4f 100644 --- a/stylelint-formatter-rdjson/index.js +++ b/stylelint-formatter-rdjson/index.js @@ -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: { @@ -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: {