Conversation
| { | ||
| code: dedent` | ||
| [Invalid Format](#l20) | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: "invalidFragment", | ||
| data: { fragment: "l20" }, | ||
| line: 1, | ||
| column: 1, | ||
| endLine: 1, | ||
| endColumn: 23, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| [Invalid Format](#l20-l30) | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: "invalidFragment", | ||
| data: { fragment: "l20-l30" }, | ||
| line: 1, | ||
| column: 1, | ||
| endLine: 1, | ||
| endColumn: 27, | ||
| }, | ||
| ], | ||
| }, |
There was a problem hiding this comment.
These test cases were added primarily to ensure that githubLineReferencePattern is case-insensitive.
Before adding this test, the suite passed even when I added the i flag to the following regex pattern:
FYI: You can verify that the githubLineReferencePattern is case-insensitive using the following examples:
- OK
https://github.com/eslint/markdown/blob/main/package.json#L4
https://github.com/eslint/markdown/blob/main/package.json#L4-L6
- Not OK
https://github.com/eslint/markdown/blob/main/package.json#l4 // Unworking.
https://github.com/eslint/markdown/blob/main/package.json#l4-l6 // Unworking.
| "definition, link"(/** @type {Definition | Link} */ node) { | ||
| const { url } = node; | ||
|
|
||
| const fragment = url.slice(1); | ||
| if (!fragment) { | ||
| // If `url` is empty, `"#"`, or does not start with `"#"`, skip it. | ||
| if (url === "" || url === "#" || !url.startsWith("#")) { | ||
| return; | ||
| } | ||
|
|
||
| linkNodes.push({ node, fragment }); | ||
| relevantNodes.push(node); | ||
| }, |
There was a problem hiding this comment.
This is a small improvement: I've simplified the logic to reduce a bit of memory usage and computation.
Previously, we created a new data structure type as follows:
markdown/src/rules/no-missing-link-fragments.js
Lines 88 to 89 in 8f92161
However, now we no longer create a new object and instead use an existing object reference; it's small, but it would reduce memory usage and computation.
There was a problem hiding this comment.
Pull request overview
This PR fixes a false negative in the no-missing-link-fragments rule where reference-style link definitions (e.g., [reference]: #fragment) were not being validated against existing headings. The rule now correctly reports when Definition nodes reference non-existent fragments.
Key changes:
- Added support for checking
Definitionnodes in addition toLinknodes - Refactored redundant code, including removing unnecessary
toLowerCase()calls (github-slugger already lowercases) - Replaced
htmlCommentPatternregex withstripHtmlComments()utility function for better maintainability
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/rules/no-missing-link-fragments.js |
Updated to recognize Definition nodes using a combined "definition, link" handler; refactored to remove redundant toLowerCase operations since github-slugger already returns lowercase slugs; switched to using stripHtmlComments() utility function |
tests/rules/no-missing-link-fragments.test.js |
Added test cases for valid and invalid Definition nodes; added additional test cases for invalid line reference formats (#l20 and #l20-l30) to verify case-sensitive validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…cognize-definition-node-in-no-missing-link-fragments
|
Changes LGTM, thanks. |
Prerequisites checklist
What is the purpose of this pull request?
Which language are you using?
CommonMark and GFM.
What did you do?
I expected the
[reference]: #non-existentdefinition to be reported by theno-missing-link-fragmentsrule, but it isn't.The
Invalidlink reference is also considered valid link when using reference-style links.What did you expect to happen?
I expected the
[reference]: #non-existentdefinition to be reported by theno-missing-link-fragmentsrule.Link to minimal reproducible Example
The following example would help identify the problem:
What changes did you make? (Give an overview)
In this PR, I've fixed a false negative caused by an unrecognized
Definitionnode inno-missing-link-fragments.This issue was discovered while working on #583, and to avoid making that PR too large, I split this into a separate PR.
Previously, the rule did not handle a
Definitionnode like the following:markdown/src/rules/no-missing-link-fragments.js
Lines 129 to 141 in 8f92161
I've updated the logic to recognize
Definitionnodes accordingly.I've also refactored the codebase. There was some unnecessary or duplicate logic in the rule, so I refactored it to make the rule logic clearer, and faster. I've left comments on the notable parts of this change.
Related Issues
Ref: #583
Is there anything you'd like reviewers to focus on?
N/A