2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-04-13 11:18:57 -07:00
|
|
|
const { addErrorContext, filterTokens, forEachInlineCodeSpan, newLineRe } =
|
|
|
|
require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2022-12-19 21:51:18 -08:00
|
|
|
const leftSpaceRe = /^\s(?:[^`]|$)/;
|
2019-09-06 22:35:33 -07:00
|
|
|
const rightSpaceRe = /[^`]\s$/;
|
2022-06-12 19:04:39 -07:00
|
|
|
|
|
|
|
const spaceInsideCodeInline = (token) => (
|
|
|
|
(token.type === "code_inline") &&
|
|
|
|
(leftSpaceRe.test(token.content) || rightSpaceRe.test(token.content))
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD038", "no-space-in-code" ],
|
|
|
|
"description": "Spaces inside code span elements",
|
|
|
|
"tags": [ "whitespace", "code" ],
|
|
|
|
"function": function MD038(params, onError) {
|
2019-04-13 11:18:57 -07:00
|
|
|
filterTokens(params, "inline", (token) => {
|
2022-06-12 19:04:39 -07:00
|
|
|
if (token.children.some(spaceInsideCodeInline)) {
|
2019-01-30 22:09:20 -08:00
|
|
|
const tokenLines = params.lines.slice(token.map[0], token.map[1]);
|
2019-04-13 11:18:57 -07:00
|
|
|
forEachInlineCodeSpan(
|
2019-01-30 22:09:20 -08:00
|
|
|
tokenLines.join("\n"),
|
|
|
|
(code, lineIndex, columnIndex, tickCount) => {
|
|
|
|
let rangeIndex = columnIndex - tickCount;
|
|
|
|
let rangeLength = code.length + (2 * tickCount);
|
|
|
|
let rangeLineOffset = 0;
|
2019-09-19 21:39:59 -07:00
|
|
|
let fixIndex = columnIndex;
|
|
|
|
let fixLength = code.length;
|
2019-04-13 11:18:57 -07:00
|
|
|
const codeLines = code.split(newLineRe);
|
2019-09-06 22:35:33 -07:00
|
|
|
const left = leftSpaceRe.test(code);
|
|
|
|
const right = !left && rightSpaceRe.test(code);
|
2019-01-30 22:09:20 -08:00
|
|
|
if (right && (codeLines.length > 1)) {
|
|
|
|
rangeIndex = 0;
|
|
|
|
rangeLineOffset = codeLines.length - 1;
|
2019-09-19 21:39:59 -07:00
|
|
|
fixIndex = 0;
|
2018-07-15 23:05:18 -07:00
|
|
|
}
|
2022-06-12 19:04:39 -07:00
|
|
|
if (left || right) {
|
2019-09-19 21:39:59 -07:00
|
|
|
const codeLinesRange = codeLines[rangeLineOffset];
|
2019-01-30 22:09:20 -08:00
|
|
|
if (codeLines.length > 1) {
|
2019-09-19 21:39:59 -07:00
|
|
|
rangeLength = codeLinesRange.length + tickCount;
|
|
|
|
fixLength = codeLinesRange.length;
|
2019-01-30 22:09:20 -08:00
|
|
|
}
|
|
|
|
const context = tokenLines[lineIndex + rangeLineOffset]
|
|
|
|
.substring(rangeIndex, rangeIndex + rangeLength);
|
2019-09-19 21:39:59 -07:00
|
|
|
const codeLinesRangeTrim = codeLinesRange.trim();
|
|
|
|
const fixText =
|
|
|
|
(codeLinesRangeTrim.startsWith("`") ? " " : "") +
|
|
|
|
codeLinesRangeTrim +
|
|
|
|
(codeLinesRangeTrim.endsWith("`") ? " " : "");
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorContext(
|
2019-09-06 22:35:33 -07:00
|
|
|
onError,
|
|
|
|
token.lineNumber + lineIndex + rangeLineOffset,
|
|
|
|
context,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
[ rangeIndex + 1, rangeLength ],
|
|
|
|
{
|
2019-09-19 21:39:59 -07:00
|
|
|
"editColumn": fixIndex + 1,
|
|
|
|
"deleteCount": fixLength,
|
|
|
|
"insertText": fixText
|
2019-09-06 22:35:33 -07:00
|
|
|
}
|
|
|
|
);
|
2019-01-30 22:09:20 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|