Update MD037/MD038/MD039 to report fixInfo for violations.

This commit is contained in:
David Anson 2019-09-06 22:35:33 -07:00
parent 620853f200
commit c8a74bd72c
3 changed files with 66 additions and 29 deletions

View file

@ -4,29 +4,43 @@
const { addErrorContext, forEachInlineChild } = require("../helpers"); const { addErrorContext, forEachInlineChild } = require("../helpers");
const leftSpaceRe = /(?:^|\s)(\*\*?|__?)\s.*[^\\]\1/g;
const rightSpaceRe = /(?:^|[^\\])(\*\*?|__?).+\s\1(?:\s|$)/g;
module.exports = { module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ], "names": [ "MD037", "no-space-in-emphasis" ],
"description": "Spaces inside emphasis markers", "description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ], "tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) { "function": function MD037(params, onError) {
forEachInlineChild(params, "text", (token) => { forEachInlineChild(params, "text", (token) => {
let left = true; [ leftSpaceRe, rightSpaceRe ].forEach((spaceRe, index) => {
let match = /(?:^|\s)(\*\*?|__?)\s.*[^\\]\1/.exec(token.content); let match = null;
if (!match) { while ((match = spaceRe.exec(token.content)) !== null) {
left = false; const [ fullText, marker ] = match;
match = /(?:^|[^\\])(\*\*?|__?).+\s\1(?:\s|$)/.exec(token.content);
}
if (match) {
const fullText = match[0];
const line = params.lines[token.lineNumber - 1]; const line = params.lines[token.lineNumber - 1];
if (line.includes(fullText)) { if (line.includes(fullText)) {
const text = fullText.trim(); const text = fullText.trim();
const column = line.indexOf(text) + 1; const column = line.indexOf(text) + 1;
const length = text.length; const length = text.length;
addErrorContext(onError, token.lineNumber, const markerLength = marker.length;
text, left, !left, [ column, length ]); const emphasized = text.slice(markerLength, length - markerLength);
const fixedText = `${marker}${emphasized.trim()}${marker}`;
addErrorContext(
onError,
token.lineNumber,
text,
index === 0,
index !== 0,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
);
} }
} }
}); });
});
} }
}; };

View file

@ -5,8 +5,8 @@
const { addErrorContext, filterTokens, forEachInlineCodeSpan, newLineRe } = const { addErrorContext, filterTokens, forEachInlineCodeSpan, newLineRe } =
require("../helpers"); require("../helpers");
const startRe = /^\s([^`]|$)/; const leftSpaceRe = /^\s([^`]|$)/;
const endRe = /[^`]\s$/; const rightSpaceRe = /[^`]\s$/;
module.exports = { module.exports = {
"names": [ "MD038", "no-space-in-code" ], "names": [ "MD038", "no-space-in-code" ],
@ -23,8 +23,8 @@ module.exports = {
let rangeLength = code.length + (2 * tickCount); let rangeLength = code.length + (2 * tickCount);
let rangeLineOffset = 0; let rangeLineOffset = 0;
const codeLines = code.split(newLineRe); const codeLines = code.split(newLineRe);
const left = startRe.test(code); const left = leftSpaceRe.test(code);
const right = !left && endRe.test(code); const right = !left && rightSpaceRe.test(code);
if (right && (codeLines.length > 1)) { if (right && (codeLines.length > 1)) {
rangeIndex = 0; rangeIndex = 0;
rangeLineOffset = codeLines.length - 1; rangeLineOffset = codeLines.length - 1;
@ -36,8 +36,18 @@ module.exports = {
const context = tokenLines[lineIndex + rangeLineOffset] const context = tokenLines[lineIndex + rangeLineOffset]
.substring(rangeIndex, rangeIndex + rangeLength); .substring(rangeIndex, rangeIndex + rangeLength);
addErrorContext( addErrorContext(
onError, token.lineNumber + lineIndex + rangeLineOffset, onError,
context, left, right, [ rangeIndex + 1, rangeLength ]); token.lineNumber + lineIndex + rangeLineOffset,
context,
left,
right,
[ rangeIndex + 1, rangeLength ],
{
"editColumn": rangeIndex + (left ? tickCount : 0) + 1,
"deleteCount": rangeLength - (right ? tickCount : 0),
"insertText": code.trim()
}
);
} }
}); });
} }

View file

@ -2,8 +2,7 @@
"use strict"; "use strict";
const { addErrorContext, filterTokens, rangeFromRegExp } = const { addErrorContext, filterTokens } = require("../helpers");
require("../helpers");
const spaceInLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=\(\S*\))/; const spaceInLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=\(\S*\))/;
@ -12,10 +11,11 @@ module.exports = {
"description": "Spaces inside link text", "description": "Spaces inside link text",
"tags": [ "whitespace", "links" ], "tags": [ "whitespace", "links" ],
"function": function MD039(params, onError) { "function": function MD039(params, onError) {
filterTokens(params, "inline", function forToken(token) { filterTokens(params, "inline", (token) => {
const { line, lineNumber, children } = token;
let inLink = false; let inLink = false;
let linkText = ""; let linkText = "";
token.children.forEach(function forChild(child) { children.forEach((child) => {
if (child.type === "link_open") { if (child.type === "link_open") {
inLink = true; inLink = true;
linkText = ""; linkText = "";
@ -24,9 +24,22 @@ module.exports = {
const left = linkText.trimLeft().length !== linkText.length; const left = linkText.trimLeft().length !== linkText.length;
const right = linkText.trimRight().length !== linkText.length; const right = linkText.trimRight().length !== linkText.length;
if (left || right) { if (left || right) {
addErrorContext(onError, token.lineNumber, const match = line.match(spaceInLinkRe);
"[" + linkText + "]", left, right, const column = match.index + 1;
rangeFromRegExp(token.line, spaceInLinkRe)); const length = match[0].length;
addErrorContext(
onError,
lineNumber,
`[${linkText}]`,
left,
right,
[ column, length ],
{
"editColumn": column + 1,
"deleteCount": length - 2,
"insertText": linkText.trim()
}
);
} }
} else if (inLink) { } else if (inLink) {
linkText += child.content; linkText += child.content;