Skip to content

Commit

Permalink
test(linter): test rule docs with oxlint
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 5, 2024
1 parent a4131cc commit d55dab0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ schemars = { workspace = true, features = ["indexmap2"] }
static_assertions = { workspace = true }
insta = { workspace = true }
project-root = { workspace = true }
markdown = { version = "1.0.0-alpha.18" }
38 changes: 37 additions & 1 deletion crates/oxc_linter/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,48 @@ impl RuleWithSeverity {
#[cfg(test)]
mod test {
use crate::rules::RULES;
use itertools::Itertools as _;
use markdown::{to_html_with_options, Options};
use oxc_allocator::Allocator;
use oxc_diagnostics::Error;
use oxc_parser::Parser;
use oxc_span::SourceType;

#[test]
fn ensure_documentation() {
assert!(!RULES.is_empty());
let options = Options::gfm();
let source_type = SourceType::default().with_jsx(true).with_always_strict(true);

for rule in RULES.iter() {
assert!(rule.documentation().is_some_and(|s| !s.is_empty()), "{}", rule.name());
let name = rule.name();
assert!(
rule.documentation().is_some_and(|s| !s.is_empty()),
"Rule '{name}' is missing documentation."
);
// will panic if provided invalid markdown
let html = to_html_with_options(rule.documentation().unwrap(), &options).unwrap();
assert!(!html.is_empty());

// convert HTML to JSX, then use the parser to ensure valid HTML was generated
let jsx =
format!("const Documentation = <>{}</>;", html.replace("class=", "className="));
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &jsx, source_type).parse();

let has_errors = !ret.errors.is_empty();
let errors = ret
.errors
.into_iter()
.map(|error| Error::new(error).with_source_code(jsx.clone()))
.map(|e| format!("{e:?}"))
.join("\n\n");

assert!(
!ret.panicked && !has_errors,
"Documentation for rule '{name}' has invalid syntax: {errors}"
);
assert!(!ret.program.body.is_empty(), "Documentation for rule '{name}' is empty.");
}
}
}
3 changes: 3 additions & 0 deletions tasks/website/src/linter/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ fn write_rule_doc_pages(table: &RuleTable, outdir: &Path) {
let plugin_path = outdir.join(&rule.plugin);
fs::create_dir_all(&plugin_path).unwrap();
let page_path = plugin_path.join(format!("{}.md", rule.name));
if page_path.exists() {
fs::remove_file(&page_path).unwrap();
}
println!("{}", page_path.display());
let docs = render_rule_docs_page(rule).unwrap();
fs::write(&page_path, docs).unwrap();
Expand Down

0 comments on commit d55dab0

Please sign in to comment.