Merge rule name/alias in rule definitions and results (for resultVersion 2).

This commit is contained in:
David Anson 2018-01-12 23:21:06 -08:00
parent b565c1ff32
commit 54c28f47c3
13 changed files with 322 additions and 291 deletions

View file

@ -8,31 +8,36 @@ var md = require("markdown-it")({ "html": true });
var rules = require("./rules");
var shared = require("./shared");
// Mappings from rule to description and tag to rules
// Mappings from rule to description and name/tag to rules
var allRuleNames = [];
var ruleNameToRule = {};
var idUpperToRuleNames = {};
// var tagToRuleNames = {};
rules.forEach(function forRule(rule) {
allRuleNames.push(rule.name);
ruleNameToRule[rule.name] = rule;
var ruleName = rule.names[0];
var ruleAliases = rule.names.slice(1);
allRuleNames.push(ruleName);
ruleNameToRule[ruleName] = rule;
// The following is useful for updating README.md
// console.log(
// "* **[" + rule.name + "](doc/Rules.md#" + rule.name.toLowerCase() +
// ")** *" + rule.aliases.join(", ") + "* - " + rule.desc);
// "* **[" + ruleName + "](doc/Rules.md#" + ruleName.toLowerCase() +
// ")** *" + ruleAliases.join(", ") + "* - " + rule.desc);
rule.tags.forEach(function forTag(tag) {
var tagUpper = tag.toUpperCase();
var ruleNames = idUpperToRuleNames[tagUpper] || [];
ruleNames.push(rule.name);
ruleNames.push(ruleName);
idUpperToRuleNames[tagUpper] = ruleNames;
// tagToRuleNames[tag] = ruleName;
});
rule.aliases.forEach(function forAlias(alias) {
ruleAliases.forEach(function forAlias(alias) {
var aliasUpper = alias.toUpperCase();
idUpperToRuleNames[aliasUpper] = [ rule.name ];
idUpperToRuleNames[aliasUpper] = [ ruleName ];
});
});
// The following is useful for updating README.md
// Object.keys(idUpperToRuleNames).sort().forEach(function forTag(tag) {
// console.log("* **" + tag + "** - " + idUpperToRuleNames[tag].join(", "));
// Object.keys(tagToRuleNames).sort().forEach(function forTag(tag) {
// console.log("* **" + tag + "** - " +
// idUpperToRuleNames[tag.toUpperCase()].join(", "));
// });
// Class for results with toString for pretty display
@ -44,11 +49,13 @@ Results.prototype.toString = function resultsToString(useAlias) {
var fileResults = that[file];
if (Array.isArray(fileResults)) {
fileResults.forEach(function forResult(result) {
var ruleMoniker = result.ruleNames ?
result.ruleNames.join("/") :
(result.ruleName + "/" + result.ruleAlias);
results.push(
file + ": " +
result.lineNumber + ": " +
result.ruleName + "/" +
result.ruleAlias + " " +
ruleMoniker + " " +
result.ruleDescription +
(result.errorDetail ?
" [" + result.errorDetail + "]" :
@ -65,7 +72,7 @@ Results.prototype.toString = function resultsToString(useAlias) {
var result =
file + ": " +
lineNumber + ": " +
(useAlias ? rule.aliases[0] : rule.name) + " " +
rule.names[useAlias ? 1 : 0] + " " +
rule.desc;
results.push(result);
});
@ -155,7 +162,8 @@ function lintContent(
var ruleDefault = (defaultKey.length === 0) || !!config[defaultKey[0]];
var mergedRules = {};
rules.forEach(function forRule(rule) {
mergedRules[rule.name] = ruleDefault;
var ruleName = rule.names[0];
mergedRules[ruleName] = ruleDefault;
});
Object.keys(config).forEach(function forKey(key) {
var value = config[key];
@ -178,7 +186,8 @@ function lintContent(
// Create mapping of enabled rules per line
var enabledRules = {};
rules.forEach(function forRule(rule) {
enabledRules[rule.name] = !!mergedRules[rule.name];
var ruleName = rule.names[0];
enabledRules[ruleName] = !!mergedRules[ruleName];
});
function forMatch(match) {
var enabled = match[1].toUpperCase() === "EN";
@ -220,7 +229,8 @@ function lintContent(
var result = (resultVersion === 0) ? {} : [];
rules.forEach(function forRule(rule) {
// Configure rule
params.options = mergedRules[rule.name];
var ruleName = rule.names[0];
params.options = mergedRules[ruleName];
var errors = [];
function addError(lineNumber, detail, context, range) {
errors.push({
@ -272,7 +282,7 @@ function lintContent(
var filteredErrors = errors
.filter(uniqueFilterForSortedErrors)
.filter(function removeDisabledRules(error) {
return enabledRulesPerLineNumber[error.lineNumber][rule.name];
return enabledRulesPerLineNumber[error.lineNumber][ruleName];
})
.map(function formatResults(error) {
if (resultVersion === 0) {
@ -296,19 +306,23 @@ function lintContent(
range = [ column, length ];
}
}
return {
"lineNumber": error.lineNumber,
"ruleName": rule.name,
"ruleAlias": rule.aliases[0],
"ruleDescription": rule.desc,
"errorDetail": error.detail,
"errorContext": error.context,
"errorRange": range
};
var errorObject = {};
errorObject.lineNumber = error.lineNumber;
if (resultVersion === 1) {
errorObject.ruleName = rule.names[0];
errorObject.ruleAlias = rule.names[1];
} else {
errorObject.ruleNames = rule.names;
}
errorObject.ruleDescription = rule.desc;
errorObject.errorDetail = error.detail;
errorObject.errorContext = error.context;
errorObject.errorRange = range;
return errorObject;
});
if (filteredErrors.length) {
if (resultVersion === 0) {
result[rule.name] = filteredErrors;
result[ruleName] = filteredErrors;
} else {
result.push.apply(result, filteredErrors);
}
@ -359,7 +373,7 @@ function lintInput(options, synchronous, callback) {
shared.frontMatterRe : options.frontMatter;
var noInlineConfig = !!options.noInlineConfig;
var resultVersion = (options.resultVersion === undefined) ?
1 : options.resultVersion;
2 : options.resultVersion;
var results = new Results();
// Helper to lint the next file in the array
function lintFilesArray() {

View file

@ -183,10 +183,9 @@ function flattenLists(params) {
module.exports = [
{
"name": "MD001",
"names": [ "MD001", "header-increment" ],
"desc": "Header levels should only increment by one level at a time",
"tags": [ "headers" ],
"aliases": [ "header-increment" ],
"regexp": null,
"func": function MD001(params, errors) {
var prevLevel = 0;
@ -202,10 +201,9 @@ module.exports = [
},
{
"name": "MD002",
"names": [ "MD002", "first-header-h1" ],
"desc": "First header should be a top level header",
"tags": [ "headers" ],
"aliases": [ "first-header-h1" ],
"regexp": null,
"func": function MD002(params, errors) {
var level = params.options.level || 1;
@ -221,10 +219,9 @@ module.exports = [
},
{
"name": "MD003",
"names": [ "MD003", "header-style" ],
"desc": "Header style",
"tags": [ "headers" ],
"aliases": [ "header-style" ],
"regexp": null,
"func": function MD003(params, errors) {
var style = params.options.style || "consistent";
@ -258,10 +255,9 @@ module.exports = [
},
{
"name": "MD004",
"names": [ "MD004", "ul-style" ],
"desc": "Unordered list style",
"tags": [ "bullet", "ul" ],
"aliases": [ "ul-style" ],
"regexp": listItemMarkerRe,
"func": function MD004(params, errors) {
var style = params.options.style || "consistent";
@ -293,10 +289,9 @@ module.exports = [
},
{
"name": "MD005",
"names": [ "MD005", "list-indent" ],
"desc": "Inconsistent indentation for list items at the same level",
"tags": [ "bullet", "ul", "indentation" ],
"aliases": [ "list-indent" ],
"regexp": listItemMarkerRe,
"func": function MD005(params, errors) {
flattenLists(params).forEach(function forList(list) {
@ -309,10 +304,9 @@ module.exports = [
},
{
"name": "MD006",
"names": [ "MD006", "ul-start-left" ],
"desc": "Consider starting bulleted lists at the beginning of the line",
"tags": [ "bullet", "ul", "indentation" ],
"aliases": [ "ul-start-left" ],
"regexp": listItemMarkerRe,
"func": function MD006(params, errors) {
flattenLists(params).forEach(function forList(list) {
@ -324,10 +318,9 @@ module.exports = [
},
{
"name": "MD007",
"names": [ "MD007", "ul-indent" ],
"desc": "Unordered list indentation",
"tags": [ "bullet", "ul", "indentation" ],
"aliases": [ "ul-indent" ],
"regexp": listItemMarkerRe,
"func": function MD007(params, errors) {
var optionsIndent = params.options.indent || 2;
@ -346,10 +339,9 @@ module.exports = [
},
{
"name": "MD009",
"names": [ "MD009", "no-trailing-spaces" ],
"desc": "Trailing spaces",
"tags": [ "whitespace" ],
"aliases": [ "no-trailing-spaces" ],
"regexp": trailingSpaceRe,
"func": function MD009(params, errors) {
var brSpaces = params.options.br_spaces || 0;
@ -377,10 +369,9 @@ module.exports = [
},
{
"name": "MD010",
"names": [ "MD010", "no-hard-tabs" ],
"desc": "Hard tabs",
"tags": [ "whitespace", "hard_tab" ],
"aliases": [ "no-hard-tabs" ],
"regexp": tabRe,
"func": function MD010(params, errors) {
var codeBlocks = params.options.code_blocks;
@ -395,10 +386,9 @@ module.exports = [
},
{
"name": "MD011",
"names": [ "MD011", "no-reversed-links" ],
"desc": "Reversed link syntax",
"tags": [ "links" ],
"aliases": [ "no-reversed-links" ],
"regexp": reversedLinkRe,
"func": function MD011(params, errors) {
forEachInlineChild(params, "text", function forToken(token) {
@ -411,10 +401,9 @@ module.exports = [
},
{
"name": "MD012",
"names": [ "MD012", "no-multiple-blanks" ],
"desc": "Multiple consecutive blank lines",
"tags": [ "whitespace", "blank_lines" ],
"aliases": [ "no-multiple-blanks" ],
"regexp": null,
"func": function MD012(params, errors) {
var maximum = params.options.maximum || 1;
@ -429,10 +418,9 @@ module.exports = [
},
{
"name": "MD013",
"names": [ "MD013", "line-length" ],
"desc": "Line length",
"tags": [ "line_length" ],
"aliases": [ "line-length" ],
"regexp": longLineReFunc,
"func": function MD013(params, errors) {
var lineLength = params.options.line_length || defaultLineLength;
@ -487,10 +475,9 @@ module.exports = [
},
{
"name": "MD014",
"names": [ "MD014", "commands-show-output" ],
"desc": "Dollar signs used before commands without showing output",
"tags": [ "code" ],
"aliases": [ "commands-show-output" ],
"regexp": dollarCommandRe,
"func": function MD014(params, errors) {
[ "code_block", "fence" ].forEach(function forType(type) {
@ -509,10 +496,9 @@ module.exports = [
},
{
"name": "MD018",
"names": [ "MD018", "no-missing-space-atx" ],
"desc": "No space after hash on atx style header",
"tags": [ "headers", "atx", "spaces" ],
"aliases": [ "no-missing-space-atx" ],
"regexp": atxHeaderSpaceRe,
"func": function MD018(params, errors) {
forEachLine(params, function forLine(line, lineIndex, inCode) {
@ -524,10 +510,9 @@ module.exports = [
},
{
"name": "MD019",
"names": [ "MD019", "no-multiple-space-atx" ],
"desc": "Multiple spaces after hash on atx style header",
"tags": [ "headers", "atx", "spaces" ],
"aliases": [ "no-multiple-space-atx" ],
"regexp": atxHeaderSpaceRe,
"func": function MD019(params, errors) {
filterTokens(params, "heading_open", function forToken(token) {
@ -540,10 +525,9 @@ module.exports = [
},
{
"name": "MD020",
"names": [ "MD020", "no-missing-space-closed-atx" ],
"desc": "No space inside hashes on closed atx style header",
"tags": [ "headers", "atx_closed", "spaces" ],
"aliases": [ "no-missing-space-closed-atx" ],
"regexp": atxClosedHeaderNoSpaceRe,
"func": function MD020(params, errors) {
forEachLine(params, function forLine(line, lineIndex, inCode) {
@ -559,10 +543,9 @@ module.exports = [
},
{
"name": "MD021",
"names": [ "MD021", "no-multiple-space-closed-atx" ],
"desc": "Multiple spaces inside hashes on closed atx style header",
"tags": [ "headers", "atx_closed", "spaces" ],
"aliases": [ "no-multiple-space-closed-atx" ],
"regexp": atxClosedHeaderSpaceRe,
"func": function MD021(params, errors) {
filterTokens(params, "heading_open", function forToken(token) {
@ -578,10 +561,9 @@ module.exports = [
},
{
"name": "MD022",
"names": [ "MD022", "blanks-around-headers" ],
"desc": "Headers should be surrounded by blank lines",
"tags": [ "headers", "blank_lines" ],
"aliases": [ "blanks-around-headers" ],
"regexp": null,
"func": function MD022(params, errors) {
var prevHeadingLineNumber = 0;
@ -613,10 +595,9 @@ module.exports = [
},
{
"name": "MD023",
"names": [ "MD023", "header-start-left" ],
"desc": "Headers must start at the beginning of the line",
"tags": [ "headers", "spaces" ],
"aliases": [ "header-start-left" ],
"regexp": spaceBeforeHeaderRe,
"func": function MD023(params, errors) {
filterTokens(params, "heading_open", function forToken(token) {
@ -628,10 +609,9 @@ module.exports = [
},
{
"name": "MD024",
"names": [ "MD024", "no-duplicate-header" ],
"desc": "Multiple headers with the same content",
"tags": [ "headers" ],
"aliases": [ "no-duplicate-header" ],
"regexp": null,
"func": function MD024(params, errors) {
var knownContent = [];
@ -646,10 +626,9 @@ module.exports = [
},
{
"name": "MD025",
"names": [ "MD025", "single-h1" ],
"desc": "Multiple top level headers in the same document",
"tags": [ "headers" ],
"aliases": [ "single-h1" ],
"regexp": null,
"func": function MD025(params, errors) {
var level = params.options.level || 1;
@ -668,10 +647,9 @@ module.exports = [
},
{
"name": "MD026",
"names": [ "MD026", "no-trailing-punctuation" ],
"desc": "Trailing punctuation in header",
"tags": [ "headers" ],
"aliases": [ "no-trailing-punctuation" ],
"regexp": trailingPunctuationRe,
"func": function MD026(params, errors) {
var punctuation = params.options.punctuation || ".,;:!?";
@ -687,10 +665,9 @@ module.exports = [
},
{
"name": "MD027",
"names": [ "MD027", "no-multiple-space-blockquote" ],
"desc": "Multiple spaces after blockquote symbol",
"tags": [ "blockquote", "whitespace", "indentation" ],
"aliases": [ "no-multiple-space-blockquote" ],
"regexp": spaceAfterBlockQuote,
"func": function MD027(params, errors) {
var blockquoteNesting = 0;
@ -723,10 +700,9 @@ module.exports = [
},
{
"name": "MD028",
"names": [ "MD028", "no-blanks-blockquote" ],
"desc": "Blank line inside blockquote",
"tags": [ "blockquote", "whitespace" ],
"aliases": [ "no-blanks-blockquote" ],
"regexp": null,
"func": function MD028(params, errors) {
var prevToken = {};
@ -741,10 +717,9 @@ module.exports = [
},
{
"name": "MD029",
"names": [ "MD029", "ol-prefix" ],
"desc": "Ordered list item prefix",
"tags": [ "ol" ],
"aliases": [ "ol-prefix" ],
"regexp": listItemMarkerRe,
"func": function MD029(params, errors) {
var style = params.options.style || "one_or_ordered";
@ -773,10 +748,9 @@ module.exports = [
},
{
"name": "MD030",
"names": [ "MD030", "list-marker-space" ],
"desc": "Spaces after list markers",
"tags": [ "ol", "ul", "whitespace" ],
"aliases": [ "list-marker-space" ],
"regexp": listItemMarkerRe,
"func": function MD030(params, errors) {
var ulSingle = params.options.ul_single || 1;
@ -799,10 +773,9 @@ module.exports = [
},
{
"name": "MD031",
"names": [ "MD031", "blanks-around-fences" ],
"desc": "Fenced code blocks should be surrounded by blank lines",
"tags": [ "code", "blank_lines" ],
"aliases": [ "blanks-around-fences" ],
"regexp": null,
"func": function MD031(params, errors) {
var lines = params.lines;
@ -816,10 +789,9 @@ module.exports = [
},
{
"name": "MD032",
"names": [ "MD032", "blanks-around-lists" ],
"desc": "Lists should be surrounded by blank lines",
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
"aliases": [ "blanks-around-lists" ],
"regexp": null,
"func": function MD032(params, errors) {
var blankOrListRe = /^[\s>]*($|\s)/;
@ -847,10 +819,9 @@ module.exports = [
},
{
"name": "MD033",
"names": [ "MD033", "no-inline-html" ],
"desc": "Inline HTML",
"tags": [ "html" ],
"aliases": [ "no-inline-html" ],
"regexp": htmlRe,
"func": function MD033(params, errors) {
var allowedElements = (params.options.allowed_elements || [])
@ -882,10 +853,9 @@ module.exports = [
},
{
"name": "MD034",
"names": [ "MD034", "no-bare-urls" ],
"desc": "Bare URL used",
"tags": [ "links", "url" ],
"aliases": [ "no-bare-urls" ],
"regexp": bareUrlRe,
"func": function MD034(params, errors) {
filterTokens(params, "inline", function forToken(token) {
@ -907,10 +877,9 @@ module.exports = [
},
{
"name": "MD035",
"names": [ "MD035", "hr-style" ],
"desc": "Horizontal rule style",
"tags": [ "hr" ],
"aliases": [ "hr-style" ],
"regexp": null,
"func": function MD035(params, errors) {
var style = params.options.style || "consistent";
@ -925,10 +894,9 @@ module.exports = [
},
{
"name": "MD036",
"names": [ "MD036", "no-emphasis-as-header" ],
"desc": "Emphasis used instead of a header",
"tags": [ "headers", "emphasis" ],
"aliases": [ "no-emphasis-as-header" ],
"regexp": null,
"func": function MD036(params, errors) {
var punctuation = params.options.punctuation || ".,;:!?";
@ -975,10 +943,9 @@ module.exports = [
},
{
"name": "MD037",
"names": [ "MD037", "no-space-in-emphasis" ],
"desc": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"aliases": [ "no-space-in-emphasis" ],
"regexp": null,
"func": function MD037(params, errors) {
forEachInlineChild(params, "text", function forToken(token) {
@ -1001,10 +968,9 @@ module.exports = [
},
{
"name": "MD038",
"names": [ "MD038", "no-space-in-code" ],
"desc": "Spaces inside code span elements",
"tags": [ "whitespace", "code" ],
"aliases": [ "no-space-in-code" ],
"regexp": null,
"func": function MD038(params, errors) {
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
@ -1031,10 +997,9 @@ module.exports = [
},
{
"name": "MD039",
"names": [ "MD039", "no-space-in-links" ],
"desc": "Spaces inside link text",
"tags": [ "whitespace", "links" ],
"aliases": [ "no-space-in-links" ],
"regexp": spaceInsideLinkRe,
"func": function MD039(params, errors) {
filterTokens(params, "inline", function forToken(token) {
@ -1061,10 +1026,9 @@ module.exports = [
},
{
"name": "MD040",
"names": [ "MD040", "fenced-code-language" ],
"desc": "Fenced code blocks should have a language specified",
"tags": [ "code", "language" ],
"aliases": [ "fenced-code-language" ],
"regexp": null,
"func": function MD040(params, errors) {
filterTokens(params, "fence", function forToken(token) {
@ -1076,10 +1040,9 @@ module.exports = [
},
{
"name": "MD041",
"names": [ "MD041", "first-line-h1" ],
"desc": "First line in file should be a top level header",
"tags": [ "headers" ],
"aliases": [ "first-line-h1" ],
"regexp": null,
"func": function MD041(params, errors) {
var level = params.options.level || 1;
@ -1109,10 +1072,9 @@ module.exports = [
},
{
"name": "MD042",
"names": [ "MD042", "no-empty-links" ],
"desc": "No empty links",
"tags": [ "links" ],
"aliases": [ "no-empty-links" ],
"regexp": emptyLinkRe,
"func": function MD042(params, errors) {
filterTokens(params, "inline", function forToken(token) {
@ -1142,10 +1104,9 @@ module.exports = [
},
{
"name": "MD043",
"names": [ "MD043", "required-headers" ],
"desc": "Required header structure",
"tags": [ "headers" ],
"aliases": [ "required-headers" ],
"regexp": null,
"func": function MD043(params, errors) {
var requiredHeaders = params.options.headers;
@ -1179,10 +1140,9 @@ module.exports = [
},
{
"name": "MD044",
"names": [ "MD044", "proper-names" ],
"desc": "Proper names should have the correct capitalization",
"tags": [ "spelling" ],
"aliases": [ "proper-names" ],
"regexp": null,
"func": function MD044(params, errors) {
var names = params.options.names || [];
@ -1221,10 +1181,9 @@ module.exports = [
}
},
{
"name": "MD045",
"names": [ "MD045", "no-alt-text" ],
"desc": "Images should have alternate text (alt text)",
"tags": [ "accessibility", "images" ],
"aliases": [ "no-alt-text" ],
"regexp": null,
"func": function MD045(params, errors) {
forEachInlineChild(params, "image", function forToken(token) {