Reimplement MD037/no-space-in-emphasis to better handle multiple fixes on a line and more scenarios (fixes #251).

This commit is contained in:
David Anson 2020-03-28 14:16:28 -07:00
parent de86a26e4e
commit 29f16bf402
4 changed files with 121 additions and 40 deletions

View file

@ -234,17 +234,22 @@ module.exports.getLineMetadata = function getLineMetadata(params) {
}
});
filterTokens(params, "list_item_open", function forToken(token) {
let count = 1;
for (let i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][5] = true;
lineMetadata[i][5] = count;
count++;
}
});
filterTokens(params, "hr", function forToken(token) {
lineMetadata[token.map[0]][6] = true;
});
return lineMetadata;
};
// Calls the provided function for each line (with context)
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
lineMetadata.forEach(function forMetadata(metadata) {
// Parameters: line, lineIndex, inCode, onFence, inTable
// Parameters: line, lineIndex, inCode, onFence, inTable, inBreak
handler(...metadata);
});
};

View file

@ -2,39 +2,67 @@
"use strict";
const { addErrorContext, forEachInlineChild } = require("../helpers");
const { addErrorContext, forEachLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const leftSpaceRe = /(?:^|\s)(\*\*?\*?|__?_?)\s.*[^\\]\1/g;
const rightSpaceRe = /(?:^|[^\\])(\*\*?\*?|__?_?).+\s\1(?:\s|$)/g;
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^(\s*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ],
"description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) {
forEachInlineChild(params, "text", (token) => {
const { content, lineNumber } = token;
const columnsReported = [];
[ leftSpaceRe, rightSpaceRe ].forEach((spaceRe, index) => {
forEachLine(
lineMetadata(),
(line, lineIndex, inCode, onFence, inTable, inItem, inBreak) => {
if (inCode || inBreak) {
// Emphasis has no meaning here
return;
}
if (inItem === 1) {
// Trim overlapping '*' list item marker
line = line.replace(asteriskListItemMarkerRe, "$1 $2");
}
let match = null;
while ((match = spaceRe.exec(content)) !== null) {
const [ fullText, marker ] = match;
const line = params.lines[lineNumber - 1];
if (line.includes(fullText)) {
const text = fullText.trim();
const column = line.indexOf(text) + 1;
if (!columnsReported.includes(column)) {
const length = text.length;
const markerLength = marker.length;
const emphasized =
text.slice(markerLength, length - markerLength);
const fixedText = `${marker}${emphasized.trim()}${marker}`;
let emphasisIndex = -1;
let emphasisLength = 0;
// Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) {
const matchIndex = match.index + match[1].length;
const matchLength = match[0].length - match[1].length;
if (emphasisIndex === -1) {
// New run
emphasisLength = matchLength;
emphasisIndex = matchIndex + emphasisLength;
} else {
// Already in a run
if (matchLength !== emphasisLength) {
// Looks like a run within a run, reset to embedded run
emphasisLength += matchLength;
emphasisIndex = matchIndex + emphasisLength;
}
// Extract emphasized content
const content = line.substring(emphasisIndex, matchIndex);
const leftSpace = leftSpaceRe.test(content);
const rightSpace = rightSpaceRe.test(content);
if (leftSpace || rightSpace) {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + emphasisLength;
const context = line.substring(contextStart, contextEnd);
const column = contextStart + 1;
const length = contextEnd - contextStart;
const marker = match[2] || match[3];
const fixedText = `${marker}${content.trim()}${marker}`;
addErrorContext(
onError,
lineNumber,
text,
index === 0,
index !== 0,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
@ -42,11 +70,15 @@ module.exports = {
"insertText": fixedText
}
);
columnsReported.push(column);
}
// Update the run
emphasisLength -= matchLength;
if (!emphasisLength) {
emphasisIndex = -1;
}
}
}
});
});
}
);
}
};

View file

@ -1,28 +1,42 @@
# Heading
## Single-character markers
None are valid emphasis without spaces.
Escaped asterisks \* should \* be ignored by MD037.
Escaped asterisks \* should * be ignored by MD037.
Escaped asterisks * should \* be ignored by MD037.
Escaped asterisks \** should ** be ignored by MD037.
Escaped asterisks *\* should ** be ignored by MD037.
Escaped asterisks ** should \** be ignored by MD037.
Escaped asterisks ** should *\* be ignored by MD037.
Escaped underscores \_ should \_ be ignored by MD037.
Escaped underscores \_ should _ be ignored by MD037.
Escaped underscores _ should \_ be ignored by MD037.
Escaped underscores \__ should __ be ignored by MD037.
## Double-character markers, start
Escaped underscores _\_ should __ be ignored by MD037.
All should be reported because they are valid single-character
marker emphasis without spaces.
Escaped asterisks \** should ** be ignored by MD037. {MD037}
Escaped asterisks *\* should ** be ignored by MD037. {MD037}
Escaped underscores \__ should __ be ignored by MD037. {MD037}
Escaped underscores _\_ should __ be ignored by MD037. {MD037}
## Double-character markers, end
All should be reported, but are ignored because they look like
the start of an embedded emphasis.
Escaped asterisks ** should \** be ignored by MD037.
Escaped asterisks ** should *\* be ignored by MD037.
Escaped underscores __ should \__ be ignored by MD037.

View file

@ -44,9 +44,11 @@ One-sided *broken emphasis * {MD037}
One-sided * broken emphasis* {MD037}
Don't _flag on _words with underscores before them.
Will _flag on _words with underscores before them. {MD037}
The same goes for words* with asterisks* after them.
The same goes for words* with asterisks* after them. {MD037}
But not with escaped\* asterisks\* \_and \_underscores.
* Emphasis* with left space is recognized as a list
@ -95,3 +97,31 @@ ___Strong and emphasis ___ with right space {MD037}
{MD037} Right space __strong __
{MD037} Right space ___strong and emphasis ___
**Multiple ** spaces **in ** emphasis **at ** once. {MD037}
**Multiple ** spaces ** in** emphasis ** at ** once. {MD037}
This is * an ambiguous * scenario {MD037}
* List item *with emphasis* on the
first and *second lines*.
* List * item* {MD037}
* List *item * {MD037}
* List * item * {MD037}
* List item with
*hanging* emphasis
and * some* lines {MD037}
with *space * problems {MD037}
throughout * the * content {MD037}
Uncommon scenarios from the CommonMark specification:
***strong emph***
***strong** in emph*
***emph* in strong**
**in strong *emph***
*in emph **strong***
```markdown
Violations * are * allowed in code blocks where emphasis does not apply.
```