Reimplement MD010/no-hard-tabs using micromark tokens.

This commit is contained in:
David Anson 2024-08-08 22:08:32 -07:00
parent e528b2998a
commit ff8c1bf9b2
5 changed files with 122 additions and 72 deletions

View file

@ -3847,9 +3847,8 @@ module.exports = {
const { addError, filterTokens, forEachLine, withinAnyRange } = const { addError, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
__webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { filterByTypes, getDescendantsByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
const { codeBlockAndSpanRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const tabRe = /\t+/g; const tabRe = /\t+/g;
@ -3859,7 +3858,7 @@ module.exports = {
"names": [ "MD010", "no-hard-tabs" ], "names": [ "MD010", "no-hard-tabs" ],
"description": "Hard tabs", "description": "Hard tabs",
"tags": [ "whitespace", "hard_tab" ], "tags": [ "whitespace", "hard_tab" ],
"parser": "markdownit", "parser": "micromark",
"function": function MD010(params, onError) { "function": function MD010(params, onError) {
const codeBlocks = params.config.code_blocks; const codeBlocks = params.config.code_blocks;
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks; const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
@ -3871,23 +3870,50 @@ module.exports = {
const spaceMultiplier = (spacesPerTab === undefined) ? const spaceMultiplier = (spacesPerTab === undefined) ?
1 : 1 :
Math.max(0, Number(spacesPerTab)); Math.max(0, Number(spacesPerTab));
const exclusions = includeCode ? [] : codeBlockAndSpanRanges(); const exclusions = [];
filterTokens(params, "fence", (token) => { // eslint-disable-next-line jsdoc/valid-types
const language = token.info.trim().toLowerCase(); /** @type import("../helpers/micromark.cjs").TokenType[] */
if (ignoreCodeLanguages.has(language)) { const exclusionTypes = [];
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) { if (includeCode) {
exclusions.push([ i, 0, params.lines[i].length ]); if (ignoreCodeLanguages.size > 0) {
exclusionTypes.push("codeFenced");
} }
} else {
exclusionTypes.push("codeFenced", "codeIndented", "codeText");
} }
const codeTokens = filterByTypes(
params.parsers.micromark.tokens,
exclusionTypes
).filter((token) => {
if ((token.type === "codeFenced") && (ignoreCodeLanguages.size > 0)) {
const fenceInfos = getDescendantsByType(token, [ "codeFencedFence", "codeFencedFenceInfo" ]);
return fenceInfos.every((fenceInfo) => ignoreCodeLanguages.has(fenceInfo.text.toLowerCase()));
}
return true;
}); });
forEachLine(lineMetadata(), (line, lineIndex, inCode) => { for (const codeToken of codeTokens) {
if (includeCode || !inCode) { const codeFenced = (codeToken.type === "codeFenced");
const startLine = codeToken.startLine + (codeFenced ? 1 : 0);
const endLine = codeToken.endLine - (codeFenced ? 1 : 0);
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
const startColumn =
(lineNumber === codeToken.startLine) ? codeToken.startColumn : 1;
const endColumn =
(lineNumber === codeToken.endLine) ? codeToken.endColumn : params.lines[lineNumber - 1].length;
exclusions.push([
lineNumber,
startColumn,
endColumn - startColumn + 1
]);
}
}
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
const line = params.lines[lineIndex];
let match = null; let match = null;
while ((match = tabRe.exec(line)) !== null) { while ((match = tabRe.exec(line)) !== null) {
const { index } = match; const column = match.index + 1;
const column = index + 1;
const length = match[0].length; const length = match[0].length;
if (!withinAnyRange(exclusions, lineIndex, index, length)) { if (!withinAnyRange(exclusions, lineIndex + 1, column, length)) {
addError( addError(
onError, onError,
lineIndex + 1, lineIndex + 1,
@ -3903,7 +3929,6 @@ module.exports = {
} }
} }
} }
});
} }
}; };

View file

@ -2,9 +2,8 @@
"use strict"; "use strict";
const { addError, filterTokens, forEachLine, withinAnyRange } = const { addError, withinAnyRange } = require("../helpers");
require("../helpers"); const { filterByTypes, getDescendantsByType } = require("../helpers/micromark.cjs");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
const tabRe = /\t+/g; const tabRe = /\t+/g;
@ -14,7 +13,7 @@ module.exports = {
"names": [ "MD010", "no-hard-tabs" ], "names": [ "MD010", "no-hard-tabs" ],
"description": "Hard tabs", "description": "Hard tabs",
"tags": [ "whitespace", "hard_tab" ], "tags": [ "whitespace", "hard_tab" ],
"parser": "markdownit", "parser": "micromark",
"function": function MD010(params, onError) { "function": function MD010(params, onError) {
const codeBlocks = params.config.code_blocks; const codeBlocks = params.config.code_blocks;
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks; const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
@ -26,23 +25,50 @@ module.exports = {
const spaceMultiplier = (spacesPerTab === undefined) ? const spaceMultiplier = (spacesPerTab === undefined) ?
1 : 1 :
Math.max(0, Number(spacesPerTab)); Math.max(0, Number(spacesPerTab));
const exclusions = includeCode ? [] : codeBlockAndSpanRanges(); const exclusions = [];
filterTokens(params, "fence", (token) => { // eslint-disable-next-line jsdoc/valid-types
const language = token.info.trim().toLowerCase(); /** @type import("../helpers/micromark.cjs").TokenType[] */
if (ignoreCodeLanguages.has(language)) { const exclusionTypes = [];
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) { if (includeCode) {
exclusions.push([ i, 0, params.lines[i].length ]); if (ignoreCodeLanguages.size > 0) {
exclusionTypes.push("codeFenced");
} }
} else {
exclusionTypes.push("codeFenced", "codeIndented", "codeText");
} }
const codeTokens = filterByTypes(
params.parsers.micromark.tokens,
exclusionTypes
).filter((token) => {
if ((token.type === "codeFenced") && (ignoreCodeLanguages.size > 0)) {
const fenceInfos = getDescendantsByType(token, [ "codeFencedFence", "codeFencedFenceInfo" ]);
return fenceInfos.every((fenceInfo) => ignoreCodeLanguages.has(fenceInfo.text.toLowerCase()));
}
return true;
}); });
forEachLine(lineMetadata(), (line, lineIndex, inCode) => { for (const codeToken of codeTokens) {
if (includeCode || !inCode) { const codeFenced = (codeToken.type === "codeFenced");
const startLine = codeToken.startLine + (codeFenced ? 1 : 0);
const endLine = codeToken.endLine - (codeFenced ? 1 : 0);
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
const startColumn =
(lineNumber === codeToken.startLine) ? codeToken.startColumn : 1;
const endColumn =
(lineNumber === codeToken.endLine) ? codeToken.endColumn : params.lines[lineNumber - 1].length;
exclusions.push([
lineNumber,
startColumn,
endColumn - startColumn + 1
]);
}
}
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
const line = params.lines[lineIndex];
let match = null; let match = null;
while ((match = tabRe.exec(line)) !== null) { while ((match = tabRe.exec(line)) !== null) {
const { index } = match; const column = match.index + 1;
const column = index + 1;
const length = match[0].length; const length = match[0].length;
if (!withinAnyRange(exclusions, lineIndex, index, length)) { if (!withinAnyRange(exclusions, lineIndex + 1, column, length)) {
addError( addError(
onError, onError,
lineIndex + 1, lineIndex + 1,
@ -58,6 +84,5 @@ module.exports = {
} }
} }
} }
});
} }
}; };

View file

@ -25,7 +25,7 @@ if (true) {
} }
} }
```text ``` Text
hello hello
world world
} }

View file

@ -8854,7 +8854,7 @@ Generated by [AVA](https://avajs.dev).
}␊ }␊
}␊ }␊
\`\`\`text␊ \`\`\` Text␊
hello␊ hello␊
world␊ world␊
}␊ }␊