From d0fcd32f3a35fce176fa2a262c1a28820ddc9966 Mon Sep 17 00:00:00 2001 From: David Anson Date: Fri, 6 Mar 2015 09:21:55 -0800 Subject: [PATCH] Add MD018, MD019 with tests, switch to RegExp.test. --- lib/rules.js | 46 ++++++++++++++++++++++++++++-------- test/atx_header_spacing.json | 4 ++++ test/atx_header_spacing.md | 5 ++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 test/atx_header_spacing.json create mode 100644 test/atx_header_spacing.md diff --git a/lib/rules.js b/lib/rules.js index d878c681..2e2f5d32 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -6,7 +6,7 @@ function indentFor(token) { function headingStyleFor(token) { if ((token.lines[1] - token.lines[0]) === 1) { - if (token.line.match(/#\s*$/)) { + if (/#\s*$/.test(token.line)) { return "atx_closed"; } return "atx"; @@ -174,7 +174,7 @@ module.exports = [ "desc": "Trailing spaces", "func": function MD009(params, errors) { params.lines.forEach(function forLine(line, lineIndex) { - if (line.match(/\s$/)) { + if (/\s$/.test(line)) { errors.push(lineIndex + 1); } }); @@ -186,7 +186,7 @@ module.exports = [ "desc": "Hard tabs", "func": function MD010(params, errors) { params.lines.forEach(function forLine(line, lineIndex) { - if (line.match(/\t/)) { + if (/\t/.test(line)) { errors.push(lineIndex + 1); } }); @@ -201,7 +201,7 @@ module.exports = [ .forEach(function forToken(token) { filterTokens(token.children, "text") .forEach(function forChild(child) { - if (child.content.match(/\([^)]+\)\[[^\]]+\]/)) { + if (/\([^)]+\)\[[^\]]+\]/.test(child.content)) { errors.push(token.lineNumber); } }); @@ -259,7 +259,33 @@ module.exports = [ }).every(function forLine(line) { return /^\$\s/.test(line); })) { - errors.push(token.lines[0] + 1); + errors.push(token.lineNumber); + } + }); + } + }, + + { + "name": "MD018", + "desc": "No space after hash on atx style header", + "func": function MD018(params, errors) { + params.lines.forEach(function forLine(line, lineIndex) { + if (/^#+[^#\s]/.test(line)) { + errors.push(lineIndex + 1); + } + }); + } + }, + + { + "name": "MD019", + "desc": "Multiple spaces after hash on atx style header", + "func": function MD019(params, errors) { + filterTokens(params.tokens, "heading_open") + .forEach(function forToken(token) { + if ((headingStyleFor(token) === "atx") && + /^#+\s\s/.test(token.line)) { + errors.push(token.lineNumber); } }); } @@ -289,7 +315,7 @@ module.exports = [ var lines = padAndTrim(params.lines); var inCode = false; lines.forEach(function forLine(line, lineNumber) { - if (line.match(/^(```|~~~)/)) { + if (/^(```|~~~)/.test(line)) { inCode = !inCode; if ((inCode && lines[lineNumber - 1].length) || (!inCode && lines[lineNumber + 1].length)) { @@ -311,15 +337,15 @@ module.exports = [ var prevLine = ""; params.lines.forEach(function forLine(line, lineIndex) { if (!inCode) { - var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/); - if (listMarker && !inList && !prevLine.match(/^($|\s)/)) { + var listMarker = /^([\*\+\-]|(\d+\.))\s/.test(line.trim()); + if (listMarker && !inList && !/^($|\s)/.test(prevLine)) { errors.push(lineIndex + 1); - } else if (!listMarker && inList && !line.match(/^($|\s)/)) { + } else if (!listMarker && inList && !/^($|\s)/.test(line)) { errors.push(lineIndex); } inList = listMarker; } - if (line.trim().match(/^(```|~~~)/)) { + if (/^(```|~~~)/.test(line.trim())) { inCode = !inCode; inList = false; } diff --git a/test/atx_header_spacing.json b/test/atx_header_spacing.json new file mode 100644 index 00000000..2f37f5bb --- /dev/null +++ b/test/atx_header_spacing.json @@ -0,0 +1,4 @@ +{ + "default": true, + "MD002": false +} diff --git a/test/atx_header_spacing.md b/test/atx_header_spacing.md new file mode 100644 index 00000000..b2c8838a --- /dev/null +++ b/test/atx_header_spacing.md @@ -0,0 +1,5 @@ +#Header 1 {MD018} + +## Header 2 {MD019} + +## Header 3 {MD019} \ No newline at end of file