From a4a38ec9d2bd7db1763b9d99dff7588c24ac9680 Mon Sep 17 00:00:00 2001 From: David Anson Date: Thu, 25 May 2023 02:50:56 +0000 Subject: [PATCH] Refactor MD049/emphasis-style and MD050/strong-style slightly to simplify. --- demo/markdownlint-browser.js | 11 ++-- lib/md049-md050.js | 105 ++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index 78ffebdc..5f90707e 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -6092,7 +6092,8 @@ var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), forEachInlineChild = _require.forEachInlineChild, getNextChildToken = _require.getNextChildToken, getRangeAndFixInfoIfFound = _require.getRangeAndFixInfoIfFound; -var impl = function impl(params, onError, tagPrefix, asterisk, underline, style) { +var impl = function impl(params, onError, tagPrefix, asterisk, underline) { + var style = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : "consistent"; var lastLineNumber = -1; var instances = new Map(); forEachInlineChild(params, "".concat(tagPrefix, "_open"), function (token, parent) { @@ -6118,7 +6119,7 @@ var impl = function impl(params, onError, tagPrefix, asterisk, underline, style) 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); + addError(onError, lineNumber, "Expected: ".concat(style, "; Actual: ").concat(markupStyle), undefined, rangeAndFixInfo.range, rangeAndFixInfo.fixInfo); } }); }; @@ -6127,16 +6128,14 @@ module.exports = [{ "description": "Emphasis style should be consistent", "tags": ["emphasis"], "function": function MD049(params, onError) { - var style = String(params.config.style || "consistent"); - return impl(params, onError, "em", "*", "_", style); + return impl(params, onError, "em", "*", "_", params.config.style || undefined); } }, { "names": ["MD050", "strong-style"], "description": "Strong style should be consistent", "tags": ["emphasis"], "function": function MD050(params, onError) { - var style = String(params.config.style || "consistent"); - return impl(params, onError, "strong", "**", "__", style); + return impl(params, onError, "strong", "**", "__", params.config.style || undefined); } }]; diff --git a/lib/md049-md050.js b/lib/md049-md050.js index 43e64044..b8f3cd91 100644 --- a/lib/md049-md050.js +++ b/lib/md049-md050.js @@ -5,51 +5,52 @@ const { addError, emphasisOrStrongStyleFor, forEachInlineChild, getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers"); -const impl = (params, onError, tagPrefix, asterisk, underline, style) => { - let lastLineNumber = -1; - const instances = new Map(); - forEachInlineChild(params, `${tagPrefix}_open`, (token, parent) => { - const { lineNumber, markup } = token; - const markupStyle = emphasisOrStrongStyleFor(markup); - if (style === "consistent") { - style = markupStyle; - } - if (style !== markupStyle) { - let rangeAndFixInfo = {}; - const contentToken = getNextChildToken( - parent, token, "text", `${tagPrefix}_close` - ); - if (contentToken) { - const { content } = contentToken; - const actual = `${markup}${content}${markup}`; - const expectedMarkup = - (style === "asterisk") ? asterisk : underline; - const expected = `${expectedMarkup}${content}${expectedMarkup}`; - if (lastLineNumber !== lineNumber) { - lastLineNumber = lineNumber; - instances.clear(); +const impl = + (params, onError, tagPrefix, asterisk, underline, style = "consistent") => { + let lastLineNumber = -1; + const instances = new Map(); + forEachInlineChild(params, `${tagPrefix}_open`, (token, parent) => { + const { lineNumber, markup } = token; + const markupStyle = emphasisOrStrongStyleFor(markup); + if (style === "consistent") { + style = markupStyle; + } + if (style !== markupStyle) { + let rangeAndFixInfo = {}; + const contentToken = getNextChildToken( + parent, token, "text", `${tagPrefix}_close` + ); + if (contentToken) { + const { content } = contentToken; + const actual = `${markup}${content}${markup}`; + const expectedMarkup = + (style === "asterisk") ? asterisk : underline; + const expected = `${expectedMarkup}${content}${expectedMarkup}`; + if (lastLineNumber !== lineNumber) { + lastLineNumber = lineNumber; + instances.clear(); + } + const instance = (instances.get(expected) || 0) + 1; + instances.set(expected, instance); + rangeAndFixInfo = getRangeAndFixInfoIfFound( + params.lines, + lineNumber - 1, + actual, + expected, + instance + ); } - const instance = (instances.get(expected) || 0) + 1; - instances.set(expected, instance); - rangeAndFixInfo = getRangeAndFixInfoIfFound( - params.lines, - lineNumber - 1, - actual, - expected, - instance + addError( + onError, + lineNumber, + `Expected: ${style}; Actual: ${markupStyle}`, + undefined, + rangeAndFixInfo.range, + rangeAndFixInfo.fixInfo ); } - addError( - onError, - lineNumber, - `Expected: ${style}; Actual: ${markupStyle}`, - null, - rangeAndFixInfo.range, - rangeAndFixInfo.fixInfo - ); - } - }); -}; + }); + }; module.exports = [ { @@ -57,8 +58,14 @@ module.exports = [ "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); + return impl( + params, + onError, + "em", + "*", + "_", + params.config.style || undefined + ); } }, { @@ -66,8 +73,14 @@ module.exports = [ "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); + return impl( + params, + onError, + "strong", + "**", + "__", + params.config.style || undefined + ); } } ];