Update all rules to better handle wrongly-typed configuration parameters.

This commit is contained in:
David Anson 2020-01-25 18:40:39 -08:00
parent 26ad0550ec
commit 3238ed4249
25 changed files with 134 additions and 39 deletions

View file

@ -465,7 +465,7 @@ module.exports.frontMatterHasTitle =
const ignoreFrontMatter =
(frontMatterTitlePattern !== undefined) && !frontMatterTitlePattern;
const frontMatterTitleRe =
new RegExp(frontMatterTitlePattern || "^\\s*title\\s*[:=]", "i");
new RegExp(String(frontMatterTitlePattern || "^\\s*title\\s*[:=]"), "i");
return !ignoreFrontMatter &&
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
};

View file

@ -9,7 +9,7 @@ module.exports = {
"description": "First heading should be a top level heading",
"tags": [ "headings", "headers" ],
"function": function MD002(params, onError) {
const level = params.config.level || 1;
const level = Number(params.config.level || 1);
const tag = "h" + level;
params.tokens.every(function forToken(token) {
if (token.type === "heading_open") {

View file

@ -10,7 +10,7 @@ module.exports = {
"description": "Heading style",
"tags": [ "headings", "headers" ],
"function": function MD003(params, onError) {
let style = params.config.style || "consistent";
let style = String(params.config.style || "consistent");
filterTokens(params, "heading_open", function forToken(token) {
const styleForToken = headingStyleFor(token);
if (style === "consistent") {

View file

@ -11,7 +11,7 @@ module.exports = {
"description": "Unordered list style",
"tags": [ "bullet", "ul" ],
"function": function MD004(params, onError) {
const style = params.config.style || "consistent";
const style = String(params.config.style || "consistent");
let expectedStyle = style;
const nestingStyles = [];
flattenedLists().forEach((list) => {

View file

@ -11,7 +11,7 @@ module.exports = {
"description": "Unordered list indentation",
"tags": [ "bullet", "ul", "indentation" ],
"function": function MD007(params, onError) {
const indent = params.config.indent || 2;
const indent = Number(params.config.indent || 2);
const startIndented = !!params.config.start_indented;
flattenedLists().forEach((list) => {
if (list.unordered && list.parentsUnordered) {

View file

@ -12,9 +12,7 @@ module.exports = {
"tags": [ "whitespace" ],
"function": function MD009(params, onError) {
let brSpaces = params.config.br_spaces;
if (brSpaces === undefined) {
brSpaces = 2;
}
brSpaces = Number((brSpaces === undefined) ? 2 : brSpaces);
const listItemEmptyLines = !!params.config.list_item_empty_lines;
const strict = !!params.config.strict;
const listItemLineNumbers = [];

View file

@ -10,7 +10,7 @@ module.exports = {
"description": "Multiple consecutive blank lines",
"tags": [ "whitespace", "blank_lines" ],
"function": function MD012(params, onError) {
const maximum = params.config.maximum || 1;
const maximum = Number(params.config.maximum || 1);
let count = 0;
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
count = (inCode || line.trim().length) ? 0 : count + 1;

View file

@ -27,9 +27,11 @@ module.exports = {
"description": "Line length",
"tags": [ "line_length" ],
"function": function MD013(params, onError) {
const lineLength = params.config.line_length || 80;
const headingLineLength = params.config.heading_line_length || lineLength;
const codeLineLength = params.config.code_block_line_length || lineLength;
const lineLength = Number(params.config.line_length || 80);
const headingLineLength =
Number(params.config.heading_line_length || lineLength);
const codeLineLength =
Number(params.config.code_block_line_length || lineLength);
const strict = !!params.config.strict;
const longLineRePostfix =
strict ? longLineRePostfixStrict : longLineRePostfixRelaxed;

View file

@ -10,13 +10,9 @@ module.exports = {
"tags": [ "headings", "headers", "blank_lines" ],
"function": function MD022(params, onError) {
let linesAbove = params.config.lines_above;
if (linesAbove === undefined) {
linesAbove = 1;
}
linesAbove = Number((linesAbove === undefined) ? 1 : linesAbove);
let linesBelow = params.config.lines_below;
if (linesBelow === undefined) {
linesBelow = 1;
}
linesBelow = Number((linesBelow === undefined) ? 1 : linesBelow);
const { lines } = params;
filterTokens(params, "heading_open", (token) => {
const [ topIndex, nextIndex ] = token.map;

View file

@ -9,8 +9,8 @@ module.exports = {
"description": "Multiple headings with the same content",
"tags": [ "headings", "headers" ],
"function": function MD024(params, onError) {
const siblingsOnly = params.config.siblings_only ||
params.config.allow_different_nesting || false;
const siblingsOnly = !!params.config.siblings_only ||
!!params.config.allow_different_nesting || false;
const knownContents = [ null, [] ];
let lastLevel = 1;
let knownContent = knownContents[lastLevel];

View file

@ -10,7 +10,7 @@ module.exports = {
"description": "Multiple top level headings in the same document",
"tags": [ "headings", "headers" ],
"function": function MD025(params, onError) {
const level = params.config.level || 1;
const level = Number(params.config.level || 1);
const tag = "h" + level;
const foundFrontMatterTitle =
frontMatterHasTitle(

View file

@ -11,9 +11,8 @@ module.exports = {
"tags": [ "headings", "headers" ],
"function": function MD026(params, onError) {
let punctuation = params.config.punctuation;
if (punctuation === undefined) {
punctuation = allPunctuation;
}
punctuation =
String((punctuation === undefined) ? allPunctuation : punctuation);
const trailingPunctuationRe =
new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
forEachHeading(params, (heading) => {

View file

@ -17,7 +17,7 @@ module.exports = {
"description": "Ordered list item prefix",
"tags": [ "ol" ],
"function": function MD029(params, onError) {
const style = params.config.style || "one_or_ordered";
const style = String(params.config.style || "one_or_ordered");
flattenedLists().forEach((list) => {
if (!list.unordered) {
let listStyle = style;

View file

@ -10,10 +10,10 @@ module.exports = {
"description": "Spaces after list markers",
"tags": [ "ol", "ul", "whitespace" ],
"function": function MD030(params, onError) {
const ulSingle = params.config.ul_single || 1;
const olSingle = params.config.ol_single || 1;
const ulMulti = params.config.ul_multi || 1;
const olMulti = params.config.ol_multi || 1;
const ulSingle = Number(params.config.ul_single || 1);
const olSingle = Number(params.config.ol_single || 1);
const ulMulti = Number(params.config.ul_multi || 1);
const olMulti = Number(params.config.ol_multi || 1);
flattenedLists().forEach((list) => {
const lineCount = list.lastLineIndex - list.open.map[0];
const allSingle = lineCount === list.items.length;

View file

@ -18,8 +18,9 @@ module.exports = {
"description": "Inline HTML",
"tags": [ "html" ],
"function": function MD033(params, onError) {
const allowedElements = (params.config.allowed_elements || [])
.map((element) => element.toLowerCase());
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
let match = null;
// eslint-disable-next-line no-unmodified-loop-condition

View file

@ -9,7 +9,7 @@ module.exports = {
"description": "Horizontal rule style",
"tags": [ "hr" ],
"function": function MD035(params, onError) {
let style = params.config.style || "consistent";
let style = String(params.config.style || "consistent");
filterTokens(params, "hr", function forToken(token) {
const lineTrim = token.line.trim();
if (style === "consistent") {

View file

@ -9,7 +9,9 @@ module.exports = {
"description": "Emphasis used instead of a heading",
"tags": [ "headings", "headers", "emphasis" ],
"function": function MD036(params, onError) {
const punctuation = params.config.punctuation || allPunctuation;
let punctuation = params.config.punctuation;
punctuation =
String((punctuation === undefined) ? allPunctuation : punctuation);
const re = new RegExp("[" + punctuation + "]$");
// eslint-disable-next-line jsdoc/require-jsdoc
function base(token) {

View file

@ -9,7 +9,7 @@ module.exports = {
"description": "First line in file should be a top level heading",
"tags": [ "headings", "headers" ],
"function": function MD041(params, onError) {
const level = params.config.level || 1;
const level = Number(params.config.level || 1);
const tag = "h" + level;
const foundFrontMatterTitle =
frontMatterHasTitle(

View file

@ -11,7 +11,7 @@ module.exports = {
"tags": [ "headings", "headers" ],
"function": function MD043(params, onError) {
const requiredHeadings = params.config.headings || params.config.headers;
if (requiredHeadings) {
if (Array.isArray(requiredHeadings)) {
const levels = {};
[ 1, 2, 3, 4, 5, 6 ].forEach(function forLevel(level) {
levels["h" + level] = "######".substr(-level);

View file

@ -10,7 +10,8 @@ module.exports = {
"description": "Proper names should have the correct capitalization",
"tags": [ "spelling" ],
"function": function MD044(params, onError) {
const names = params.config.names || [];
let names = params.config.names;
names = Array.isArray(names) ? names : [];
const codeBlocks = params.config.code_blocks;
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
names.forEach((name) => {

View file

@ -14,7 +14,7 @@ module.exports = {
"description": "Code block style",
"tags": [ "code" ],
"function": function MD046(params, onError) {
let expectedStyle = params.config.style || "consistent";
let expectedStyle = String(params.config.style || "consistent");
params.tokens
.filter((token) => token.type === "code_block" || token.type === "fence")
.forEach((token) => {

View file

@ -9,7 +9,7 @@ module.exports = {
"description": "Code fence style",
"tags": [ "code" ],
"function": function MD048(params, onError) {
const style = params.config.style || "consistent";
const style = String(params.config.style || "consistent");
let expectedStyle = style;
params.tokens
.filter((token) => token.type === "fence")

View file

@ -1589,12 +1589,14 @@ tape("validateConfigSchema", (test) => {
const jsonFileRe = /\.json$/i;
const resultsFileRe = /\.results\.json$/i;
const jsConfigFileRe = /^jsconfig\.json$/i;
const wrongTypesFileRe = /wrong-types-in-config-file.json$/i;
const testDirectory = __dirname;
const testFiles = fs.readdirSync(testDirectory);
testFiles.filter(function filterFile(file) {
return jsonFileRe.test(file) &&
!resultsFileRe.test(file) &&
!jsConfigFileRe.test(file);
!jsConfigFileRe.test(file) &&
!wrongTypesFileRe.test(file);
}).forEach(function forFile(file) {
const data = fs.readFileSync(
path.join(testDirectory, file),

View file

@ -0,0 +1,91 @@
{
"MD002": {
"level": "1"
},
"MD003": {
"style": 0
},
"MD004": {
"style": 0
},
"MD007": {
"indent": "2",
"start_indented": 0
},
"MD009": {
"br_spaces": "2",
"list_item_empty_lines": 0,
"strict": 0
},
"MD010": {
"code_blocks": 1
},
"MD012": {
"maximum": "1"
},
"MD013": {
"code_block_line_length": "80",
"code_blocks": 1,
"headers": 1,
"heading_line_length": "80",
"headings": 1,
"line_length": "80",
"strict": 0,
"tables": 1
},
"MD022": {
"lines_above": "1",
"lines_below": "1"
},
"MD024": {
"allow_different_nesting": 0,
"siblings_only": 0
},
"MD025": {
"front_matter_title": 0,
"level": "1"
},
"MD026": {
"punctuation": 0
},
"MD029": {
"style": 0
},
"MD030": {
"ol_multi": "1",
"ol_single": "1",
"ul_multi": "1",
"ul_single": "1"
},
"MD031": {
"list_items": 1
},
"MD033": {
"allowed_elements": 0
},
"MD035": {
"style": 0
},
"MD036": {
"punctuation": 0
},
"MD041": {
"front_matter_title": 0,
"level": "1"
},
"MD043": {
"headers": 0,
"headings": 0
},
"MD044": {
"code_blocks": 1,
"names": 0
},
"MD046": {
"style": 0
},
"MD048": {
"style": 0
},
"$schema": "../schema/markdownlint-config-schema.json"
}

View file

@ -0,0 +1,3 @@
# Wrong Types in Config File
Long line long line long line long line long line long line long line long line long line long line {MD013}