Refactor MD037/no-space-in-emphasis slightly to simplify.

This commit is contained in:
David Anson 2023-08-29 22:29:04 -07:00
parent 86a72a05fa
commit 84e664e86e
2 changed files with 60 additions and 66 deletions

View file

@ -4,9 +4,6 @@
const { addError } = require("../helpers");
const emphasisStartTextRe = /^(\S{1,3})(\s+)\S/;
const emphasisEndTextRe = /\S(\s+)(\S{1,3})$/;
module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ],
"description": "Spaces inside emphasis markers",
@ -45,51 +42,51 @@ module.exports = {
}
// Process bare tokens for each emphasis marker type
for (const emphasisTokens of emphasisTokensByMarker.values()) {
for (const entry of emphasisTokensByMarker.entries()) {
const [ marker, emphasisTokens ] = entry;
for (let i = 0; i + 1 < emphasisTokens.length; i += 2) {
// Process start token of start/end pair
const startToken = emphasisTokens[i];
const startText =
lines[startToken.startLine - 1].slice(startToken.startColumn - 1);
const startMatch = startText.match(emphasisStartTextRe);
const startLine = lines[startToken.startLine - 1];
const startSlice = startLine.slice(startToken.endColumn - 1);
const startMatch = startSlice.match(/^\s+\S/);
if (startMatch) {
const [ startContext, startMarker, startSpaces ] = startMatch;
if ((startMarker === startToken.text) && (startSpaces.length > 0)) {
addError(
onError,
startToken.startLine,
undefined,
startContext,
[ startToken.startColumn, startContext.length ],
{
"editColumn": startToken.endColumn,
"deleteCount": startSpaces.length
}
);
}
const [ startSpaceCharacter ] = startMatch;
const startContext = `${marker}${startSpaceCharacter}`;
addError(
onError,
startToken.startLine,
undefined,
startContext,
[ startToken.startColumn, startContext.length ],
{
"editColumn": startToken.endColumn,
"deleteCount": startSpaceCharacter.length - 1
}
);
}
// Process end token of start/end pair
const endToken = emphasisTokens[i + 1];
const endText =
lines[endToken.startLine - 1].slice(0, endToken.endColumn - 1);
const endMatch = endText.match(emphasisEndTextRe);
const endLine = lines[endToken.startLine - 1];
const endSlice = endLine.slice(0, endToken.startColumn - 1);
const endMatch = endSlice.match(/\S\s+$/);
if (endMatch) {
const [ endContext, endSpace, endMarker ] = endMatch;
if ((endMarker === endToken.text) && (endSpace.length > 0)) {
addError(
onError,
endToken.startLine,
undefined,
endContext,
[ endToken.endColumn - endContext.length, endContext.length ],
{
"editColumn": endToken.startColumn - endSpace.length,
"deleteCount": endSpace.length
}
);
}
const [ endSpaceCharacter ] = endMatch;
const endContext = `${endSpaceCharacter}${marker}`;
addError(
onError,
endToken.startLine,
undefined,
endContext,
[ endToken.endColumn - endContext.length, endContext.length ],
{
"editColumn":
endToken.startColumn - (endSpaceCharacter.length - 1),
"deleteCount": endSpaceCharacter.length - 1
}
);
}
}
}