Skip to content

Commit cb6b437

Browse files
authored
fix: no-missing-label-refs rule does not respect escaping (#348)
* fix: `no-missing-label-refs` rule does not respect escaping * wip: add an invalid test * wip: handle edge cases
1 parent 8dd5b4c commit cb6b437

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function findMissingReferences(node, nodeText) {
4040
* `left` is the content between the first brackets. It can be empty.
4141
* `right` is the content between the second brackets. It can be empty, and it can be undefined.
4242
*/
43-
const labelPattern = /\[(?<left>[^\]]*)\](?:\[(?<right>[^\]]*)\])?/dgu;
43+
const labelPattern =
44+
/(?<!\\)\[(?<left>(?:\\.|[^\]])*)(?<!\\)\](?<!\\)(?:\[(?<right>(?:\\.|[^\]])*)(?<!\\)\])?/dgu;
4445

4546
let match;
4647

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ ruleTester.run("no-missing-label-refs", rule, {
4444
"foo][bar]\n\n[bar]: http://bar.com",
4545
"foo][bar][baz]\n\n[baz]: http://baz.com",
4646
"[][foo]\n\n[foo]: http://foo.com",
47+
"\\[\\]",
48+
"[\\]",
49+
"\\[]",
50+
"\\[escaped\\]",
51+
"\\[escaped]",
52+
"[escaped\\]",
53+
"\\[escaped\\]\\[escaped\\]",
54+
"\\[escaped\\]\\[escaped]",
55+
"[escaped\\]\\[escaped\\]",
56+
"\\[escaped]\\[escaped]",
57+
"[escaped\\][escaped\\]",
4758
],
4859
invalid: [
4960
{
@@ -331,5 +342,44 @@ ruleTester.run("no-missing-label-refs", rule, {
331342
},
332343
],
333344
},
345+
{
346+
code: "\\[[foo]\\]",
347+
errors: [
348+
{
349+
messageId: "notFound",
350+
data: { label: "foo" },
351+
line: 1,
352+
column: 4,
353+
endLine: 1,
354+
endColumn: 7,
355+
},
356+
],
357+
},
358+
{
359+
code: "[\\[foo\\]]",
360+
errors: [
361+
{
362+
messageId: "notFound",
363+
data: { label: "\\[foo\\]" },
364+
line: 1,
365+
column: 2,
366+
endLine: 1,
367+
endColumn: 9,
368+
},
369+
],
370+
},
371+
{
372+
code: "[\\[\\[foo\\]\\]]",
373+
errors: [
374+
{
375+
messageId: "notFound",
376+
data: { label: "\\[\\[foo\\]\\]" },
377+
line: 1,
378+
column: 2,
379+
endLine: 1,
380+
endColumn: 13,
381+
},
382+
],
383+
},
334384
],
335385
});

0 commit comments

Comments
 (0)