-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathno-missing-label-refs.js
More file actions
181 lines (153 loc) · 4.89 KB
/
Copy pathno-missing-label-refs.js
File metadata and controls
181 lines (153 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @fileoverview Rule to prevent missing label references in Markdown.
* @author Nicholas C. Zakas
*/
//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------
import { illegalShorthandTailPattern } from "../util.js";
//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------
/**
* @import { Position } from "unist";
* @import { Text } from "mdast";
* @import { MarkdownRuleDefinition } from "../types.js";
* @import { MarkdownSourceCode } from "../language/markdown-source-code.js";
* @typedef {"notFound"} NoMissingLabelRefsMessageIds
* @typedef {[{ allowLabels?: string[] }]} NoMissingLabelRefsOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoMissingLabelRefsOptions, MessageIds: NoMissingLabelRefsMessageIds }>} NoMissingLabelRefsRuleDefinition
*/
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
/**
* Finds missing references in a node.
* @param {Text} node The node to check.
* @param {MarkdownSourceCode} sourceCode The Markdown source code object.
* @returns {Array<{label:string,position:Position}>} The missing references.
*/
function findMissingReferences(node, sourceCode) {
/** @type {Array<{label:string,position:Position}>} */
const missing = [];
const nodeText = sourceCode.getText(node);
/**
* Matches substrings like `"[foo]"`, `"[]"`, `"[foo][bar]"`, `"[foo][]"`, `"[][bar]"`, or `"[][]"`.
* `left` is the content between the first brackets. It can be empty.
* `right` is the content between the second brackets. It can be empty, and it can be undefined.
*/
const labelPattern =
/(?<=(?<!\\)(?:\\{2})*)\[(?<left>(?:\\.|[^[\]\\])*)\](?:\[(?<right>(?:\\.|[^\]\\])*)\])?/dgu;
/** @type {RegExpExecArray | null} */
let match;
/*
* This loop searches the text inside the node for sequences that
* look like label references and reports an error for each one found.
*/
while ((match = labelPattern.exec(nodeText))) {
// skip illegal shorthand tail -- handled by no-invalid-label-refs
if (illegalShorthandTailPattern.test(match[0])) {
continue;
}
const { left, right } = match.groups;
// `[][]` or `[]`
if (!left && !right) {
continue;
}
let label, labelIndices;
if (right) {
label = right;
labelIndices = match.indices.groups.right;
} else {
label = left;
labelIndices = match.indices.groups.left;
}
const startOffset = labelIndices[0] + node.position.start.offset;
const endOffset = labelIndices[1] + node.position.start.offset;
missing.push({
label: label.trim(),
position: {
start: sourceCode.getLocFromIndex(startOffset),
end: sourceCode.getLocFromIndex(endOffset),
},
});
}
return missing;
}
//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
export default /** @satisfies {NoMissingLabelRefsRuleDefinition} */ ({
meta: {
type: "problem",
docs: {
recommended: true,
description: "Disallow missing label references",
url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-missing-label-refs.md",
},
schema: [
{
type: "object",
properties: {
allowLabels: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
},
additionalProperties: false,
},
],
defaultOptions: [
{
allowLabels: [],
},
],
messages: {
notFound: "Label reference '{{label}}' not found.",
},
},
create(context) {
const { sourceCode } = context;
const allowLabels = new Set(context.options[0].allowLabels);
/** @type {Array<{label:string,position:Position}>} */
let allMissingReferences = [];
return {
"root:exit"() {
for (const missingReference of allMissingReferences) {
context.report({
loc: missingReference.position,
messageId: "notFound",
data: {
label: missingReference.label,
},
});
}
},
text(node) {
const missingReferences = findMissingReferences(
node,
sourceCode,
);
for (const missingReference of missingReferences) {
if (!allowLabels.has(missingReference.label)) {
allMissingReferences.push(missingReference);
}
}
},
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,
);
},
};
},
});