From 9202ebe39042d586aa461e80df7da3ac7691bd3a Mon Sep 17 00:00:00 2001 From: David Anson Date: Sat, 19 Oct 2019 17:34:02 -0700 Subject: [PATCH] Update fixInfo.lineNumber for front matter, sanitize fixInfo object from rules. --- lib/markdownlint.js | 52 +++++++++++-------- ...tailed-results-fixing-with-front-matter.md | 11 ++++ ...-results-fixing-with-front-matter.md.fixed | 13 +++++ ...ults-fixing-with-front-matter.results.json | 47 +++++++++++++++++ test/markdownlint-test.js | 35 +++++++++++++ 5 files changed, 137 insertions(+), 21 deletions(-) create mode 100644 test/detailed-results-fixing-with-front-matter.md create mode 100644 test/detailed-results-fixing-with-front-matter.md.fixed create mode 100644 test/detailed-results-fixing-with-front-matter.results.json diff --git a/lib/markdownlint.js b/lib/markdownlint.js index e788767f..2bdc6e80 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -392,34 +392,44 @@ function lintContent( throwError("range"); } const fixInfo = errorInfo.fixInfo; + const cleanFixInfo = {}; if (fixInfo) { if (!helpers.isObject(fixInfo)) { throwError("fixInfo"); } - if ((fixInfo.lineNumber !== undefined) && - (!helpers.isNumber(fixInfo.lineNumber) || - (fixInfo.lineNumber < 1) || - (fixInfo.lineNumber > lines.length))) { - throwError("fixInfo.lineNumber"); + if (fixInfo.lineNumber !== undefined) { + if ((!helpers.isNumber(fixInfo.lineNumber) || + (fixInfo.lineNumber < 1) || + (fixInfo.lineNumber > lines.length))) { + throwError("fixInfo.lineNumber"); + } + cleanFixInfo.lineNumber = + fixInfo.lineNumber + frontMatterLines.length; } const effectiveLineNumber = fixInfo.lineNumber || errorInfo.lineNumber; - if ((fixInfo.editColumn !== undefined) && - (!helpers.isNumber(fixInfo.editColumn) || - (fixInfo.editColumn < 1) || - (fixInfo.editColumn > - lines[effectiveLineNumber - 1].length + 1))) { - throwError("fixInfo.editColumn"); + if (fixInfo.editColumn !== undefined) { + if ((!helpers.isNumber(fixInfo.editColumn) || + (fixInfo.editColumn < 1) || + (fixInfo.editColumn > + lines[effectiveLineNumber - 1].length + 1))) { + throwError("fixInfo.editColumn"); + } + cleanFixInfo.editColumn = fixInfo.editColumn; } - if ((fixInfo.deleteCount !== undefined) && - (!helpers.isNumber(fixInfo.deleteCount) || - (fixInfo.deleteCount < -1) || - (fixInfo.deleteCount > - lines[effectiveLineNumber - 1].length))) { - throwError("fixInfo.deleteCount"); + if (fixInfo.deleteCount !== undefined) { + if ((!helpers.isNumber(fixInfo.deleteCount) || + (fixInfo.deleteCount < -1) || + (fixInfo.deleteCount > + lines[effectiveLineNumber - 1].length))) { + throwError("fixInfo.deleteCount"); + } + cleanFixInfo.deleteCount = fixInfo.deleteCount; } - if ((fixInfo.insertText !== undefined) && - !helpers.isString(fixInfo.insertText)) { - throwError("fixInfo.insertText"); + if (fixInfo.insertText !== undefined) { + if (!helpers.isString(fixInfo.insertText)) { + throwError("fixInfo.insertText"); + } + cleanFixInfo.insertText = fixInfo.insertText; } } errors.push({ @@ -427,7 +437,7 @@ function lintContent( "detail": errorInfo.detail || null, "context": errorInfo.context || null, "range": errorInfo.range || null, - "fixInfo": errorInfo.fixInfo || null + "fixInfo": fixInfo ? cleanFixInfo : null }); } // Call (possibly external) rule function diff --git a/test/detailed-results-fixing-with-front-matter.md b/test/detailed-results-fixing-with-front-matter.md new file mode 100644 index 00000000..9ba14846 --- /dev/null +++ b/test/detailed-results-fixing-with-front-matter.md @@ -0,0 +1,11 @@ +--- +front: matter +ignore: this +--- +# Fixing with Front Matter +Text text text + +Text [ link ](url) text +## Nested Heading + +Text \ No newline at end of file diff --git a/test/detailed-results-fixing-with-front-matter.md.fixed b/test/detailed-results-fixing-with-front-matter.md.fixed new file mode 100644 index 00000000..f4f6cc11 --- /dev/null +++ b/test/detailed-results-fixing-with-front-matter.md.fixed @@ -0,0 +1,13 @@ +--- +front: matter +ignore: this +--- +# Fixing with Front Matter + +Text text text + +Text [link](url) text + +## Nested Heading + +Text diff --git a/test/detailed-results-fixing-with-front-matter.results.json b/test/detailed-results-fixing-with-front-matter.results.json new file mode 100644 index 00000000..9a376aa9 --- /dev/null +++ b/test/detailed-results-fixing-with-front-matter.results.json @@ -0,0 +1,47 @@ +[ + { + "lineNumber": 6, + "ruleNames": [ "MD009", "no-trailing-spaces" ], + "ruleDescription": "Trailing spaces", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md009", + "errorDetail": "Expected: 0 or 2; Actual: 3", + "errorContext": null, + "errorRange": [ 15, 3 ] + }, + { + "lineNumber": 5, + "ruleNames": [ "MD022", "blanks-around-headings", "blanks-around-headers" ], + "ruleDescription": "Headings should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md022", + "errorDetail": "Expected: 1; Actual: 0; Below", + "errorContext": "# Fixing with Front Matter", + "errorRange": null + }, + { + "lineNumber": 9, + "ruleNames": [ "MD022", "blanks-around-headings", "blanks-around-headers" ], + "ruleDescription": "Headings should be surrounded by blank lines", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md022", + "errorDetail": "Expected: 1; Actual: 0; Above", + "errorContext": "## Nested Heading", + "errorRange": null + }, + { + "lineNumber": 8, + "ruleNames": [ "MD039", "no-space-in-links" ], + "ruleDescription": "Spaces inside link text", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md039", + "errorDetail": null, + "errorContext": "[ link ]", + "errorRange": [ 6, 8 ] + }, + { + "lineNumber": 11, + "ruleNames": [ "MD047", "single-trailing-newline" ], + "ruleDescription": "Files should end with a single newline character", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md047", + "errorDetail": null, + "errorContext": null, + "errorRange": [ 4, 1 ] + } +] diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 224968da..bf670be5 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -751,6 +751,41 @@ module.exports.manyPerLineResultVersion3 = }); }; +module.exports.frontMatterResultVersion3 = + function frontMatterResultVersion3(test) { + test.expect(2); + const options = { + "strings": { + "input": "---\n---\n# Heading\nText\n" + }, + "resultVersion": 3 + }; + markdownlint(options, function callback(err, actualResult) { + test.ifError(err); + const expectedResult = { + "input": [ + { + "lineNumber": 3, + "ruleNames": + [ "MD022", "blanks-around-headings", "blanks-around-headers" ], + "ruleDescription": "Headings should be surrounded by blank lines", + "ruleInformation": + `${homepage}/blob/v${version}/doc/Rules.md#md022`, + "errorDetail": "Expected: 1; Actual: 0; Below", + "errorContext": "# Heading", + "errorRange": null, + "fixInfo": { + "lineNumber": 4, + "insertText": "\n" + } + } + ] + }; + test.deepEqual(actualResult, expectedResult, "Undetected issues."); + test.done(); + }); + }; + module.exports.stringInputLineEndings = function stringInputLineEndings(test) { test.expect(2); const options = {