Update MD018/MD019/MD020/MD021 to report fixInfo for violations.

This commit is contained in:
David Anson 2019-09-08 16:51:00 -07:00
parent c8a74bd72c
commit 316bfeadaa
8 changed files with 167 additions and 50 deletions

View file

@ -2,23 +2,59 @@
"use strict";
const { addErrorContext, forEachLine, rangeFromRegExp } = require("../helpers");
const { addErrorContext, forEachLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const atxClosedHeadingNoSpaceRe = /(?:^#+[^#\s])|(?:[^#\s]#+\s*$)/;
module.exports = {
"names": [ "MD020", "no-missing-space-closed-atx" ],
"description": "No space inside hashes on closed atx style heading",
"tags": [ "headings", "headers", "atx_closed", "spaces" ],
"function": function MD020(params, onError) {
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
if (!inCode && /^#+[^#]*[^\\]#+$/.test(line)) {
const left = /^#+[^#\s]/.test(line);
const right = /[^#\s]#+$/.test(line);
if (left || right) {
addErrorContext(onError, lineIndex + 1, line.trim(), left,
right, rangeFromRegExp(line, atxClosedHeadingNoSpaceRe));
if (!inCode) {
const match = /^(#+)(\s*)([^#]+?)(\s*)(\\?)(#+)(\s*)$/.exec(line);
if (match) {
const [
,
leftHash,
{ "length": leftSpaceLength },
content,
{ "length": rightSpaceLength },
rightEscape,
rightHash,
{ "length": trailSpaceLength }
] = match;
const leftHashLength = leftHash.length;
const rightHashLength = rightHash.length;
const left = !leftSpaceLength;
const right =
(!rightSpaceLength && (!rightEscape || (rightHashLength > 1))) ||
(rightEscape && (rightHashLength > 1));
if (left || right) {
const range = left ?
[
1,
leftHashLength + 1
] :
[
line.length - trailSpaceLength - rightHashLength,
rightHashLength + 1
];
addErrorContext(
onError,
lineIndex + 1,
line.trim(),
left,
right,
range,
{
"editColumn": 1,
"deleteLength": line.length,
"insertText":
`${leftHash} ${content} ${rightEscape}${rightHash}`
}
);
}
}
}
});