diff --git a/.eslintrc b/.eslintrc index 56df64c9..f4b0502a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -126,7 +126,7 @@ "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "max-depth": [2, 4], "max-len": [2, 80, 2], - "max-nested-callbacks": [2, 2], + "max-nested-callbacks": [2, 3], "max-params": [2, 4], "max-statements": [0, 10], "new-cap": 2, diff --git a/lib/markdownlint.js b/lib/markdownlint.js index 369f060e..37c769a8 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -5,6 +5,30 @@ var md = require("markdown-it")(); var rules = require("./rules"); var shared = require("./shared"); +var ruleToDescription = {}; +rules.forEach(function forRule(rule) { + ruleToDescription[rule.name] = rule.desc; +}); + +function Results() { } +Results.prototype.toString = function resultsToString() { + var self = this; + var results = []; + Object.keys(self).forEach(function forFile(file) { + var fileResults = self[file]; + Object.keys(fileResults).forEach(function forRule(rule) { + var ruleResults = fileResults[rule]; + ruleResults.forEach(function forLine(lineNumber) { + var result = + file + ": " + lineNumber + ": " + + rule + " " + ruleToDescription[rule]; + results.push(result); + }); + }); + }); + return results.join("\n"); +}; + function numberComparison(a, b) { return a - b; } @@ -55,7 +79,7 @@ module.exports = function markdownlint(options, callback) { callback = callback || function noop() {}; var files = options.files || []; var config = options.config || { "default": true }; - var results = {}; + var results = new Results(); function lintFiles() { var file = files.shift(); if (file) { diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 6079c593..bd31e184 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -80,6 +80,44 @@ module.exports.projectFiles = function projectFiles(test) { }); }; +module.exports.resultFormatting = function resultFormatting(test) { + test.expect(3); + var options = { + "files": [ + "./test/atx_header_spacing.md", + "./test/first_header_bad_atx.md" + ] + }; + markdownlint(options, function callback(err, actualResult) { + test.ifError(err); + var expectedResult = { + "./test/atx_header_spacing.md": { + "MD002": [ 3 ], + "MD018": [ 1 ], + "MD019": [ 3, 5 ] + }, + "./test/first_header_bad_atx.md": { + "MD002": [ 1 ] + } + }; + test.deepEqual(actualResult, expectedResult, "Undetected issues."); + var actualMessage = actualResult.toString(); + var expectedMessage = + "./test/atx_header_spacing.md: 3: MD002" + + " First header should be a h1 header\n" + + "./test/atx_header_spacing.md: 1: MD018" + + " No space after hash on atx style header\n" + + "./test/atx_header_spacing.md: 3: MD019" + + " Multiple spaces after hash on atx style header\n" + + "./test/atx_header_spacing.md: 5: MD019" + + " Multiple spaces after hash on atx style header\n" + + "./test/first_header_bad_atx.md: 1: MD002" + + " First header should be a h1 header"; + test.equal(actualMessage, expectedMessage, "Incorrect message."); + test.done(); + }); +}; + module.exports.missingOptions = function missingOptions(test) { test.expect(2); markdownlint(null, function callback(err, result) {