From 1f9497ad09cb7f0b4d66b36ba2db012a53019cfe Mon Sep 17 00:00:00 2001 From: David Anson Date: Thu, 16 Jun 2022 05:19:27 +0000 Subject: [PATCH] Refactor MD009/no-trailing-spaces to remove dependency on helpers.forEachInlineCodeSpan. --- demo/markdownlint-browser.js | 27 ++++++++++++++++++--------- lib/md009.js | 29 +++++++++++++++++++---------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index 2110ac35..02830236 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -2644,7 +2644,7 @@ module.exports = { "use strict"; // @ts-check -const { addError, filterTokens, forEachInlineCodeSpan, forEachLine, includesSorted, newLineRe, numericSortAscending } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); +const { addError, filterTokens, forEachLine, includesSorted, numericSortAscending } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); module.exports = { "names": ["MD009", "no-trailing-spaces"], @@ -2672,15 +2672,24 @@ module.exports = { paragraphLineNumbers.push(i + 1); } }); + const addLineNumberRange = (start, end) => { + for (let i = start; i < end; i++) { + codeInlineLineNumbers.push(i); + } + }; filterTokens(params, "inline", (token) => { - if (token.children.some((child) => child.type === "code_inline")) { - const tokenLines = params.lines.slice(token.map[0], token.map[1]); - forEachInlineCodeSpan(tokenLines.join("\n"), (code, lineIndex) => { - const codeLineCount = code.split(newLineRe).length; - for (let i = 0; i < codeLineCount - 1; i++) { - codeInlineLineNumbers.push(token.lineNumber + lineIndex + i); - } - }); + let start = 0; + for (const child of token.children) { + if (start > 0) { + addLineNumberRange(start, child.lineNumber); + start = 0; + } + if (child.type === "code_inline") { + start = child.lineNumber; + } + } + if (start > 0) { + addLineNumberRange(start, token.map[1]); } }); } diff --git a/lib/md009.js b/lib/md009.js index 7436ada0..b6581e2d 100644 --- a/lib/md009.js +++ b/lib/md009.js @@ -2,8 +2,8 @@ "use strict"; -const { addError, filterTokens, forEachInlineCodeSpan, forEachLine, - includesSorted, newLineRe, numericSortAscending } = require("../helpers"); +const { addError, filterTokens, forEachLine, includesSorted, + numericSortAscending } = require("../helpers"); const { lineMetadata } = require("./cache"); module.exports = { @@ -32,15 +32,24 @@ module.exports = { paragraphLineNumbers.push(i + 1); } }); + const addLineNumberRange = (start, end) => { + for (let i = start; i < end; i++) { + codeInlineLineNumbers.push(i); + } + }; filterTokens(params, "inline", (token) => { - if (token.children.some((child) => child.type === "code_inline")) { - const tokenLines = params.lines.slice(token.map[0], token.map[1]); - forEachInlineCodeSpan(tokenLines.join("\n"), (code, lineIndex) => { - const codeLineCount = code.split(newLineRe).length; - for (let i = 0; i < codeLineCount - 1; i++) { - codeInlineLineNumbers.push(token.lineNumber + lineIndex + i); - } - }); + let start = 0; + for (const child of token.children) { + if (start > 0) { + addLineNumberRange(start, child.lineNumber); + start = 0; + } + if (child.type === "code_inline") { + start = child.lineNumber; + } + } + if (start > 0) { + addLineNumberRange(start, token.map[1]); } }); }