Skip to content
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
32 changes: 28 additions & 4 deletions docs/rules/no-empty-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ 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 allows you to specify a label as a placeholder for a URL in both links and images, or as a footnote reference, using square brackets. For example:

```markdown
[ESLint][eslint]

[eslint]: https://eslint.org

[ESLint][^eslint]

[^eslint]: Find and fix problmes in your JavaScript code
```

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.
Definitions with an empty URL or only an empty fragment (`#`), as well as footnote definitions with no content, are usually mistakes and do not provide useful information.

## Rule Details

This rule warns when it finds definitions where the URL is either not specified or contains only an empty fragment (`#`).
> [!IMPORTANT] <!-- eslint-disable-line -- This should be fixed in https://github.com/eslint/markdown/issues/294 -->
>
> 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.

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

Expand All @@ -25,15 +33,31 @@ Examples of **incorrect** code for this rule:

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

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

```markdown
<!-- eslint markdown/no-empty-definitions: "error" -->

[earth]: https://example.com/earth/
[moon]: #section
[^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`).

Examples of **correct** code for this rule with `checkFootnoteDefinitions: false`:

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

[^note]:
```

## When Not to Use It
Expand Down
58 changes: 56 additions & 2 deletions src/rules/no-empty-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,29 @@
//-----------------------------------------------------------------------------

/**
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>}
* NoEmptyDefinitionsRuleDefinition
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"emptyDefinition" | "emptyFootnoteDefinition"} NoEmptyDefinitionsMessageIds
* @typedef {[{ checkFootnoteDefinitions?: boolean }]} NoEmptyDefinitionsOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoEmptyDefinitionsOptions, MessageIds: NoEmptyDefinitionsMessageIds }>} NoEmptyDefinitionsRuleDefinition
*/

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------

const htmlCommentPattern = /<!--[\s\S]*?-->/gu;

/**
* Checks if a string contains only HTML comments.
* @param {string} value The input string to check.
* @returns {boolean} True if the string contains only HTML comments, false otherwise.
*/
function isOnlyComments(value) {
const withoutComments = value.replace(htmlCommentPattern, "");

return withoutComments.trim().length === 0;
}

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
Expand All @@ -29,10 +48,28 @@ export default {

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

schema: [
{
type: "object",
properties: {
checkFootnoteDefinitions: {
type: "boolean",
},
},
additionalProperties: false,
},
],

defaultOptions: [{ checkFootnoteDefinitions: true }],
},

create(context) {
const [{ checkFootnoteDefinitions }] = context.options;

return {
definition(node) {
if (!node.url || node.url === "#") {
Expand All @@ -42,6 +79,23 @@ export default {
});
}
},

footnoteDefinition(node) {
if (
checkFootnoteDefinitions &&
(node.children.length === 0 ||
node.children.every(
child =>
child.type === "html" &&
isOnlyComments(child.value),
))
) {
context.report({
loc: node.position,
messageId: "emptyFootnoteDefinition",
});
}
},
};
},
};
151 changes: 150 additions & 1 deletion tests/rules/no-empty-definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ruleTester = new RuleTester({
plugins: {
markdown,
},
language: "markdown/commonmark",
language: "markdown/gfm",
});

ruleTester.run("no-empty-definitions", rule, {
Expand All @@ -29,6 +29,28 @@ ruleTester.run("no-empty-definitions", rule, {
"[foo]: #bar",
"[foo]: http://bar.com",
"[foo]: <https://bar.com>",
"[^note]: This is a footnote.",
"[^note]: ![]()",
"[^note]: [text](url)",
"[^note]:\n Content",
"[^note]:\n > blockquote",
"[^note]: <span></span>",
"\\[^note]:",
"[\\^note]:",
"[^note\\]:",
"[^note]\\:",
"[^foo]: <span></span> <!-- comment -->",
"[^foo]: content <!-- comment -->",
"[^foo]: <!-- comment --> content",
"[^foo]: <!-- comment --> content <!-- comment -->",
dedent`
[^foo]: <!-- comm
ent --> content <!-- comment -->
`,
{
code: "[^note]:",
options: [{ checkFootnoteDefinitions: false }],
},
],
invalid: [
{
Expand Down Expand Up @@ -77,5 +99,132 @@ ruleTester.run("no-empty-definitions", rule, {
},
],
},
{
code: "[^note]:",
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: "[^note]: ",
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 12,
},
],
},
{
code: "[^note]:\n",
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: "[^a]:\n[^b]:",
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 6,
},
{
messageId: "emptyFootnoteDefinition",
line: 2,
column: 1,
endLine: 2,
endColumn: 6,
},
],
},
{
code: "[foo]: #\n[^note]:",
errors: [
{
messageId: "emptyDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
{
messageId: "emptyFootnoteDefinition",
line: 2,
column: 1,
endLine: 2,
endColumn: 9,
},
],
},
{
code: "[foo]: #\n[^note]:",
options: [{ checkFootnoteDefinitions: false }],
errors: [
{
messageId: "emptyDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: "[^foo]: <!-- comment -->",
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 1,
endColumn: 25,
},
],
},
{
code: dedent`
[^foo]: <!-- comment
-->`,
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 2,
endColumn: 8,
},
],
},
{
code: dedent`
[^foo]: <!-- comment -->
<!-- another comment -->`,
errors: [
{
messageId: "emptyFootnoteDefinition",
line: 1,
column: 1,
endLine: 2,
endColumn: 29,
},
],
},
],
});