Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stylelint Format Message Text #115

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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