Skip to content

Commit ed01747

Browse files
authored
fix: no-invalid-label-refs do not report correct position (#366)
1 parent 36800b4 commit ed01747

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/rules/no-invalid-label-refs.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { findOffsets, illegalShorthandTailPattern } from "../util.js";
1515

1616
/** @typedef {import("unist").Position} Position */
1717
/** @typedef {import("mdast").Text} TextNode */
18+
/** @typedef {Parameters<import("../types.ts").MarkdownRuleDefinition['create']>[0]['sourceCode']} sourceCode */
1819
/**
1920
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>}
2021
* NoInvalidLabelRuleDefinition
@@ -30,10 +31,12 @@ const labelPattern = /\]\[([^\]]+)\]/u;
3031
/**
3132
* Finds missing references in a node.
3233
* @param {TextNode} node The node to check.
33-
* @param {string} docText The text of the node.
34+
* @param {sourceCode} sourceCode The Markdown source code object.
3435
* @returns {Array<{label:string,position:Position}>} The missing references.
3536
*/
36-
function findInvalidLabelReferences(node, docText) {
37+
function findInvalidLabelReferences(node, sourceCode) {
38+
const nodeText = sourceCode.getText(node);
39+
const docText = sourceCode.text;
3740
const invalid = [];
3841
let startIndex = 0;
3942
const offset = node.position.start.offset;
@@ -47,8 +50,8 @@ function findInvalidLabelReferences(node, docText) {
4750
* It then moves the start index to the end of the label reference and
4851
* continues searching the text until the end of the text is found.
4952
*/
50-
while (startIndex < node.value.length) {
51-
const value = node.value.slice(startIndex);
53+
while (startIndex < nodeText.length) {
54+
const value = nodeText.slice(startIndex);
5255
const match = value.match(labelPattern);
5356

5457
if (!match) {
@@ -87,11 +90,11 @@ function findInvalidLabelReferences(node, docText) {
8790

8891
// find location of [ in the document text
8992
const { lineOffset: startLineOffset, columnOffset: startColumnOffset } =
90-
findOffsets(node.value, nodeMatchIndex + 1);
93+
findOffsets(nodeText, nodeMatchIndex + 1);
9194

9295
// find location of [ in the document text
9396
const { lineOffset: endLineOffset, columnOffset: endColumnOffset } =
94-
findOffsets(node.value, nodeMatchIndex + match[0].length);
97+
findOffsets(nodeText, nodeMatchIndex + match[0].length);
9598

9699
const startLine = nodeStartLine + startLineOffset;
97100
const startColumn = nodeStartColumn + startColumnOffset;
@@ -147,7 +150,7 @@ export default {
147150
text(node) {
148151
const invalidReferences = findInvalidLabelReferences(
149152
node,
150-
sourceCode.text,
153+
sourceCode,
151154
);
152155

153156
for (const invalidReference of invalidReferences) {

tests/rules/no-invalid-label-refs.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,44 @@ ruleTester.run("no-invalid-label-refs", rule, {
127127
},
128128
],
129129
},
130+
{
131+
code: "eslint [eslint][ ]",
132+
errors: [
133+
{
134+
messageId: "invalidLabelRef",
135+
data: { label: "eslint" },
136+
line: 1,
137+
column: 16,
138+
endLine: 1,
139+
endColumn: 19,
140+
},
141+
],
142+
},
143+
{
144+
code: "\\\\eslint [eslint][ ]",
145+
errors: [
146+
{
147+
messageId: "invalidLabelRef",
148+
data: { label: "eslint" },
149+
line: 1,
150+
column: 18,
151+
endLine: 1,
152+
endColumn: 21,
153+
},
154+
],
155+
},
156+
{
157+
code: "es\\\\lint\\\\ [eslint][ ]",
158+
errors: [
159+
{
160+
messageId: "invalidLabelRef",
161+
data: { label: "eslint" },
162+
line: 1,
163+
column: 20,
164+
endLine: 1,
165+
endColumn: 23,
166+
},
167+
],
168+
},
130169
],
131170
});

0 commit comments

Comments
 (0)