Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/rules/no-missing-label-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ export default /** @satisfies {NoMissingLabelRefsRuleDefinition} */ ({
const allowLabels = new Set(context.options[0].allowLabels);

/** @type {Array<{label:string,position:Position}>} */
let allMissingReferences = [];
const allMissingReferences = [];

return {
"root:exit"() {
for (const missingReference of allMissingReferences) {
if (allowLabels.has(missingReference.label)) {
continue;
}

context.report({
loc: missingReference.position,
messageId: "notFound",
Expand All @@ -168,15 +172,7 @@ export default /** @satisfies {NoMissingLabelRefsRuleDefinition} */ ({
},

definition(node) {
/*
* Sometimes a poorly-formatted link will end up a text node instead of a link node
* even though the label definition exists. Here, we remove any missing references
* that have a matching label definition.
*/
allMissingReferences = allMissingReferences.filter(
missingReference =>
missingReference.label !== node.identifier,
);
allowLabels.add(node.identifier);
},
};
},
Expand Down
33 changes: 33 additions & 0 deletions tests/rules/no-missing-label-refs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ ruleTester.run("no-missing-label-refs", rule, {
"foo][bar][baz]\n\n[baz]: http://baz.com",
"[][foo]\n\n[foo]: http://foo.com",
"[foo][bar\\]\n\n[foo]: http://foo.com",
"[foo]: http://foo.com\n\n[foo][bar",
"[foo]: http://foo.com\n\n[foo][bar\\]",
"[foo][bar\n\n[foo]: http://foo.com\n\n[foo][bar",
"[foo][bar\\]\n\n[foo]: http://foo.com\n\n[foo][bar\\]",
"\\[\\]",
`${"\\".repeat(3)}[${"\\".repeat(3)}]`,
`${"\\".repeat(5)}[${"\\".repeat(5)}]`,
Expand Down Expand Up @@ -257,6 +261,35 @@ ruleTester.run("no-missing-label-refs", rule, {
},
],
},
{
code: "[foo]\n[bar]\n[foo]",
errors: [
{
messageId: "notFound",
data: { label: "foo" },
line: 1,
column: 2,
endLine: 1,
endColumn: 5,
},
{
messageId: "notFound",
data: { label: "bar" },
line: 2,
column: 2,
endLine: 2,
endColumn: 5,
},
{
messageId: "notFound",
data: { label: "foo" },
line: 3,
column: 2,
endLine: 3,
endColumn: 5,
},
],
},
{
code: "- - - [foo]",
errors: [
Expand Down
Loading