Unify near-identical implementations of MD049/emphasis-style and MD050/strong-style.

This commit is contained in:
David Anson 2022-05-03 21:59:49 -07:00
parent 76bc976d3b
commit 580b7ed020
4 changed files with 122 additions and 187 deletions

View file

@ -4411,88 +4411,55 @@ module.exports = {
// @ts-check // @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, emphasisOrStrongStyleFor = _a.emphasisOrStrongStyleFor, forEachInlineChild = _a.forEachInlineChild, getNextChildToken = _a.getNextChildToken, getRangeAndFixInfoIfFound = _a.getRangeAndFixInfoIfFound; var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, emphasisOrStrongStyleFor = _a.emphasisOrStrongStyleFor, forEachInlineChild = _a.forEachInlineChild, getNextChildToken = _a.getNextChildToken, getRangeAndFixInfoIfFound = _a.getRangeAndFixInfoIfFound;
module.exports = { var impl = function (params, onError, tagPrefix, asterisk, underline, style) {
"names": ["MD049", "emphasis-style"], var lastLineNumber = -1;
"description": "Emphasis style should be consistent", var instances = new Map();
"tags": ["emphasis"], forEachInlineChild(params, "".concat(tagPrefix, "_open"), function (token, parent) {
"function": function MD049(params, onError) { var lineNumber = token.lineNumber, markup = token.markup;
var expectedStyle = String(params.config.style || "consistent"); var markupStyle = emphasisOrStrongStyleFor(markup);
var lastLineNumber = -1; if (style === "consistent") {
var instances = new Map(); style = markupStyle;
forEachInlineChild(params, "em_open", function (token, parent) { }
var lineNumber = token.lineNumber, markup = token.markup; if (style !== markupStyle) {
var markupStyle = emphasisOrStrongStyleFor(markup); var rangeAndFixInfo = {};
if (expectedStyle === "consistent") { var contentToken = getNextChildToken(parent, token, "text", "".concat(tagPrefix, "_close"));
expectedStyle = markupStyle; if (contentToken) {
} var content = contentToken.content;
if (expectedStyle !== markupStyle) { var actual = "".concat(markup).concat(content).concat(markup);
var rangeAndFixInfo = {}; var expectedMarkup = (style === "asterisk") ? asterisk : underline;
var contentToken = getNextChildToken(parent, token, "text", "em_close"); var expected = "".concat(expectedMarkup).concat(content).concat(expectedMarkup);
if (contentToken) { if (lastLineNumber !== lineNumber) {
var content = contentToken.content; lastLineNumber = lineNumber;
var actual = "".concat(markup).concat(content).concat(markup); instances.clear();
var expectedMarkup = (expectedStyle === "asterisk") ? "*" : "_";
var expected = "".concat(expectedMarkup).concat(content).concat(expectedMarkup);
if (lastLineNumber !== lineNumber) {
lastLineNumber = lineNumber;
instances.clear();
}
instances.set(expected, (instances.get(expected) || 0) + 1);
rangeAndFixInfo = getRangeAndFixInfoIfFound(params.lines, lineNumber - 1, actual, expected, instances.get(expected));
} }
addError(onError, lineNumber, "Expected: ".concat(expectedStyle, "; Actual: ").concat(markupStyle), null, rangeAndFixInfo.range, rangeAndFixInfo.fixInfo); var instance = (instances.get(expected) || 0) + 1;
instances.set(expected, instance);
rangeAndFixInfo = getRangeAndFixInfoIfFound(params.lines, lineNumber - 1, actual, expected, instance);
} }
}); addError(onError, lineNumber, "Expected: ".concat(style, "; Actual: ").concat(markupStyle), null, rangeAndFixInfo.range, rangeAndFixInfo.fixInfo);
} }
});
}; };
module.exports = [
{
/***/ }), "names": ["MD049", "emphasis-style"],
"description": "Emphasis style should be consistent",
/***/ "../lib/md050.js": "tags": ["emphasis"],
/*!***********************!*\ "function": function MD049(params, onError) {
!*** ../lib/md050.js ***! var style = String(params.config.style || "consistent");
\***********************/ return impl(params, onError, "em", "*", "_", style);
/***/ ((module, __unused_webpack_exports, __webpack_require__) => { }
},
"use strict"; {
// @ts-check "names": ["MD050", "strong-style"],
"description": "Strong style should be consistent",
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, emphasisOrStrongStyleFor = _a.emphasisOrStrongStyleFor, forEachInlineChild = _a.forEachInlineChild, getNextChildToken = _a.getNextChildToken, getRangeAndFixInfoIfFound = _a.getRangeAndFixInfoIfFound; "tags": ["emphasis"],
module.exports = { "function": function MD050(params, onError) {
"names": ["MD050", "strong-style"], var style = String(params.config.style || "consistent");
"description": "Strong style should be consistent", return impl(params, onError, "strong", "**", "__", style);
"tags": ["emphasis"], }
"function": function MD050(params, onError) {
var expectedStyle = String(params.config.style || "consistent");
var lastLineNumber = -1;
var instances = new Map();
forEachInlineChild(params, "strong_open", function (token, parent) {
var lineNumber = token.lineNumber, markup = token.markup;
var markupStyle = emphasisOrStrongStyleFor(markup);
if (expectedStyle === "consistent") {
expectedStyle = markupStyle;
}
if (expectedStyle !== markupStyle) {
var rangeAndFixInfo = {};
var contentToken = getNextChildToken(parent, token, "text", "strong_close");
if (contentToken) {
var content = contentToken.content;
var actual = "".concat(markup).concat(content).concat(markup);
var expectedMarkup = (expectedStyle === "asterisk") ? "**" : "__";
var expected = "".concat(expectedMarkup).concat(content).concat(expectedMarkup);
if (lastLineNumber !== lineNumber) {
lastLineNumber = lineNumber;
instances.clear();
}
instances.set(expected, (instances.get(expected) || 0) + 1);
rangeAndFixInfo = getRangeAndFixInfoIfFound(params.lines, lineNumber - 1, actual, expected, instances.get(expected));
}
addError(onError, lineNumber, "Expected: ".concat(expectedStyle, "; Actual: ").concat(markupStyle), null, rangeAndFixInfo.range, rangeAndFixInfo.fixInfo);
}
});
} }
}; ];
/***/ }), /***/ }),
@ -4583,13 +4550,22 @@ module.exports = {
/*!***********************!*\ /*!***********************!*\
!*** ../lib/rules.js ***! !*** ../lib/rules.js ***!
\***********************/ \***********************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => { /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
"use strict"; "use strict";
// @ts-check // @ts-check
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var _a = __webpack_require__(/*! ./constants */ "../lib/constants.js"), homepage = _a.homepage, version = _a.version; var _a = __webpack_require__(/*! ./constants */ "../lib/constants.js"), homepage = _a.homepage, version = _a.version;
var rules = [ var rules = __spreadArray(__spreadArray([
__webpack_require__(/*! ./md001 */ "../lib/md001.js"), __webpack_require__(/*! ./md001 */ "../lib/md001.js"),
__webpack_require__(/*! ./md002 */ "../lib/md002.js"), __webpack_require__(/*! ./md002 */ "../lib/md002.js"),
__webpack_require__(/*! ./md003 */ "../lib/md003.js"), __webpack_require__(/*! ./md003 */ "../lib/md003.js"),
@ -4633,11 +4609,10 @@ var rules = [
__webpack_require__(/*! ./md045 */ "../lib/md045.js"), __webpack_require__(/*! ./md045 */ "../lib/md045.js"),
__webpack_require__(/*! ./md046 */ "../lib/md046.js"), __webpack_require__(/*! ./md046 */ "../lib/md046.js"),
__webpack_require__(/*! ./md047 */ "../lib/md047.js"), __webpack_require__(/*! ./md047 */ "../lib/md047.js"),
__webpack_require__(/*! ./md048 */ "../lib/md048.js"), __webpack_require__(/*! ./md048 */ "../lib/md048.js")
__webpack_require__(/*! ./md049-md050 */ "../lib/md049-md050.js"), ], __webpack_require__(/*! ./md049-md050 */ "../lib/md049-md050.js"), true), [
__webpack_require__(/*! ./md050 */ "../lib/md050.js"),
__webpack_require__(/*! ./md051 */ "../lib/md051.js") __webpack_require__(/*! ./md051 */ "../lib/md051.js")
]; ], false);
rules.forEach(function (rule) { rules.forEach(function (rule) {
var name = rule.names[0].toLowerCase(); var name = rule.names[0].toLowerCase();
// eslint-disable-next-line dot-notation // eslint-disable-next-line dot-notation

View file

@ -5,52 +5,69 @@
const { addError, emphasisOrStrongStyleFor, forEachInlineChild, const { addError, emphasisOrStrongStyleFor, forEachInlineChild,
getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers"); getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers");
module.exports = { const impl = (params, onError, tagPrefix, asterisk, underline, style) => {
"names": [ "MD049", "emphasis-style" ], let lastLineNumber = -1;
"description": "Emphasis style should be consistent", const instances = new Map();
"tags": [ "emphasis" ], forEachInlineChild(params, `${tagPrefix}_open`, (token, parent) => {
"function": function MD049(params, onError) { const { lineNumber, markup } = token;
let expectedStyle = String(params.config.style || "consistent"); const markupStyle = emphasisOrStrongStyleFor(markup);
let lastLineNumber = -1; if (style === "consistent") {
const instances = new Map(); style = markupStyle;
forEachInlineChild(params, "em_open", (token, parent) => { }
const { lineNumber, markup } = token; if (style !== markupStyle) {
const markupStyle = emphasisOrStrongStyleFor(markup); let rangeAndFixInfo = {};
if (expectedStyle === "consistent") { const contentToken = getNextChildToken(
expectedStyle = markupStyle; parent, token, "text", `${tagPrefix}_close`
} );
if (expectedStyle !== markupStyle) { if (contentToken) {
let rangeAndFixInfo = {}; const { content } = contentToken;
const contentToken = getNextChildToken( const actual = `${markup}${content}${markup}`;
parent, token, "text", "em_close" const expectedMarkup =
); (style === "asterisk") ? asterisk : underline;
if (contentToken) { const expected = `${expectedMarkup}${content}${expectedMarkup}`;
const { content } = contentToken; if (lastLineNumber !== lineNumber) {
const actual = `${markup}${content}${markup}`; lastLineNumber = lineNumber;
const expectedMarkup = (expectedStyle === "asterisk") ? "*" : "_"; instances.clear();
const expected = `${expectedMarkup}${content}${expectedMarkup}`;
if (lastLineNumber !== lineNumber) {
lastLineNumber = lineNumber;
instances.clear();
}
instances.set(expected, (instances.get(expected) || 0) + 1);
rangeAndFixInfo = getRangeAndFixInfoIfFound(
params.lines,
lineNumber - 1,
actual,
expected,
instances.get(expected)
);
} }
addError( const instance = (instances.get(expected) || 0) + 1;
onError, instances.set(expected, instance);
lineNumber, rangeAndFixInfo = getRangeAndFixInfoIfFound(
`Expected: ${expectedStyle}; Actual: ${markupStyle}`, params.lines,
null, lineNumber - 1,
rangeAndFixInfo.range, actual,
rangeAndFixInfo.fixInfo expected,
instance
); );
} }
}); addError(
} onError,
lineNumber,
`Expected: ${style}; Actual: ${markupStyle}`,
null,
rangeAndFixInfo.range,
rangeAndFixInfo.fixInfo
);
}
});
}; };
module.exports = [
{
"names": [ "MD049", "emphasis-style" ],
"description": "Emphasis style should be consistent",
"tags": [ "emphasis" ],
"function": function MD049(params, onError) {
const style = String(params.config.style || "consistent");
return impl(params, onError, "em", "*", "_", style);
}
},
{
"names": [ "MD050", "strong-style" ],
"description": "Strong style should be consistent",
"tags": [ "emphasis" ],
"function": function MD050(params, onError) {
const style = String(params.config.style || "consistent");
return impl(params, onError, "strong", "**", "__", style);
}
}
];

View file

@ -1,56 +0,0 @@
// @ts-check
"use strict";
const { addError, emphasisOrStrongStyleFor, forEachInlineChild,
getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers");
module.exports = {
"names": [ "MD050", "strong-style" ],
"description": "Strong style should be consistent",
"tags": [ "emphasis" ],
"function": function MD050(params, onError) {
let expectedStyle = String(params.config.style || "consistent");
let lastLineNumber = -1;
const instances = new Map();
forEachInlineChild(params, "strong_open", (token, parent) => {
const { lineNumber, markup } = token;
const markupStyle = emphasisOrStrongStyleFor(markup);
if (expectedStyle === "consistent") {
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}`;
if (lastLineNumber !== lineNumber) {
lastLineNumber = lineNumber;
instances.clear();
}
instances.set(expected, (instances.get(expected) || 0) + 1);
rangeAndFixInfo = getRangeAndFixInfoIfFound(
params.lines,
lineNumber - 1,
actual,
expected,
instances.get(expected)
);
}
addError(
onError,
lineNumber,
`Expected: ${expectedStyle}; Actual: ${markupStyle}`,
null,
rangeAndFixInfo.range,
rangeAndFixInfo.fixInfo
);
}
});
}
};

View file

@ -49,8 +49,7 @@ const rules = [
require("./md046"), require("./md046"),
require("./md047"), require("./md047"),
require("./md048"), require("./md048"),
require("./md049-md050"), ...require("./md049-md050"),
require("./md050"),
require("./md051") require("./md051")
]; ];
rules.forEach((rule) => { rules.forEach((rule) => {