mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
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:
parent
a508824b0f
commit
291597edb9
13 changed files with 263 additions and 41 deletions
35
lib/md049.js
35
lib/md049.js
|
|
@ -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
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
35
lib/md050.js
35
lib/md050.js
|
|
@ -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
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue