Skip to content

feat: add no-empty-definitions rule #364

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

Merged
merged 2 commits into from
May 14, 2025
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default defineConfig([
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Require languages for fenced code blocks | yes |
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one | yes |
| [`no-duplicate-headings`](./docs/rules/no-duplicate-headings.md) | Disallow duplicate headings in the same document | no |
| [`no-empty-definitions`](./docs/rules/no-empty-definitions.md) | Disallow empty definitions | yes |
| [`no-empty-images`](./docs/rules/no-empty-images.md) | Disallow empty images | yes |
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links | yes |
| [`no-html`](./docs/rules/no-html.md) | Disallow HTML tags | no |
Expand Down
41 changes: 41 additions & 0 deletions docs/rules/no-empty-definitions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# no-empty-definitions

Disallow empty definitions.

## Background

Markdown allows you to specify a label as a placeholder for a URL in both links and images using square brackets, such as:

```markdown
[ESLint][eslint]

[eslint]: https://eslint.org
```

If the definition's URL is empty or only contains an empty fragment (`#`), then it's not providing any useful information and could be a mistake.

## Rule Details

This rule warns when it finds definitions where the URL is either not specified or contains only an empty fragment (`#`).

Examples of incorrect code:

```markdown
[earth]: <>
[earth]: #
```

Examples of correct code:

```markdown
[earth]: https://example.com/earth/
[earth]: #section
```

## When Not to Use It

If you aren't concerned with empty definitions, you can safely disable this rule.

## Prior Art

* [remark-lint-no-empty-url](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-no-empty-url)
47 changes: 47 additions & 0 deletions src/rules/no-empty-definitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @fileoverview Rule to prevent empty definitions in Markdown.
* @author Pixel998
*/

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------

/**
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>}
* NoEmptyDefinitionsRuleDefinition
*/

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------

/** @type {NoEmptyDefinitionsRuleDefinition} */
export default {
meta: {
type: "problem",

docs: {
recommended: true,
description: "Disallow empty definitions",
url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-empty-definitions.md",
},

messages: {
emptyDefinition: "Unexpected empty definition found.",
},
},

create(context) {
return {
definition(node) {
if (!node.url || node.url === "#") {
context.report({
loc: node.position,
messageId: "emptyDefinition",
});
}
},
};
},
};
81 changes: 81 additions & 0 deletions tests/rules/no-empty-definitions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @fileoverview Tests for no-empty-definitions rule.
* @author Pixel998
*/

//------------------------------------------------------------------------------
// Imports
//------------------------------------------------------------------------------

import rule from "../../src/rules/no-empty-definitions.js";
import markdown from "../../src/index.js";
import { RuleTester } from "eslint";
import dedent from "dedent";

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
plugins: {
markdown,
},
language: "markdown/commonmark",
});

ruleTester.run("no-empty-definitions", rule, {
valid: [
"[foo]: bar",
"[foo]: #bar",
"[foo]: http://bar.com",
"[foo]: <https://bar.com>",
],
invalid: [
{
code: "[foo]: #",
errors: [
{
messageId: "emptyDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: "[foo]: <>",
errors: [
{
messageId: "emptyDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 10,
},
],
},
{
code: dedent`
[foo]: #
[bar]: <>
`,
errors: [
{
messageId: "emptyDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
{
messageId: "emptyDefinition",
line: 2,
column: 1,
endLine: 2,
endColumn: 10,
},
],
},
],
});