2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-08 22:08:32 -07:00
|
|
|
const { addError, withinAnyRange } = require("../helpers");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { getDescendantsByType, getExclusionsForToken } = require("../helpers/micromark.cjs");
|
|
|
|
const { filterByTypesCached } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2019-08-21 21:02:09 -07:00
|
|
|
const tabRe = /\t+/g;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD010", "no-hard-tabs" ],
|
|
|
|
"description": "Hard tabs",
|
|
|
|
"tags": [ "whitespace", "hard_tab" ],
|
2024-08-08 22:08:32 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD010(params, onError) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const codeBlocks = params.config.code_blocks;
|
2021-12-17 17:24:00 -08:00
|
|
|
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
|
2022-04-28 21:09:06 -07:00
|
|
|
const ignoreCodeLanguages = new Set(
|
|
|
|
(params.config.ignore_code_languages || [])
|
|
|
|
.map((language) => language.toLowerCase())
|
|
|
|
);
|
2021-04-09 16:33:01 -07:00
|
|
|
const spacesPerTab = params.config.spaces_per_tab;
|
|
|
|
const spaceMultiplier = (spacesPerTab === undefined) ?
|
|
|
|
1 :
|
|
|
|
Math.max(0, Number(spacesPerTab));
|
2024-08-08 22:08:32 -07:00
|
|
|
const exclusions = [];
|
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("../helpers/micromark.cjs").TokenType[] */
|
|
|
|
const exclusionTypes = [];
|
|
|
|
if (includeCode) {
|
|
|
|
if (ignoreCodeLanguages.size > 0) {
|
|
|
|
exclusionTypes.push("codeFenced");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exclusionTypes.push("codeFenced", "codeIndented", "codeText");
|
|
|
|
}
|
2024-08-24 22:05:16 -07:00
|
|
|
const codeTokens = filterByTypesCached(exclusionTypes).filter((token) => {
|
2024-08-08 22:08:32 -07:00
|
|
|
if ((token.type === "codeFenced") && (ignoreCodeLanguages.size > 0)) {
|
|
|
|
const fenceInfos = getDescendantsByType(token, [ "codeFencedFence", "codeFencedFenceInfo" ]);
|
|
|
|
return fenceInfos.every((fenceInfo) => ignoreCodeLanguages.has(fenceInfo.text.toLowerCase()));
|
2022-04-28 21:09:06 -07:00
|
|
|
}
|
2024-08-08 22:08:32 -07:00
|
|
|
return true;
|
2022-04-28 21:09:06 -07:00
|
|
|
});
|
2024-08-08 22:08:32 -07:00
|
|
|
for (const codeToken of codeTokens) {
|
2024-08-15 21:34:27 -07:00
|
|
|
const exclusionsForToken = getExclusionsForToken(params.lines, codeToken);
|
|
|
|
if (codeToken.type === "codeFenced") {
|
|
|
|
exclusionsForToken.pop();
|
|
|
|
exclusionsForToken.shift();
|
2024-08-08 22:08:32 -07:00
|
|
|
}
|
2024-08-15 21:34:27 -07:00
|
|
|
exclusions.push(...exclusionsForToken);
|
2024-08-08 22:08:32 -07:00
|
|
|
}
|
|
|
|
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
|
|
|
const line = params.lines[lineIndex];
|
|
|
|
let match = null;
|
|
|
|
while ((match = tabRe.exec(line)) !== null) {
|
|
|
|
const column = match.index + 1;
|
|
|
|
const length = match[0].length;
|
|
|
|
if (!withinAnyRange(exclusions, lineIndex + 1, column, length)) {
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineIndex + 1,
|
|
|
|
"Column: " + column,
|
|
|
|
undefined,
|
|
|
|
[ column, length ],
|
|
|
|
{
|
|
|
|
"editColumn": column,
|
|
|
|
"deleteCount": length,
|
|
|
|
"insertText": "".padEnd(length * spaceMultiplier)
|
|
|
|
}
|
|
|
|
);
|
2019-08-21 21:02:09 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-08-08 22:08:32 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|