Improve highlighting for MD038/no-space-in-code, add more tests.

This commit is contained in:
David Anson 2017-10-31 23:05:28 -07:00
parent 681e8bae4e
commit 5fee0a921d
4 changed files with 87 additions and 20 deletions

View file

@ -220,11 +220,12 @@ function lintContent(
// Configure rule
params.options = mergedRules[rule.name];
var errors = [];
function addError(lineNumber, detail, context) {
function addError(lineNumber, detail, context, range) {
errors.push({
"lineNumber": lineNumber + frontMatterLines.length,
"detail": detail || null,
"context": context || null
"context": context || null,
"range": range || null
});
}
errors.add = function add(lineNumber) {
@ -238,18 +239,19 @@ function lintContent(
addError(lineNumber, "Expected: " + expected + "; Actual: " + actual);
}
};
errors.addContext = function addContext(lineNumber, context, left, right) {
if (context.length <= 30) {
// Nothing to do
} else if (left && right) {
context = context.substr(0, 15) + "..." + context.substr(-15);
} else if (right) {
context = "..." + context.substr(-30);
} else {
context = context.substr(0, 30) + "...";
}
addError(lineNumber, null, context);
};
errors.addContext =
function addContext(lineNumber, context, left, right, range) {
if (context.length <= 30) {
// Nothing to do
} else if (left && right) {
context = context.substr(0, 15) + "..." + context.substr(-15);
} else if (right) {
context = "..." + context.substr(-30);
} else {
context = context.substr(0, 30) + "...";
}
addError(lineNumber, null, context, range);
};
rule.func(params, errors);
// Record any errors (significant performance benefit from length check)
if (errors.length) {
@ -263,9 +265,9 @@ function lintContent(
if (resultVersion === 0) {
return error.lineNumber;
}
var range = null;
var range = error.range;
var regexp = rule.regexp;
if (regexp) {
if (!range && regexp) {
if (typeof regexp === "function") {
regexp = regexp(params.options);
}

View file

@ -15,7 +15,6 @@ var listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
var reversedLinkRe = /\([^)]+\)\[[^\]^][^\]]*]/;
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
var spaceBeforeHeaderRe = /^\s+\S/;
var spaceInsideCodeRe = /`(?:(?:\s[^`]*)|(?:[^`]*\s))`/;
var spaceInsideEmphasisRe = /(\*\*?|__?)(?:(?:\s.+)|(?:.+\s))\1/;
var spaceInsideLinkRe = /\[(?:(?:\s[^\]]*)|(?:[^\]]*\s))](?=\(\S*\))/;
var tabRe = /\t+/;
@ -982,7 +981,7 @@ module.exports = [
"desc": "Spaces inside code span elements",
"tags": [ "whitespace", "code" ],
"aliases": [ "no-space-in-code" ],
"regexp": spaceInsideCodeRe,
"regexp": null,
"func": function MD038(params, errors) {
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
forEachInlineChild(params, "code_inline",
@ -992,10 +991,15 @@ module.exports = [
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
var inlineCodeSpan = match[1];
var content = match[3];
var length = inlineCodeSpan.length;
var column = match.index + 1 + (match[0].length - length);
var range = [ column, length ];
if (/^\s([^`]|$)/.test(content)) {
errors.addContext(token.lineNumber, inlineCodeSpan);
errors.addContext(
token.lineNumber, inlineCodeSpan, true, false, range);
} else if (/[^`]\s$/.test(content)) {
errors.addContext(token.lineNumber, inlineCodeSpan, false, true);
errors.addContext(
token.lineNumber, inlineCodeSpan, false, true, range);
}
}
});

View file

@ -20,3 +20,10 @@ Space [ inside ](link) text
```
```
space `` inside `` code
space `inside` of ` code` elements
`space` inside `of` code ` elements`
space ``inside`` of `` code`` elements
`` ` embedded backtick``
``embedded backtick` ``

View file

@ -71,6 +71,60 @@
"errorContext": "` inside `",
"errorRange": [7, 10]
},
{
"lineNumber": 24,
"ruleName": "MD038",
"ruleAlias": "no-space-in-code",
"ruleDescription": "Spaces inside code span elements",
"errorDetail": null,
"errorContext": "`` inside ``",
"errorRange": [ 7, 12 ]
},
{
"lineNumber": 25,
"ruleName": "MD038",
"ruleAlias": "no-space-in-code",
"ruleDescription": "Spaces inside code span elements",
"errorDetail": null,
"errorContext": "` code`",
"errorRange": [ 19, 7 ]
},
{
"lineNumber": 26,
"ruleName": "MD038",
"ruleAlias": "no-space-in-code",
"ruleDescription": "Spaces inside code span elements",
"errorDetail": null,
"errorContext": "` elements`",
"errorRange": [ 26, 11 ]
},
{
"lineNumber": 27,
"ruleName": "MD038",
"ruleAlias": "no-space-in-code",
"ruleDescription": "Spaces inside code span elements",
"errorDetail": null,
"errorContext": "`` code``",
"errorRange": [ 21, 9 ]
},
{
"lineNumber": 28,
"ruleName": "MD038",
"ruleAlias": "no-space-in-code",
"ruleDescription": "Spaces inside code span elements",
"errorDetail": null,
"errorContext": "`` ` embedded backtick``",
"errorRange": [ 1, 25 ]
},
{
"lineNumber": 29,
"ruleName": "MD038",
"ruleAlias": "no-space-in-code",
"ruleDescription": "Spaces inside code span elements",
"errorDetail": null,
"errorContext": "``embedded backtick` ``",
"errorRange": [ 1, 24 ]
},
{
"lineNumber": 19,
"ruleName": "MD039",