Update MD037/no-space-in-emphasis to ignore violations in tables that include the table pipe character to avoid spanning cells.

This commit is contained in:
David Anson 2021-01-23 20:47:27 -08:00
parent 238781506a
commit d6cd840e7d
3 changed files with 50 additions and 8 deletions

View file

@ -3211,6 +3211,7 @@ var emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
var asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/; var asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
var leftSpaceRe = /^\s+/; var leftSpaceRe = /^\s+/;
var rightSpaceRe = /\s+$/; var rightSpaceRe = /\s+$/;
var tablePipeRe = /\|/;
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",
@ -3227,7 +3228,7 @@ module.exports = {
pendingError = null; pendingError = null;
} }
// eslint-disable-next-line jsdoc/require-jsdoc // eslint-disable-next-line jsdoc/require-jsdoc
function handleRunEnd(line, lineIndex, contextLength, match, matchIndex) { function handleRunEnd(line, lineIndex, contextLength, match, matchIndex, inTable) {
// Close current run // Close current run
var content = line.substring(emphasisIndex, matchIndex); var content = line.substring(emphasisIndex, matchIndex);
if (!emphasisLength) { if (!emphasisLength) {
@ -3238,7 +3239,8 @@ module.exports = {
} }
var leftSpace = leftSpaceRe.test(content); var leftSpace = leftSpaceRe.test(content);
var rightSpace = rightSpaceRe.test(content); var rightSpace = rightSpaceRe.test(content);
if (leftSpace || rightSpace) { if ((leftSpace || rightSpace) &&
(!inTable || !tablePipeRe.test(content))) {
// Report the violation // Report the violation
var contextStart = emphasisIndex - emphasisLength; var contextStart = emphasisIndex - emphasisLength;
var contextEnd = matchIndex + contextLength; var contextEnd = matchIndex + contextLength;
@ -3308,7 +3310,7 @@ module.exports = {
addErrorContext.apply(void 0, pendingError); addErrorContext.apply(void 0, pendingError);
pendingError = null; pendingError = null;
} }
var error = handleRunEnd(line, lineIndex, effectiveEmphasisLength, match, matchIndex); var error = handleRunEnd(line, lineIndex, effectiveEmphasisLength, match, matchIndex, inTable);
if (error) { if (error) {
// @ts-ignore // @ts-ignore
addErrorContext.apply(void 0, error); addErrorContext.apply(void 0, error);
@ -3342,7 +3344,7 @@ module.exports = {
} }
if (emphasisIndex !== -1) { if (emphasisIndex !== -1) {
pendingError = pendingError || pendingError = pendingError ||
handleRunEnd(line, lineIndex, 0, null, line.length); handleRunEnd(line, lineIndex, 0, null, line.length, inTable);
// Adjust for pending run on new line // Adjust for pending run on new line
emphasisIndex = 0; emphasisIndex = 0;
emphasisLength = 0; emphasisLength = 0;

View file

@ -10,6 +10,7 @@ const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/; const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/; const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/; const rightSpaceRe = /\s+$/;
const tablePipeRe = /\|/;
module.exports = { module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ], "names": [ "MD037", "no-space-in-emphasis" ],
@ -28,7 +29,9 @@ module.exports = {
pendingError = null; pendingError = null;
} }
// eslint-disable-next-line jsdoc/require-jsdoc // eslint-disable-next-line jsdoc/require-jsdoc
function handleRunEnd(line, lineIndex, contextLength, match, matchIndex) { function handleRunEnd(
line, lineIndex, contextLength, match, matchIndex, inTable
) {
// Close current run // Close current run
let content = line.substring(emphasisIndex, matchIndex); let content = line.substring(emphasisIndex, matchIndex);
if (!emphasisLength) { if (!emphasisLength) {
@ -39,7 +42,10 @@ module.exports = {
} }
const leftSpace = leftSpaceRe.test(content); const leftSpace = leftSpaceRe.test(content);
const rightSpace = rightSpaceRe.test(content); const rightSpace = rightSpaceRe.test(content);
if (leftSpace || rightSpace) { if (
(leftSpace || rightSpace) &&
(!inTable || !tablePipeRe.test(content))
) {
// Report the violation // Report the violation
const contextStart = emphasisIndex - emphasisLength; const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + contextLength; const contextEnd = matchIndex + contextLength;
@ -111,7 +117,13 @@ module.exports = {
pendingError = null; pendingError = null;
} }
const error = handleRunEnd( const error = handleRunEnd(
line, lineIndex, effectiveEmphasisLength, match, matchIndex); line,
lineIndex,
effectiveEmphasisLength,
match,
matchIndex,
inTable
);
if (error) { if (error) {
// @ts-ignore // @ts-ignore
addErrorContext(...error); addErrorContext(...error);
@ -141,7 +153,7 @@ module.exports = {
} }
if (emphasisIndex !== -1) { if (emphasisIndex !== -1) {
pendingError = pendingError || pendingError = pendingError ||
handleRunEnd(line, lineIndex, 0, null, line.length); handleRunEnd(line, lineIndex, 0, null, line.length, inTable);
// Adjust for pending run on new line // Adjust for pending run on new line
emphasisIndex = 0; emphasisIndex = 0;
emphasisLength = 0; emphasisLength = 0;

View file

@ -313,3 +313,31 @@ text [reference*link] star * star text
*** text *** text
**text** **text**
*** ***
| Table | Table |
| ----- | ----- |
| star | x * y |
| under | x _ y |
| Table | Table |
| ----- | ----- |
| star | x * y |
| star | x * y |
| under | x _ y |
| under | x _ y |
| Table | Table |
| ----- | ------------------------- |
| star | text *text* text |
| star | text * text* text {MD037} |
| star | text *text * text {MD037} |
| under | text _text_ text |
| under | text _ text_ text {MD037} |
| under | text _text _ text {MD037} |
| Table | Table |
| ----- | ----- |
| x * y | x * y |
| x** y | x** y |
| x _ y | x _ y |
| x__ y | x__ y |