Skip to content

Commit

Permalink
Update MD032/blanks-around-lists to ignore (end-appended) undefined r…
Browse files Browse the repository at this point in the history
…eference tokens when determining the last line of a list (fixes #1453).
  • Loading branch information
DavidAnson committed Dec 17, 2024
1 parent 599b687 commit 8ad4698
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
9 changes: 6 additions & 3 deletions helpers/micromark-helpers.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ function addRangeToSet(set, start, end) {
* Filter a list of Micromark tokens by predicate.
*
* @param {Token[]} tokens Micromark tokens.
* @param {AllowedPredicate} [allowed] Allowed token predicate.
* @param {AllowedPredicate} allowed Allowed token predicate.
* @param {TransformPredicate} [transformChildren] Transform predicate.
* @returns {Token[]} Filtered tokens.
*/
function filterByPredicate(tokens, allowed, transformChildren) {
allowed = allowed || (() => true);
const result = [];
const queue = [
{
Expand Down Expand Up @@ -278,7 +277,11 @@ const nonContentTokens = new Set([
"lineEnding",
"lineEndingBlank",
"linePrefix",
"listItemIndent"
"listItemIndent",
"undefinedReference",
"undefinedReferenceCollapsed",
"undefinedReferenceFull",
"undefinedReferenceShortcut"
]);

module.exports = {
Expand Down
13 changes: 7 additions & 6 deletions lib/md032.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ export default {
}

// Find the "visual" end of the list
const flattenedChildren = filterByPredicate(
list.children,
(token) => !nonContentTokens.has(token.type),
(token) => nonContentTokens.has(token.type) ? [] : token.children
);
let endLine = list.endLine;
const flattenedChildren = filterByPredicate(list.children);
for (const child of flattenedChildren.reverse()) {
if (!nonContentTokens.has(child.type)) {
endLine = child.endLine;
break;
}
if (flattenedChildren.length > 0) {
endLine = flattenedChildren[flattenedChildren.length - 1].endLine;
}

// Look for a blank line below the list
Expand Down
5 changes: 5 additions & 0 deletions test/lists_without_blank_lines.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ text

<p>* Not a list</p>

1. Undefined reference token
<pre>
[()]
</pre>

<!-- markdownlint-configure-file {
"no-inline-html": false,
"ul-style": false,
Expand Down
7 changes: 6 additions & 1 deletion test/snapshots/markdownlint-test-scenarios.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -37346,7 +37346,7 @@ Generated by [AVA](https://avajs.dev).
insertText: `␊
`,
},
lineNumber: 98,
lineNumber: 103,
ruleDescription: 'Files should end with a single newline character',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md047.md',
ruleNames: [
Expand Down Expand Up @@ -37457,6 +37457,11 @@ Generated by [AVA](https://avajs.dev).
<p>* Not a list</p>␊
1. Undefined reference token␊
<pre>␊
[()]␊
</pre>␊
<!-- markdownlint-configure-file {␊
"no-inline-html": false,␊
"ul-style": false,␊
Expand Down
Binary file modified test/snapshots/markdownlint-test-scenarios.mjs.snap
Binary file not shown.

0 comments on commit 8ad4698

Please sign in to comment.