Update rules MD049/emphasis-style and MD050/strong-style to include range and fixInfo when reporting issues (i.e., to be automatically fixable).

This commit is contained in:
David Anson 2021-11-28 23:18:57 -08:00
parent a508824b0f
commit 291597edb9
13 changed files with 263 additions and 41 deletions

View file

@ -2,8 +2,8 @@
"use strict";
const { addErrorDetailIf, emphasisOrStrongStyleFor, forEachInlineChild } =
require("../helpers");
const { addError, emphasisOrStrongStyleFor, forEachInlineChild,
getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers");
module.exports = {
"names": [ "MD049", "emphasis-style" ],
@ -11,18 +11,35 @@ module.exports = {
"tags": [ "emphasis" ],
"function": function MD049(params, onError) {
let expectedStyle = String(params.config.style || "consistent");
forEachInlineChild(params, "em_open", (token) => {
forEachInlineChild(params, "em_open", (token, parent) => {
const { lineNumber, markup } = token;
const markupStyle = emphasisOrStrongStyleFor(markup);
if (expectedStyle === "consistent") {
expectedStyle = markupStyle;
}
addErrorDetailIf(
onError,
lineNumber,
expectedStyle,
markupStyle
);
if (expectedStyle !== markupStyle) {
let rangeAndFixInfo = {};
const contentToken = getNextChildToken(
parent, token, "text", "em_close"
);
if (contentToken) {
const { content } = contentToken;
const actual = `${markup}${content}${markup}`;
const expectedMarkup = (expectedStyle === "asterisk") ? "*" : "_";
const expected = `${expectedMarkup}${content}${expectedMarkup}`;
rangeAndFixInfo = getRangeAndFixInfoIfFound(
params.lines, lineNumber - 1, actual, expected
);
}
addError(
onError,
lineNumber,
`Expected: ${expectedStyle}; Actual: ${markupStyle}`,
null,
rangeAndFixInfo.range,
rangeAndFixInfo.fixInfo
);
}
});
}
};

View file

@ -2,8 +2,8 @@
"use strict";
const { addErrorDetailIf, emphasisOrStrongStyleFor, forEachInlineChild } =
require("../helpers");
const { addError, emphasisOrStrongStyleFor, forEachInlineChild,
getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers");
module.exports = {
"names": [ "MD050", "strong-style" ],
@ -11,18 +11,35 @@ module.exports = {
"tags": [ "emphasis" ],
"function": function MD050(params, onError) {
let expectedStyle = String(params.config.style || "consistent");
forEachInlineChild(params, "strong_open", (token) => {
forEachInlineChild(params, "strong_open", (token, parent) => {
const { lineNumber, markup } = token;
const markupStyle = emphasisOrStrongStyleFor(markup);
if (expectedStyle === "consistent") {
expectedStyle = markupStyle;
}
addErrorDetailIf(
onError,
lineNumber,
expectedStyle,
markupStyle
);
if (expectedStyle !== markupStyle) {
let rangeAndFixInfo = {};
const contentToken = getNextChildToken(
parent, token, "text", "strong_close"
);
if (contentToken) {
const { content } = contentToken;
const actual = `${markup}${content}${markup}`;
const expectedMarkup = (expectedStyle === "asterisk") ? "**" : "__";
const expected = `${expectedMarkup}${content}${expectedMarkup}`;
rangeAndFixInfo = getRangeAndFixInfoIfFound(
params.lines, lineNumber - 1, actual, expected
);
}
addError(
onError,
lineNumber,
`Expected: ${expectedStyle}; Actual: ${markupStyle}`,
null,
rangeAndFixInfo.range,
rangeAndFixInfo.fixInfo
);
}
});
}
};