Skip to content
Open
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
18 changes: 18 additions & 0 deletions docs/rules/no-missing-label-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ Examples of **incorrect** code for this rule:
[eslint]
```

## Options

The following options are available on this rule:

* `allowLabels: Array<string>` - labels to allow when checking for missing label references. (default: `[]`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your original issue you mentioned wanting to use ignoreLabels. Why the change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially called it ignoreLabels, but @lumirlumir suggested being consistent with other rules, so I renamed it. Would you like me to revert the change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked @jaymarvelz (and other team members) for their opinion on #513 (comment), how about renaming it to allow for consistency across the Markdown rules.

If you feel ignore is more appropriate here, please disregard my suggestion and feel free to keep ignore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowLabels amkes sense to me 👍🏻


Examples of **correct** code when configured as `"no-missing-label-refs": ["error", { allowLabels: ["eslint"] }]`:

```markdown
<!-- eslint markdown/no-missing-label-refs: ["error", { allowLabels: ["eslint"] }] -->

[ESLint][eslint]

[eslint][]

[eslint]
```

## When Not to Use It

If you aren't concerned with missing label references, you can safely disable this rule.
Expand Down
36 changes: 33 additions & 3 deletions src/rules/no-missing-label-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { findOffsets, illegalShorthandTailPattern } from "../util.js";
* @import { Text } from "mdast";
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"notFound"} NoMissingLabelRefsMessageIds
* @typedef {[]} NoMissingLabelRefsOptions
* @typedef {[{ allowLabels?: string[] }]} NoMissingLabelRefsOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoMissingLabelRefsOptions, MessageIds: NoMissingLabelRefsMessageIds }>} NoMissingLabelRefsRuleDefinition
*/

Expand Down Expand Up @@ -118,13 +118,36 @@ export default {
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 = [];
Expand All @@ -143,9 +166,16 @@ export default {
},

text(node) {
allMissingReferences.push(
...findMissingReferences(node, sourceCode.getText(node)),
const missingReferences = findMissingReferences(
node,
sourceCode.getText(node),
);

for (const missingReference of missingReferences) {
if (!allowLabels.has(missingReference.label)) {
allMissingReferences.push(missingReference);
}
}
},

definition(node) {
Expand Down
125 changes: 125 additions & 0 deletions tests/rules/no-missing-label-refs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import rule from "../../src/rules/no-missing-label-refs.js";
import markdown from "../../src/index.js";
import { Linter, RuleTester } from "eslint";
import dedent from "dedent";

//------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -55,6 +56,46 @@ ruleTester.run("no-missing-label-refs", rule, {
"[escaped\\]\\[escaped\\]",
"\\[escaped]\\[escaped]",
"[escaped\\][escaped\\]",
{
code: "[foo][bar]",
options: [{ allowLabels: ["bar"] }],
},
{
code: "![foo][bar]",
options: [{ allowLabels: ["bar"] }],
},
{
code: "[foo][]",
options: [{ allowLabels: ["foo"] }],
},
{
code: "![foo][]",
options: [{ allowLabels: ["foo"] }],
},
{
code: "[foo]",
options: [{ allowLabels: ["foo"] }],
},
{
code: "![foo]",
options: [{ allowLabels: ["foo"] }],
},
{
code: "[foo]\n[bar]",
options: [{ allowLabels: ["foo", "bar"] }],
},
{
code: "[Foo][]\n[Bar][]",
options: [{ allowLabels: ["Foo", "Bar"] }],
},
{
code: dedent`
- [x] Checked
- [-] In progress
`,
language: "markdown/gfm",
options: [{ allowLabels: ["-"] }],
},
],
invalid: [
{
Expand Down Expand Up @@ -381,6 +422,90 @@ ruleTester.run("no-missing-label-refs", rule, {
},
],
},
{
code: "[foo][bar]",
options: [{ allowLabels: ["baz"] }],
errors: [
{
messageId: "notFound",
data: { label: "bar" },
line: 1,
column: 7,
endLine: 1,
endColumn: 10,
},
],
},
{
code: "![foo][bar]",
options: [{ allowLabels: ["foo"] }],
errors: [
{
messageId: "notFound",
data: { label: "bar" },
line: 1,
column: 8,
endLine: 1,
endColumn: 11,
},
],
},
{
code: "[foo][]",
options: [{ allowLabels: ["bar"] }],
errors: [
{
messageId: "notFound",
data: { label: "foo" },
line: 1,
column: 2,
endLine: 1,
endColumn: 5,
},
],
},
{
code: "[foo]\n[bar]",
options: [{ allowLabels: ["foo"] }],
errors: [
{
messageId: "notFound",
data: { label: "bar" },
line: 2,
column: 2,
endLine: 2,
endColumn: 5,
},
],
},
{
code: "[Foo][]",
options: [{ allowLabels: ["foo"] }],
errors: [
{
messageId: "notFound",
data: { label: "Foo" },
line: 1,
column: 2,
endLine: 1,
endColumn: 5,
},
],
},
{
code: "[Foo][foo]\n[Bar][]",
options: [{ allowLabels: ["Bar"] }],
errors: [
{
messageId: "notFound",
data: { label: "foo" },
line: 1,
column: 7,
endLine: 1,
endColumn: 10,
},
],
},
],
});

Expand Down