diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index 25ae54a59..cbddd354f 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -3400,44 +3400,42 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o = function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _require.addError, - addErrorDetailIf = _require.addErrorDetailIf, - indentFor = _require.indentFor, - listItemMarkerRe = _require.listItemMarkerRe, - orderedListItemMarkerRe = _require.orderedListItemMarkerRe, - rangeFromRegExp = _require.rangeFromRegExp; -var _require2 = __webpack_require__(/*! ./cache */ "../lib/cache.js"), - flattenedLists = _require2.flattenedLists; + addErrorDetailIf = _require.addErrorDetailIf; +var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"), + filterByTypes = _require2.filterByTypes; module.exports = { "names": ["MD005", "list-indent"], "description": "Inconsistent indentation for list items at the same level", "tags": ["bullet", "ul", "indentation"], "function": function MD005(params, onError) { - var _iterator = _createForOfIteratorHelper(flattenedLists()), + var lists = filterByTypes(params.parsers.micromark.tokens, ["listOrdered", "listUnordered"]); + var _iterator = _createForOfIteratorHelper(lists), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var list = _step.value; - var expectedIndent = list.indent; + var expectedIndent = list.startColumn - 1; var expectedEnd = 0; - var actualEnd = -1; var endMatching = false; - var _iterator2 = _createForOfIteratorHelper(list.items), + var listItemPrefixes = list.children.filter(function (token) { + return token.type === "listItemPrefix"; + }); + var _iterator2 = _createForOfIteratorHelper(listItemPrefixes), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { - var item = _step2.value; - var line = item.line, - lineNumber = item.lineNumber; - var actualIndent = indentFor(item); - var match = null; - if (list.unordered) { - addErrorDetailIf(onError, lineNumber, expectedIndent, actualIndent, null, null, rangeFromRegExp(line, listItemMarkerRe) + var listItemPrefix = _step2.value; + var lineNumber = listItemPrefix.startLine; + var actualIndent = listItemPrefix.startColumn - 1; + var markerLength = listItemPrefix.text.trim().length; + var range = [1, listItemPrefix.startColumn + markerLength]; + if (list.type === "listUnordered") { + addErrorDetailIf(onError, lineNumber, expectedIndent, actualIndent, null, null, range // No fixInfo; MD007 handles this scenario better ); - } else if (match = orderedListItemMarkerRe.exec(line)) { - actualEnd = match[0].length; + } else { + var actualEnd = range[1] - 1; expectedEnd = expectedEnd || actualEnd; - var markerLength = match[1].length + 1; if (expectedIndent !== actualIndent || endMatching) { if (expectedEnd === actualEnd) { endMatching = true; @@ -3445,7 +3443,7 @@ module.exports = { var detail = endMatching ? "Expected: (".concat(expectedEnd, "); Actual: (").concat(actualEnd, ")") : "Expected: ".concat(expectedIndent, "; Actual: ").concat(actualIndent); var expected = endMatching ? expectedEnd - markerLength : expectedIndent; var actual = endMatching ? actualEnd - markerLength : actualIndent; - addError(onError, lineNumber, detail, null, rangeFromRegExp(line, listItemMarkerRe), { + addError(onError, lineNumber, detail, undefined, range, { "editColumn": Math.min(actual, expected) + 1, "deleteCount": Math.max(actual - expected, 0), "insertText": "".padEnd(Math.max(expected - actual, 0)) diff --git a/lib/md005.js b/lib/md005.js index 696fce1d7..39a7e8bee 100644 --- a/lib/md005.js +++ b/lib/md005.js @@ -2,25 +2,30 @@ "use strict"; -const { addError, addErrorDetailIf, indentFor, listItemMarkerRe, - orderedListItemMarkerRe, rangeFromRegExp } = require("../helpers"); -const { flattenedLists } = require("./cache"); +const { addError, addErrorDetailIf } = require("../helpers"); +const { filterByTypes } = require("../helpers/micromark.cjs"); module.exports = { "names": [ "MD005", "list-indent" ], "description": "Inconsistent indentation for list items at the same level", "tags": [ "bullet", "ul", "indentation" ], "function": function MD005(params, onError) { - for (const list of flattenedLists()) { - const expectedIndent = list.indent; + const lists = filterByTypes( + params.parsers.micromark.tokens, + [ "listOrdered", "listUnordered" ] + ); + for (const list of lists) { + const expectedIndent = list.startColumn - 1; let expectedEnd = 0; - let actualEnd = -1; let endMatching = false; - for (const item of list.items) { - const { line, lineNumber } = item; - const actualIndent = indentFor(item); - let match = null; - if (list.unordered) { + const listItemPrefixes = + list.children.filter((token) => (token.type === "listItemPrefix")); + for (const listItemPrefix of listItemPrefixes) { + const lineNumber = listItemPrefix.startLine; + const actualIndent = listItemPrefix.startColumn - 1; + const markerLength = listItemPrefix.text.trim().length; + const range = [ 1, listItemPrefix.startColumn + markerLength ]; + if (list.type === "listUnordered") { addErrorDetailIf( onError, lineNumber, @@ -28,13 +33,12 @@ module.exports = { actualIndent, null, null, - rangeFromRegExp(line, listItemMarkerRe) + range // No fixInfo; MD007 handles this scenario better ); - } else if ((match = orderedListItemMarkerRe.exec(line))) { - actualEnd = match[0].length; + } else { + const actualEnd = range[1] - 1; expectedEnd = expectedEnd || actualEnd; - const markerLength = match[1].length + 1; if ((expectedIndent !== actualIndent) || endMatching) { if (expectedEnd === actualEnd) { endMatching = true; @@ -52,8 +56,8 @@ module.exports = { onError, lineNumber, detail, - null, - rangeFromRegExp(line, listItemMarkerRe), + undefined, + range, { "editColumn": Math.min(actual, expected) + 1, "deleteCount": Math.max(actual - expected, 0), diff --git a/test/lists-in-blockquote.md b/test/lists-in-blockquote.md index 314c9547c..35eb38a91 100644 --- a/test/lists-in-blockquote.md +++ b/test/lists-in-blockquote.md @@ -83,37 +83,37 @@ Text > Text > > > - Item {MD004} - > > - Item {MD004} {MD027} - > > - Item {MD004} + > > - Item {MD004} {MD005} {MD027} + > > - Item {MD004} {MD005} > > > > > - Item {MD004} - > > > - Item {MD004} {MD027} - > > > - Item {MD004} {MD027} + > > > - Item {MD004} {MD005} {MD027} + > > > - Item {MD004} {MD005} {MD027} Text > Text > > - Item {MD004} {MD032} - > > - Item {MD004} {MD027} - > > - Item {MD004} {MD032} + > > - Item {MD004} {MD005} {MD027} + > > - Item {MD004} {MD005} {MD032} > > > - Item {MD004} {MD032} - > > > - Item {MD004} {MD027} - > > > - Item {MD004} {MD027} + > > > - Item {MD004} {MD005} {MD027} + > > > - Item {MD004} {MD005} {MD027} Text >+ Item -> + Item +> + Item {MD005} >> >>+ Item ->> + Item +>> + Item {MD005} Text >+ Item -> + Item {MD032} +> + Item {MD005} {MD032} >>+ Item {MD032} ->> + Item +>> + Item {MD005} Text diff --git a/test/snapshots/markdownlint-test-scenarios.js.md b/test/snapshots/markdownlint-test-scenarios.js.md index df9506974..2fcaa61e6 100644 --- a/test/snapshots/markdownlint-test-scenarios.js.md +++ b/test/snapshots/markdownlint-test-scenarios.js.md @@ -28764,7 +28764,7 @@ Generated by [AVA](https://avajs.dev). }, { errorContext: null, - errorDetail: 'Expected: 2; Actual: 3', + errorDetail: 'Expected: 7; Actual: 8', errorRange: [ 1, 10, @@ -28780,7 +28780,7 @@ Generated by [AVA](https://avajs.dev). }, { errorContext: null, - errorDetail: 'Expected: 0; Actual: 1', + errorDetail: 'Expected: 5; Actual: 6', errorRange: [ 1, 8, @@ -28796,7 +28796,7 @@ Generated by [AVA](https://avajs.dev). }, { errorContext: null, - errorDetail: 'Expected: 4; Actual: 5', + errorDetail: 'Expected: 9; Actual: 10', errorRange: [ 1, 12, @@ -28812,7 +28812,7 @@ Generated by [AVA](https://avajs.dev). }, { errorContext: null, - errorDetail: 'Expected: 2; Actual: 3', + errorDetail: 'Expected: 8; Actual: 9', errorRange: [ 1, 11, @@ -28828,7 +28828,199 @@ Generated by [AVA](https://avajs.dev). }, { errorContext: null, - errorDetail: 'Expected: 0; Actual: 1', + errorDetail: 'Expected: 6; Actual: 7', + errorRange: [ + 1, + 9, + ], + fixInfo: null, + lineNumber: 86, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 6; Actual: 7', + errorRange: [ + 1, + 9, + ], + fixInfo: null, + lineNumber: 87, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 8; Actual: 9', + errorRange: [ + 1, + 11, + ], + fixInfo: null, + lineNumber: 90, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 8; Actual: 9', + errorRange: [ + 1, + 11, + ], + fixInfo: null, + lineNumber: 91, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 6; Actual: 7', + errorRange: [ + 1, + 9, + ], + fixInfo: null, + lineNumber: 97, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 6; Actual: 7', + errorRange: [ + 1, + 9, + ], + fixInfo: null, + lineNumber: 98, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 8; Actual: 9', + errorRange: [ + 1, + 11, + ], + fixInfo: null, + lineNumber: 100, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 8; Actual: 9', + errorRange: [ + 1, + 11, + ], + fixInfo: null, + lineNumber: 101, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 1; Actual: 2', + errorRange: [ + 1, + 4, + ], + fixInfo: null, + lineNumber: 106, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 2; Actual: 3', + errorRange: [ + 1, + 5, + ], + fixInfo: null, + lineNumber: 109, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 1; Actual: 2', + errorRange: [ + 1, + 4, + ], + fixInfo: null, + lineNumber: 114, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 2; Actual: 3', + errorRange: [ + 1, + 5, + ], + fixInfo: null, + lineNumber: 116, + ruleDescription: 'Inconsistent indentation for list items at the same level', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md005.md', + ruleNames: [ + 'MD005', + 'list-indent', + ], + }, + { + errorContext: null, + errorDetail: 'Expected: 4; Actual: 5', errorRange: [ 1, 7, @@ -28844,7 +29036,7 @@ Generated by [AVA](https://avajs.dev). }, { errorContext: null, - errorDetail: 'Expected: 2; Actual: 3', + errorDetail: 'Expected: 6; Actual: 7', errorRange: [ 1, 9, @@ -28979,7 +29171,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: ' > > - Item {MD004} {MD027}', + errorContext: ' > > - Item {MD004} {MD005} ...', errorDetail: null, errorRange: [ 1, @@ -28998,7 +29190,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: ' > > > - Item {MD004} {MD027...', + errorContext: ' > > > - Item {MD004} {MD005...', errorDetail: null, errorRange: [ 1, @@ -29017,7 +29209,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: ' > > > - Item {MD004} {MD027...', + errorContext: ' > > > - Item {MD004} {MD005...', errorDetail: null, errorRange: [ 1, @@ -29036,7 +29228,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: ' > > - Item {MD004} {MD027}', + errorContext: ' > > - Item {MD004} {MD005} ...', errorDetail: null, errorRange: [ 1, @@ -29055,7 +29247,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: ' > > > - Item {MD004} {MD027...', + errorContext: ' > > > - Item {MD004} {MD005...', errorDetail: null, errorRange: [ 1, @@ -29074,7 +29266,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: ' > > > - Item {MD004} {MD027...', + errorContext: ' > > > - Item {MD004} {MD005...', errorDetail: null, errorRange: [ 1, @@ -29242,7 +29434,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: '> > - Item {MD004} {MD032}', + errorContext: '> > - Item {MD004} {MD005} {MD...', errorDetail: null, errorRange: null, fixInfo: { @@ -29275,7 +29467,7 @@ Generated by [AVA](https://avajs.dev). ], }, { - errorContext: '> + Item {MD032}', + errorContext: '> + Item {MD005} {MD032}', errorDetail: null, errorRange: null, fixInfo: { @@ -29395,40 +29587,40 @@ Generated by [AVA](https://avajs.dev). > Text␊ >␊ > > + Item {MD004}␊ - > > + Item {MD004} {MD027}␊ - > > + Item {MD004}␊ + > > + Item {MD004} {MD005} {MD027}␊ + > > + Item {MD004} {MD005}␊ > >␊ > > > + Item {MD004}␊ - > > > + Item {MD004} {MD027}␊ - > > > + Item {MD004} {MD027}␊ + > > > + Item {MD004} {MD005} {MD027}␊ + > > > + Item {MD004} {MD005} {MD027}␊ ␊ Text␊ ␊ > Text␊ > >␊ > > + Item {MD004} {MD032}␊ - > > + Item {MD004} {MD027}␊ - > > + Item {MD004} {MD032}␊ + > > + Item {MD004} {MD005} {MD027}␊ + > > + Item {MD004} {MD005} {MD032}␊ > > >␊ > > > + Item {MD004} {MD032}␊ - > > > + Item {MD004} {MD027}␊ - > > > + Item {MD004} {MD027}␊ + > > > + Item {MD004} {MD005} {MD027}␊ + > > > + Item {MD004} {MD005} {MD027}␊ ␊ Text␊ ␊ >+ Item␊ - > + Item␊ + > + Item {MD005}␊ >>␊ >>+ Item␊ - >> + Item␊ + >> + Item {MD005}␊ ␊ Text␊ ␊ >+ Item␊ - > + Item {MD032}␊ + > + Item {MD005} {MD032}␊ >>␊ >>+ Item {MD032}␊ - >> + Item␊ + >> + Item {MD005}␊ ␊ Text␊ ␊ diff --git a/test/snapshots/markdownlint-test-scenarios.js.snap b/test/snapshots/markdownlint-test-scenarios.js.snap index b541380b7..908cf8adc 100644 Binary files a/test/snapshots/markdownlint-test-scenarios.js.snap and b/test/snapshots/markdownlint-test-scenarios.js.snap differ