eslint / eslint/markdown

Change Request: Support `MarkdownSourceCode#comments` as other language plugins do

Open
#732 2 comments 1 reaction 1 assignee Claimed by @lumirlumir View on GitHub
enhancement
Dominant language
JavaScript
Stars
581
Forks
92
Avg merge
1d 4h
Merged PRs (30d)
11

Description

### Environment

ESLint version: HEAD
@eslint/markdown version: HEAD
Node version: 24.18.0
npm version: 11.16.0
Operating System: Windows

### What problem do you want to solve?

Coming from the community plugin thread https://github.com/eslint-community/eslint-plugin-eslint-comments/pull/267#discussion_r2675845791, which discusses adding `MarkdownSourceCode#comments` to the Markdown plugin.

Currently, JavaScript, JSON, and CSS provide a direct way to access comment information through their respective `XXXSourceCode` classes.

For example, JavaScript has `SourceCode#getAllComments`, JSON has `JSONSourceCode#comments`, and CSS has `CSSSourceCode#comments` to provide direct access to comment information.

- https://eslint.org/docs/latest/extend/custom-rules#accessing-the-source-code
- https://github.com/eslint/css/blob/main/src/languages/css-source-code.js#L95-L99
- https://github.com/eslint/json/blob/main/src/languages/json-source-code.js#L126-L129

However, Markdown does not currently provide a way to directly access comment information through `MarkdownSourceCode`.

This makes it difficult for language-agnostic rule authors - who create rules that apply to all languages or a subset of them - to write rules related to comments, since there is no consistent way to retrieve comment information.

Therefore, I’d like to suggest adding `MarkdownSourceCode#comments`, as other language plugins do.

### What do you think is the correct solution?

We already have logic to extract HTML comments, but it currently only handles `eslint-xxx` inline configuration comments:

https://github.com/eslint/markdown/blob/186ad3ee58982d006e63d3bbe046d69d8ee36e33/src/language/markdown-source-code.js#L72

We could extend this logic to extract all HTML comments, including non-`eslint-xxx` inline configuration comments, and store that information.

### Participation

- [x] I am willing to submit a pull request for this change.

### AI acknowledgment

- [x] I did not use AI to generate this issue report.
- [ ] (If the above is not checked) I have reviewed the AI-generated content before submitting.

### Additional comments

For performance, I think we could also consider implementing it as a lazy getter, similar to `XXXSourceCode#lines` in `rewrite`:

https://github.com/eslint/rewrite/blob/main/packages/plugin-kit/src/source-code.js#L632-L636

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.