-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): auto-generate rule docs pages (#4640)
> AI-generated description because I'm lazy ### TL;DR This PR introduces the ability to generate documentation for linter rules and adds new methods and metadata for rule fix capabilities. To see what this looks like, please check out oxc-project/oxc-project.github.io#165. ## Screenshots Hyperlinks to rule doc pages in auto-generated rules table <img width="809" alt="image" src="https://github.com/user-attachments/assets/e09eb47d-e86a-4ed1-b1f9-5034f33c71a2"> Example of a docs page <img width="1273" alt="image" src="https://github.com/user-attachments/assets/78f7e9e6-f4dd-4cc9-aebc-1cdd64b024ec"> ### What changed? - Added `RuleFixMeta` to indicate rule fix capabilities - Introduced methods `is_none` and `is_pending` in `RuleFixMeta` - Modified `render_markdown_table` in `RuleTableSection` to accept an optional link prefix - Created new modules for rule documentation and HTML rendering - Updated `print_rules` function to generate markdown for rules and detailed documentation pages ### How to test? Run the `linter-rules` task with appropriate arguments to generate the markdown table and documentation pages. Verify the generated files for correctness and that all metadata is correctly displayed. ### Why make this change? To enhance the project documentation and provide clear rule fix capabilities, thereby improving the developer experience and easing the integration process. ---
- Loading branch information
Showing
16 changed files
with
377 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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,57 @@ | ||
//! Create documentation pages for each rule. Pages are printed as Markdown and | ||
//! get added to the website. | ||
use oxc_linter::{table::RuleTableRow, RuleFixMeta}; | ||
use std::fmt::{self, Write}; | ||
|
||
use crate::linter::rules::html::HtmlWriter; | ||
|
||
pub fn render_rule_docs_page(rule: &RuleTableRow) -> Result<String, fmt::Error> { | ||
const APPROX_FIX_CATEGORY_AND_PLUGIN_LEN: usize = 512; | ||
let RuleTableRow { name, documentation, plugin, turned_on_by_default, autofix, .. } = rule; | ||
|
||
let mut page = HtmlWriter::with_capacity( | ||
documentation.map_or(0, str::len) + name.len() + APPROX_FIX_CATEGORY_AND_PLUGIN_LEN, | ||
); | ||
|
||
writeln!( | ||
page, | ||
"<!-- This file is auto-generated by {}. Do not edit it manually. -->\n", | ||
file!() | ||
)?; | ||
writeln!(page, "# {plugin}/{name}\n")?; | ||
|
||
// rule metadata | ||
page.div(r#"class="rule-meta""#, |p| { | ||
if *turned_on_by_default { | ||
p.span(r#"class="default-on""#, |p| { | ||
p.writeln("✅ This rule is turned on by default.") | ||
})?; | ||
} | ||
|
||
if let Some(emoji) = fix_emoji(*autofix) { | ||
p.span(r#"class="fix""#, |p| { | ||
p.writeln(format!("{} {}", emoji, autofix.description())) | ||
})?; | ||
} | ||
|
||
Ok(()) | ||
})?; | ||
|
||
// rule documentation | ||
if let Some(docs) = documentation { | ||
writeln!(page, "\n{}", *docs)?; | ||
} | ||
|
||
// TODO: link to rule source | ||
|
||
Ok(page.into()) | ||
} | ||
|
||
fn fix_emoji(fix: RuleFixMeta) -> Option<&'static str> { | ||
match fix { | ||
RuleFixMeta::None => None, | ||
RuleFixMeta::FixPending => Some("🚧"), | ||
RuleFixMeta::Conditional(_) | RuleFixMeta::Fixable(_) => Some("🛠️"), | ||
} | ||
} |
Oops, something went wrong.