Skip to content

Commit d189c5e

Browse files
ckhe1215lumirlumir
andauthored
feat: add option to no-duplicate/unused-definitions rules (#616)
* feat: add checkFootnoteDefinitions option to no-duplicate-definitions * feat: add checkFootnoteDefinitions option to no-unused-definitions * refactor: simplify footnote definition condition checks * Add blank line docs/rules/no-duplicate-definitions.md example Co-authored-by: 루밀LuMir <rpfos@naver.com> --------- Co-authored-by: 루밀LuMir <rpfos@naver.com>
1 parent ede3935 commit d189c5e

6 files changed

Lines changed: 131 additions & 4 deletions

File tree

docs/rules/no-duplicate-definitions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ The following options are available on this rule:
9090
[^mercury]: Hello, Venus!
9191
```
9292

93+
- `checkFootnoteDefinitions: boolean` - When set to `false`, the rule will not report duplicate footnote definitions. (default: `true`)
94+
95+
Examples of **correct** code when configured as `"no-duplicate-definitions": ["error", { checkFootnoteDefinitions: false }]`:
96+
97+
```markdown
98+
<!-- eslint markdown/no-duplicate-definitions: ["error", { checkFootnoteDefinitions: false }] -->
99+
100+
[^mercury]: Hello, Mercury!
101+
[^mercury]: Hello, Venus!
102+
```
103+
93104
## When Not to Use It
94105

95106
If you are using a different style of definition comments, or not concerned with duplicate definitions, you can safely disable this rule.

docs/rules/no-unused-definitions.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ Mercury[^mercury]
9090
[^mercury]: Hello, Venus!
9191
```
9292

93+
- `checkFootnoteDefinitions: boolean` - When set to `false`, the rule will not report unused footnote definitions. (default: `true`)
94+
95+
Examples of **correct** code when configured as `"no-unused-definitions": ["error", { checkFootnoteDefinitions: false }]`:
96+
97+
```markdown
98+
<!-- eslint markdown/no-unused-definitions: ["error", { checkFootnoteDefinitions: false }] -->
99+
100+
[^mercury]: Hello, Mercury!
101+
```
102+
93103
## When Not to Use It
94104

95105
You might want to disable this rule if:

src/rules/no-duplicate-definitions.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { normalizeIdentifier } from "micromark-util-normalize-identifier";
1717
* @import { Definition, FootnoteDefinition } from "mdast";
1818
* @import { MarkdownRuleDefinition } from "../types.js";
1919
* @typedef {"duplicateDefinition" | "duplicateFootnoteDefinition"} NoDuplicateDefinitionsMessageIds
20-
* @typedef {[{ allowDefinitions?: string[], allowFootnoteDefinitions?: string[] }]} NoDuplicateDefinitionsOptions
20+
* @typedef {[{ allowDefinitions?: string[], allowFootnoteDefinitions?: string[], checkFootnoteDefinitions?: boolean }]} NoDuplicateDefinitionsOptions
2121
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoDuplicateDefinitionsOptions, MessageIds: NoDuplicateDefinitionsMessageIds }>} NoDuplicateDefinitionsRuleDefinition
2222
*/
2323

@@ -61,6 +61,9 @@ export default {
6161
},
6262
uniqueItems: true,
6363
},
64+
checkFootnoteDefinitions: {
65+
type: "boolean",
66+
},
6467
},
6568
additionalProperties: false,
6669
},
@@ -70,6 +73,7 @@ export default {
7073
{
7174
allowDefinitions: ["//"],
7275
allowFootnoteDefinitions: [],
76+
checkFootnoteDefinitions: true,
7377
},
7478
],
7579
},
@@ -85,6 +89,7 @@ export default {
8589
normalizeIdentifier(identifier).toLowerCase(),
8690
),
8791
);
92+
const [{ checkFootnoteDefinitions }] = context.options;
8893

8994
/** @type {Map<string, Definition>} */
9095
const definitions = new Map();
@@ -119,7 +124,10 @@ export default {
119124
},
120125

121126
footnoteDefinition(node) {
122-
if (allowFootnoteDefinitions.has(node.identifier)) {
127+
if (
128+
!checkFootnoteDefinitions ||
129+
allowFootnoteDefinitions.has(node.identifier)
130+
) {
123131
return;
124132
}
125133

src/rules/no-unused-definitions.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { normalizeIdentifier } from "micromark-util-normalize-identifier";
1717
* @import { Definition, FootnoteDefinition } from "mdast";
1818
* @import { MarkdownRuleDefinition } from "../types.js";
1919
* @typedef {"unusedDefinition" | "unusedFootnoteDefinition"} NoUnusedDefinitionsMessageIds
20-
* @typedef {[{ allowDefinitions?: string[], allowFootnoteDefinitions?: string[] }]} NoUnusedDefinitionsOptions
20+
* @typedef {[{ allowDefinitions?: string[], allowFootnoteDefinitions?: string[], checkFootnoteDefinitions?: boolean }]} NoUnusedDefinitionsOptions
2121
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoUnusedDefinitionsOptions, MessageIds: NoUnusedDefinitionsMessageIds }>} NoUnusedDefinitionsRuleDefinition
2222
*/
2323

@@ -61,6 +61,9 @@ export default {
6161
},
6262
uniqueItems: true,
6363
},
64+
checkFootnoteDefinitions: {
65+
type: "boolean",
66+
},
6467
},
6568
additionalProperties: false,
6669
},
@@ -70,6 +73,7 @@ export default {
7073
{
7174
allowDefinitions: ["//"],
7275
allowFootnoteDefinitions: [],
76+
checkFootnoteDefinitions: true,
7377
},
7478
],
7579
},
@@ -85,6 +89,7 @@ export default {
8589
normalizeIdentifier(identifier).toLowerCase(),
8690
),
8791
);
92+
const [{ checkFootnoteDefinitions }] = context.options;
8893

8994
/** @type {Set<string>} Set to track used identifiers */
9095
const usedIdentifiers = new Set();
@@ -117,7 +122,10 @@ export default {
117122
},
118123

119124
footnoteDefinition(node) {
120-
if (allowFootnoteDefinitions.has(node.identifier)) {
125+
if (
126+
!checkFootnoteDefinitions ||
127+
allowFootnoteDefinitions.has(node.identifier)
128+
) {
121129
return;
122130
}
123131

tests/rules/no-duplicate-definitions.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ ruleTester.run("no-duplicate-definitions", rule, {
153153
},
154154
],
155155
},
156+
{
157+
code: `
158+
[^mercury]: Hello, Mercury!
159+
[^mercury]: Hello, Venus!
160+
`,
161+
options: [
162+
{
163+
checkFootnoteDefinitions: false,
164+
},
165+
],
166+
},
167+
{
168+
code: `
169+
[^mercury]: Hello, Mercury!
170+
[^mercury]: Hello, Venus!
171+
`,
172+
options: [
173+
{
174+
checkFootnoteDefinitions: true,
175+
allowFootnoteDefinitions: ["mercury"],
176+
},
177+
],
178+
},
156179
// This test case is skipped when running on Bun
157180
...(!process.versions.bun
158181
? [
@@ -443,6 +466,34 @@ ruleTester.run("no-duplicate-definitions", rule, {
443466
],
444467
},
445468

469+
{
470+
code: `
471+
[^mercury]: Hello, Mercury!
472+
[^mercury]: Hello, Venus!
473+
`,
474+
options: [
475+
{
476+
checkFootnoteDefinitions: true,
477+
},
478+
],
479+
480+
errors: [
481+
{
482+
messageId: "duplicateFootnoteDefinition",
483+
data: {
484+
identifier: "mercury",
485+
label: "mercury",
486+
firstLine: "2",
487+
firstLabel: "mercury",
488+
},
489+
line: 3,
490+
column: 1,
491+
endLine: 3,
492+
endColumn: 26,
493+
},
494+
],
495+
},
496+
446497
{
447498
code: `
448499
[mercury]: https://example.com/mercury/

tests/rules/no-unused-definitions.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ Mercury[^mercury]
199199
},
200200
],
201201
},
202+
{
203+
code: "[^mercury]: Hello, Mercury!",
204+
options: [
205+
{
206+
checkFootnoteDefinitions: false,
207+
},
208+
],
209+
},
210+
{
211+
code: "[^mercury]: Hello, Mercury!",
212+
options: [
213+
{
214+
checkFootnoteDefinitions: true,
215+
allowFootnoteDefinitions: ["mercury"],
216+
},
217+
],
218+
},
202219
// This test case is skipped when running on Bun
203220
...(!process.versions.bun
204221
? [
@@ -417,6 +434,28 @@ Mercury[^mercury]
417434
],
418435
},
419436

437+
{
438+
code: `
439+
[^mercury]: Hello, Mercury!
440+
`,
441+
options: [
442+
{
443+
checkFootnoteDefinitions: true,
444+
},
445+
],
446+
447+
errors: [
448+
{
449+
messageId: "unusedFootnoteDefinition",
450+
data: { identifier: "mercury", label: "mercury" },
451+
line: 2,
452+
column: 1,
453+
endLine: 2,
454+
endColumn: 28,
455+
},
456+
],
457+
},
458+
420459
{
421460
code: `
422461
Hello, [Mercury][mercury]!

0 commit comments

Comments
 (0)