-
Notifications
You must be signed in to change notification settings - Fork 80
feat: create no-duplicate-definitions
#360
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
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3182d34
feat: create `no-duplicate-definitions`
lumirlumir f1402d3
wip: complete rule definition
lumirlumir 8b4c35d
wip: update `README.md`
lumirlumir c365bf2
wip: add more testcases
lumirlumir 686da3a
wip: rename `ignores` to `ignore`
lumirlumir e94bea4
wip: add support for `FootnoteDefinition`
lumirlumir 2d26d70
Merge branch 'main' of https://github.com/eslint/markdown into patch-1
lumirlumir 09e1e57
wip: add more testcases
lumirlumir 9c6bab3
wip: update docs
lumirlumir b96ecfb
wip: docs
lumirlumir 6e37b06
wip: refactor code
lumirlumir 983c325
wip: address reviews
lumirlumir 0592ed5
wip: address reviews by snitin315
lumirlumir c844324
wip: address reviews by fasttime
lumirlumir 8f329c0
wip: fix typos
lumirlumir 56c503a
wip: refactor logics
lumirlumir a7d89a3
wip: use Set
lumirlumir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # no-duplicate-definitions | ||
|
|
||
| Disallow duplicate definitions. | ||
|
|
||
| ## Background | ||
|
|
||
| In Markdown, it's possible to define the same definition identifier multiple times. However, this is usually a mistake, as it can lead to unintended or incorrect link, image, and footnote references. | ||
|
|
||
| Please note that this rule does not report definition-style comments. For example: | ||
|
|
||
| ```markdown | ||
| [//]: # (This is a comment 1) | ||
| [//]: <> (This is a comment 2) | ||
| ``` | ||
|
|
||
| ## Rule Details | ||
|
|
||
| > [!IMPORTANT] <!-- eslint-disable-line -- This should be fixed in https://github.com/eslint/markdown/issues/294 --> | ||
| > | ||
| > The `FootnoteDefinition` node is detected only when using `language` mode [`markdown/gfm`](/README.md#languages). | ||
|
|
||
| This rule warns when `Definition` and `FootnoteDefinition` type identifiers are defined multiple times. Please note that this rule is **case-insensitive**, meaning `earth` and `Earth` are treated as the same identifier. | ||
|
|
||
| Examples of **incorrect** code: | ||
|
|
||
| ```markdown | ||
| <!-- eslint markdown/no-duplicate-definitions: "error" --> | ||
| <!-- definition --> | ||
|
|
||
| [mercury]: https://example.com/mercury/ | ||
| [mercury]: https://example.com/venus/ | ||
|
|
||
| [earth]: https://example.com/earth/ | ||
| [Earth]: https://example.com/mars/ | ||
|
|
||
| <!-- footnote definition --> | ||
|
|
||
| [^mercury]: Hello, Mercury! | ||
| [^mercury]: Hello, Venus! | ||
|
|
||
| [^earth]: Hello, Earth! | ||
| [^Earth]: Hello, Mars! | ||
| ``` | ||
|
|
||
| Examples of **correct** code: | ||
|
|
||
| ```markdown | ||
| <!-- eslint markdown/no-duplicate-definitions: "error" --> | ||
| <!-- definition --> | ||
|
|
||
| [mercury]: https://example.com/mercury/ | ||
| [venus]: https://example.com/venus/ | ||
|
|
||
| <!-- footnote definition --> | ||
|
|
||
| [^mercury]: Hello, Mercury! | ||
| [^venus]: Hello, Venus! | ||
|
|
||
| <!-- definition-style comment --> | ||
|
|
||
| [//]: # (This is a comment 1) | ||
| [//]: <> (This is a comment 2) | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| The following options are available on this rule: | ||
|
|
||
| - `allowDefinitions: Array<string>` - when specified, duplicate definitions are allowed if they match one of the identifiers in this array. This is useful for ignoring definitions that are intentionally duplicated. (default: `["//"]`) | ||
|
|
||
| Examples of **correct** code when configured as `"no-duplicate-definitions: ["error", { allowDefinitions: ["mercury"] }]`: | ||
|
|
||
| ```markdown | ||
| <!-- eslint markdown/no-duplicate-definitions: ["error", { allowDefinitions: ["mercury"] }] --> | ||
| [mercury]: https://example.com/mercury/ | ||
| [mercury]: https://example.com/venus/ | ||
| ``` | ||
|
|
||
| - `allowFootnoteDefinitions: Array<string>` - when specified, duplicate footnote definitions are allowed if they match one of the identifiers in this array. This is useful for ignoring footnote definitions that are intentionally duplicated. (default: `[]`) | ||
|
|
||
| Examples of **correct** code when configured as `"no-duplicate-definitions: ["error", { allowFootnoteDefinitions: ["mercury"] }]`: | ||
|
|
||
| ```markdown | ||
| <!-- eslint markdown/no-duplicate-definitions: ["error", { allowFootnoteDefinitions: ["mercury"] }] --> | ||
| [^mercury]: Hello, Mercury! | ||
| [^mercury]: Hello, Venus! | ||
| ``` | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you are using a different style of definition comments, or not concerned with duplicate definitions, you can safely disable this rule. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * @fileoverview Rule to prevent duplicate definitions in Markdown. | ||
| * @author 루밀LuMir(lumirlumir) | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Type Definitions | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: [{ allowDefinitions: string[], allowFootnoteDefinitions: string[]; }]; }>} | ||
| * NoDuplicateDefinitionsRuleDefinition | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** @type {NoDuplicateDefinitionsRuleDefinition} */ | ||
| export default { | ||
| meta: { | ||
| type: "problem", | ||
|
|
||
| docs: { | ||
| recommended: true, | ||
| description: "Disallow duplicate definitions", | ||
| url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-duplicate-definitions.md", | ||
| }, | ||
|
|
||
| messages: { | ||
| duplicateDefinition: "Unexpected duplicate definition found.", | ||
| duplicateFootnoteDefinition: | ||
| "Unexpected duplicate footnote definition found.", | ||
| }, | ||
|
|
||
| schema: [ | ||
| { | ||
| type: "object", | ||
| properties: { | ||
| allowDefinitions: { | ||
| type: "array", | ||
| items: { | ||
| type: "string", | ||
| }, | ||
| uniqueItems: true, | ||
| }, | ||
| allowFootnoteDefinitions: { | ||
| type: "array", | ||
| items: { | ||
| type: "string", | ||
| }, | ||
| uniqueItems: true, | ||
| }, | ||
| }, | ||
| additionalProperties: false, | ||
| }, | ||
| ], | ||
|
|
||
| defaultOptions: [ | ||
| { | ||
| allowDefinitions: ["//"], | ||
| allowFootnoteDefinitions: [], | ||
| }, | ||
| ], | ||
| }, | ||
|
|
||
| create(context) { | ||
| const [{ allowDefinitions, allowFootnoteDefinitions }] = | ||
| context.options; | ||
|
|
||
| const definitions = new Set(); | ||
| const footnoteDefinitions = new Set(); | ||
|
|
||
| return { | ||
| definition(node) { | ||
| if (allowDefinitions.includes(node.identifier)) { | ||
lumirlumir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| if (definitions.has(node.identifier)) { | ||
| context.report({ | ||
| node, | ||
| messageId: "duplicateDefinition", | ||
| }); | ||
| } else { | ||
| definitions.add(node.identifier); | ||
| } | ||
| }, | ||
|
|
||
| footnoteDefinition(node) { | ||
| if (allowFootnoteDefinitions.includes(node.identifier)) { | ||
| return; | ||
| } | ||
|
|
||
| if (footnoteDefinitions.has(node.identifier)) { | ||
| context.report({ | ||
| node, | ||
| messageId: "duplicateFootnoteDefinition", | ||
| }); | ||
| } else { | ||
| footnoteDefinitions.add(node.identifier); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.