Skip to content

feat: add allow options to no-empty-definitions #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
36 changes: 29 additions & 7 deletions docs/rules/no-empty-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Definitions with an empty URL or only an empty fragment (`#`), as well as footno
>
> Footnotes are only supported when using `language` mode [`markdown/gfm`](/README.md#languages).

This rule warns when it finds definitions where the URL is either not specified or contains only an empty fragment (`#`). It also warns for empty footnote definitions by default.
This rule warns when it finds definitions where the URL is either not specified or contains only an empty fragment (`#`). It also warns for empty footnote definitions by default. Please note that this rule doesn't report definition-style comments (e.g., `[//]: # (This is a comment)`) by default.

Examples of **incorrect** code for this rule:

Expand All @@ -43,22 +43,44 @@ Examples of **correct** code for this rule:

[earth]: https://example.com/earth/
[moon]: #section
[//]: <> (This is a comment 1)
[//]: # (This is a comment 2)
[^note]: This is a footnote.
```

## Options

The following options are available on this rule:

* `checkFootnoteDefinitions: boolean` - When set to `false`, the rule will not report empty footnote definitions. (default: `true`).
- `allowDefinitions: Array<string>` - When specified, empty definitions are allowed if they match one of the identifiers in this array. This is useful for ignoring definitions that are intentionally empty. (default: `["//"]`)

Examples of **correct** code for this rule with `checkFootnoteDefinitions: false`:
Examples of **correct** code when configured as `"no-empty-definitions": ["error", { allowDefinitions: ["moon"] }]`:

```markdown
<!-- eslint markdown/no-empty-definitions: ["error", { checkFootnoteDefinitions: false }] -->
```markdown
<!-- eslint markdown/no-empty-definitions: ["error", { allowDefinitions: ["moon"] }] -->

[^note]:
```
[moon]: <>
```

- `allowFootnoteDefinitions: Array<string>` - When specified, empty footnote definitions are allowed if they match one of the identifiers in this array. This is useful for ignoring footnote definitions that are intentionally empty. (default: `[]`)

Examples of **correct** code when configured as `"no-empty-definitions": ["error", { allowFootnoteDefinitions: ["note"] }]`:

```markdown
<!-- eslint markdown/no-empty-definitions: ["error", { allowFootnoteDefinitions: ["note"] }] -->

[^note]:
```

- `checkFootnoteDefinitions: boolean` - When set to `false`, the rule will not report empty footnote definitions. (default: `true`)

Examples of **correct** code when configured as `"no-empty-definitions": ["error", { checkFootnoteDefinitions: false }]`:

```markdown
<!-- eslint markdown/no-empty-definitions: ["error", { checkFootnoteDefinitions: false }] -->

[^note]:
```

## When Not to Use It

Expand Down
34 changes: 31 additions & 3 deletions src/rules/no-empty-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"emptyDefinition" | "emptyFootnoteDefinition"} NoEmptyDefinitionsMessageIds
* @typedef {[{ checkFootnoteDefinitions?: boolean }]} NoEmptyDefinitionsOptions
* @typedef {[{ allowDefinitions?: string[], allowFootnoteDefinitions?: string[], checkFootnoteDefinitions?: boolean }]} NoEmptyDefinitionsOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoEmptyDefinitionsOptions, MessageIds: NoEmptyDefinitionsMessageIds }>} NoEmptyDefinitionsRuleDefinition
*/

Expand Down Expand Up @@ -56,6 +56,20 @@ export default {
{
type: "object",
properties: {
allowDefinitions: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
allowFootnoteDefinitions: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
checkFootnoteDefinitions: {
type: "boolean",
},
Expand All @@ -64,15 +78,28 @@ export default {
},
],

defaultOptions: [{ checkFootnoteDefinitions: true }],
defaultOptions: [
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

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

Because ESLint does not deep-merge defaultOptions with a user-supplied options object, users who specify only one property will lose the defaults for the others. Consider destructuring context.options[0] with in-function defaults (e.g., const { allowDefinitions = ["//"], allowFootnoteDefinitions = [], checkFootnoteDefinitions = true } = context.options[0] || {};) to ensure all defaults apply even when partial options are provided.

Copilot uses AI. Check for mistakes.

{
allowDefinitions: ["//"],
allowFootnoteDefinitions: [],
checkFootnoteDefinitions: true,
},
],
},

create(context) {
const allowDefinitions = new Set(context.options[0].allowDefinitions);
const allowFootnoteDefinitions = new Set(
context.options[0].allowFootnoteDefinitions,
);
const [{ checkFootnoteDefinitions }] = context.options;

return {
definition(node) {
if (!node.url || node.url === "#") {
if (
(!node.url || node.url === "#") &&
!allowDefinitions.has(node.identifier)
) {
context.report({
loc: node.position,
messageId: "emptyDefinition",
Expand All @@ -83,6 +110,7 @@ export default {
footnoteDefinition(node) {
if (
checkFootnoteDefinitions &&
!allowFootnoteDefinitions.has(node.identifier) &&
(node.children.length === 0 ||
node.children.every(
child =>
Expand Down
71 changes: 71 additions & 0 deletions tests/rules/no-empty-definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ ruleTester.run("no-empty-definitions", rule, {
"[foo]: #bar",
"[foo]: http://bar.com",
"[foo]: <https://bar.com>",
"[//]: # (This is a comment 1)",
"[//]: <> (This is a comment 2)",
"[^note]: This is a footnote.",
"[^note]: ![]()",
"[^note]: [text](url)",
Expand All @@ -47,10 +49,31 @@ ruleTester.run("no-empty-definitions", rule, {
[^foo]: <!-- comm
ent --> content <!-- comment -->
`,
{
code: "[foo]: #",
options: [{ allowDefinitions: ["foo"] }],
},
{
code: "[bar]: <>",
options: [{ allowDefinitions: ["bar"] }],
},
{
code: "[foo]: #\n[bar]: <>",
options: [{ allowDefinitions: ["foo", "bar"] }],
},
{
code: "[^note]:",
options: [{ checkFootnoteDefinitions: false }],
},
{
code: "[^note]:",
options: [
{
checkFootnoteDefinitions: true,
allowFootnoteDefinitions: ["note"],
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -226,5 +249,53 @@ ruleTester.run("no-empty-definitions", rule, {
},
],
},
{
code: "[//]: #\n[foo]: #",
errors: [
{
messageId: "emptyDefinition",
line: 2,
column: 1,
endLine: 2,
endColumn: 9,
},
],
},
{
code: "[foo]: #",
options: [
{
allowDefinitions: ["bar"],
allowFootnoteDefinitions: ["foo"],
},
],
errors: [
{
messageId: "emptyDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: "[^foo]:",
options: [
{
allowDefinitions: ["foo"],
allowFootnoteDefinitions: ["bar"],
},
],
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 8,
},
],
},
],
});