Reimplement MD023/heading-start-left using micromark tokens.

This commit is contained in:
David Anson 2024-06-17 22:18:58 -07:00
parent 26a0084515
commit 3b9a7fb2dd
5 changed files with 70 additions and 85 deletions

View file

@ -4521,9 +4521,8 @@ module.exports = {
const { addErrorContext, filterTokens } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -4531,31 +4530,34 @@ module.exports = {
"names": [ "MD023", "heading-start-left" ],
"description": "Headings must start at the beginning of the line",
"tags": [ "headings", "spaces" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD023(params, onError) {
filterTokens(params, "heading_open", function forToken(token) {
const { lineNumber, line } = token;
const match = line.match(spaceBeforeHeadingRe);
if (match) {
const [ prefixAndFirstChar, prefix ] = match;
let deleteCount = prefix.length;
const prefixLengthNoSpace = prefix.trimEnd().length;
if (prefixLengthNoSpace) {
deleteCount -= prefixLengthNoSpace - 1;
}
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading", "linePrefix", "setextHeading" ]
);
for (let i = 0; i < headings.length - 1; i++) {
if (
(headings[i].type === "linePrefix") &&
(headings[i + 1].type !== "linePrefix") &&
(headings[i].startLine === headings[i + 1].startLine)
) {
const { endColumn, startColumn, startLine } = headings[i];
const length = endColumn - startColumn;
addErrorContext(
onError,
lineNumber,
line,
null,
null,
[ 1, prefixAndFirstChar.length ],
startLine,
params.lines[startLine - 1],
true,
false,
[ startColumn, length ],
{
"editColumn": prefixLengthNoSpace + 1,
"deleteCount": deleteCount
});
"editColumn": startColumn,
"deleteCount": length
}
);
}
}
});
}
};

View file

@ -2,9 +2,8 @@
"use strict";
const { addErrorContext, filterTokens } = require("../helpers");
const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
const { addErrorContext } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -12,30 +11,33 @@ module.exports = {
"names": [ "MD023", "heading-start-left" ],
"description": "Headings must start at the beginning of the line",
"tags": [ "headings", "spaces" ],
"parser": "markdownit",
"parser": "micromark",
"function": function MD023(params, onError) {
filterTokens(params, "heading_open", function forToken(token) {
const { lineNumber, line } = token;
const match = line.match(spaceBeforeHeadingRe);
if (match) {
const [ prefixAndFirstChar, prefix ] = match;
let deleteCount = prefix.length;
const prefixLengthNoSpace = prefix.trimEnd().length;
if (prefixLengthNoSpace) {
deleteCount -= prefixLengthNoSpace - 1;
}
const headings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading", "linePrefix", "setextHeading" ]
);
for (let i = 0; i < headings.length - 1; i++) {
if (
(headings[i].type === "linePrefix") &&
(headings[i + 1].type !== "linePrefix") &&
(headings[i].startLine === headings[i + 1].startLine)
) {
const { endColumn, startColumn, startLine } = headings[i];
const length = endColumn - startColumn;
addErrorContext(
onError,
lineNumber,
line,
null,
null,
[ 1, prefixAndFirstChar.length ],
startLine,
params.lines[startLine - 1],
true,
false,
[ startColumn, length ],
{
"editColumn": prefixLengthNoSpace + 1,
"deleteCount": deleteCount
});
"editColumn": startColumn,
"deleteCount": length
}
);
}
}
});
}
};

View file

@ -16,7 +16,7 @@ Some text
```
* This is another case where MD023 shouldn't be triggered
# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)
# Test {MD022} Valid heading for CommonMark (see section 5.2)
# Test {MD022} {MD023} Also valid heading for CommonMark
<!-- markdownlint-configure-file {

View file

@ -623,7 +623,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
@ -5602,12 +5602,12 @@ Generated by [AVA](https://avajs.dev).
errorContext: '> ## Quoted indented sub-head...',
errorDetail: null,
errorRange: [
3,
1,
4,
],
fixInfo: {
deleteCount: 3,
editColumn: 2,
deleteCount: 1,
editColumn: 3,
},
lineNumber: 17,
ruleDescription: 'Headings must start at the beginning of the line',
@ -5621,12 +5621,12 @@ Generated by [AVA](https://avajs.dev).
errorContext: ' > ## Quoted indented sub-he...',
errorDetail: null,
errorRange: [
5,
1,
6,
],
fixInfo: {
deleteCount: 3,
editColumn: 4,
deleteCount: 1,
editColumn: 5,
},
lineNumber: 33,
ruleDescription: 'Headings must start at the beginning of the line',
@ -7141,7 +7141,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
@ -16650,7 +16650,7 @@ Generated by [AVA](https://avajs.dev).
{
errors: [
{
errorContext: '# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)',
errorContext: '# Test {MD022} Valid heading for CommonMark (see section 5.2)',
errorDetail: 'Expected: 1; Actual: 0; Above',
errorRange: null,
fixInfo: {
@ -16666,7 +16666,7 @@ Generated by [AVA](https://avajs.dev).
],
},
{
errorContext: '# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)',
errorContext: '# Test {MD022} Valid heading for CommonMark (see section 5.2)',
errorDetail: 'Expected: 1; Actual: 0; Below',
errorRange: null,
fixInfo: {
@ -16703,7 +16703,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
@ -16722,7 +16722,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
@ -16741,7 +16741,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: null,
errorRange: [
1,
2,
1,
],
fixInfo: {
deleteCount: 1,
@ -16755,35 +16755,16 @@ Generated by [AVA](https://avajs.dev).
'heading-start-left',
],
},
{
errorContext: ' # Test {MD022} {MD023} Valid...',
errorDetail: null,
errorRange: [
1,
3,
],
fixInfo: {
deleteCount: 2,
editColumn: 1,
},
lineNumber: 19,
ruleDescription: 'Headings must start at the beginning of the line',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md023.md',
ruleNames: [
'MD023',
'heading-start-left',
],
},
{
errorContext: ' # Test {MD022} {MD023} Als...',
errorDetail: null,
errorRange: [
1,
5,
3,
2,
],
fixInfo: {
deleteCount: 4,
editColumn: 1,
deleteCount: 2,
editColumn: 3,
},
lineNumber: 20,
ruleDescription: 'Headings must start at the beginning of the line',
@ -16813,7 +16794,7 @@ Generated by [AVA](https://avajs.dev).
* This is another case where MD023 shouldn't be triggered␊
# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)␊
# Test {MD022} Valid heading for CommonMark (see section 5.2)␊
# Test {MD022} {MD023} Also valid heading for CommonMark␊