eslint / eslint/markdown

Change Request: Support MDX linting

Open
#316 12 comments 14 reactions 1 assignee Claimed by @nzakas 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: n/a
npm version: n/a
Operating System: n/a

### What problem do you want to solve?

Currently, the Markdown plugin does not formally parse MDX (it just becomes text but doesn't throw an error). Ideally, we could provide smart parsing for MDX so it can be linted just like anything else.

I investigated using https://github.com/syntax-tree/mdast-util-mdx, but it outputs an AST that is difficult to work with. Any JavaScript code produces a `Program` node (it uses Acorn under the hood), and there can be multiple `Program` nodes in a single document. This makes the AST difficult to navigate and make sense of.

Further, because actual JavaScript code can be embedded inside of MDX, we'd need to recreate all of the JavaScript-specific rules just to work in MDX. That seems like wasted effort. I don't think the out-of-the-box approach to MDX from mdast works for our case.

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

I think ultimately what we want is a way to run existing JavaScript rules on an MDX file in addition to MDX-specific rules. The only way I can think to do that using something like a [prelint plugin](https://github.com/eslint/rfcs/pull/105) to extract the JavaScript into a separate virtual file to be linted.

In order to do that, I think we'll need to create a custom mdast plugin that extracts the JavaScript code without creating an AST, basically treating it like a code block.

### Participation

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

### Additional comments

I'm just documenting this for now as I have other, higher-priority work to complete. I'd like to consider this on the roadmap for this plugin.

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.