|
| 1 | +use markdown::{to_html, to_html_with_options, Options}; |
| 2 | +use oxc_diagnostics::NamedSource; |
| 3 | +use scraper::{ElementRef, Html, Selector}; |
| 4 | +use std::sync::{Arc, OnceLock}; |
| 5 | + |
| 6 | +use oxc_allocator::Allocator; |
| 7 | +use oxc_linter::table::RuleTable; |
| 8 | +use oxc_parser::Parser; |
| 9 | +use oxc_span::SourceType; |
| 10 | + |
| 11 | +use super::{render_rule_docs_page, render_rules_table}; |
| 12 | + |
| 13 | +static TABLE: OnceLock<RuleTable> = OnceLock::new(); |
| 14 | + |
| 15 | +fn table() -> &'static RuleTable { |
| 16 | + TABLE.get_or_init(|| RuleTable::new()) |
| 17 | +} |
| 18 | + |
| 19 | +fn parse(filename: &str, jsx: &str) -> Result<(), String> { |
| 20 | + let filename = format!("{filename}.tsx"); |
| 21 | + let source_type = SourceType::from_path(&filename).unwrap(); |
| 22 | + parse_type(&filename, jsx, source_type) |
| 23 | +} |
| 24 | + |
| 25 | +fn parse_type(filename: &str, source_text: &str, source_type: SourceType) -> Result<(), String> { |
| 26 | + let alloc = Allocator::default(); |
| 27 | + let ret = Parser::new(&alloc, source_text, source_type).parse(); |
| 28 | + |
| 29 | + if ret.errors.is_empty() { |
| 30 | + Ok(()) |
| 31 | + } else { |
| 32 | + let num_errs = ret.errors.len(); |
| 33 | + let source = Arc::new(NamedSource::new(filename, source_text.to_string())); |
| 34 | + ret.errors |
| 35 | + .into_iter() |
| 36 | + .map(|e| e.with_source_code(Arc::clone(&source))) |
| 37 | + .for_each(|e| println!("{e:?}")); |
| 38 | + Err(format!("{} errors occurred while parsing {filename}.jsx", num_errs)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn test_rules_table() { |
| 44 | + const PREFIX: &str = "/docs/guide/usage/linter/rules"; |
| 45 | + let rendered_table = render_rules_table(table(), PREFIX); |
| 46 | + let html = to_html(&rendered_table); |
| 47 | + let jsx = format!("const Table = () => <>{html}</>"); |
| 48 | + parse("rules-table", &jsx).unwrap(); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn test_doc_pages() { |
| 53 | + let mut options = Options::gfm(); |
| 54 | + options.compile.allow_dangerous_html = true; |
| 55 | + |
| 56 | + for section in &table().sections { |
| 57 | + let category = section.category; |
| 58 | + let code = Selector::parse("code").unwrap(); |
| 59 | + |
| 60 | + for row in §ion.rows { |
| 61 | + let filename = format!("{category}/{}/{}", row.plugin, row.name); |
| 62 | + let docs = render_rule_docs_page(row).unwrap(); |
| 63 | + let docs = to_html_with_options(&docs, &options).unwrap(); |
| 64 | + let docs = if let Some(end_of_autogen_comment) = docs.find("-->") { |
| 65 | + &docs[end_of_autogen_comment + 4..] |
| 66 | + } else { |
| 67 | + &docs |
| 68 | + }; |
| 69 | + |
| 70 | + // ensure the docs are valid JSX |
| 71 | + { |
| 72 | + let jsx = format!("const Docs = () => <>{docs}</>"); |
| 73 | + parse(&filename, &jsx).unwrap(); |
| 74 | + } |
| 75 | + |
| 76 | + // ensure code examples are valid |
| 77 | + { |
| 78 | + let html = Html::parse_fragment(&docs); |
| 79 | + assert!(html.errors.is_empty(), "HTML parsing errors: {:#?}", html.errors); |
| 80 | + for code_el in html.select(&code) { |
| 81 | + let inner = code_el.inner_html(); |
| 82 | + assert!( |
| 83 | + !inner.trim().is_empty(), |
| 84 | + "Rule '{}' has an empty code snippet", |
| 85 | + row.name |
| 86 | + ); |
| 87 | + let inner = inner.replace("<", "<").replace(">", ">"); |
| 88 | + let filename = filename.clone() + "/code-snippet"; |
| 89 | + let source_type = source_type_from_code_element(code_el); |
| 90 | + parse_type(&filename, &inner, source_type).unwrap(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn default_source() -> SourceType { |
| 98 | + SourceType::default().with_typescript(true).with_jsx(true).with_always_strict(true) |
| 99 | +} |
| 100 | +fn source_type_from_code_element(code: ElementRef) -> SourceType { |
| 101 | + let Some(class) = code.attr("class") else { |
| 102 | + return default_source(); |
| 103 | + }; |
| 104 | + let maybe_class = class.split('-').collect::<Vec<_>>(); |
| 105 | + let ["language", lang] = maybe_class.as_slice() else { |
| 106 | + return default_source(); |
| 107 | + }; |
| 108 | + |
| 109 | + match *lang { |
| 110 | + "javascript" | "js" => SourceType::default().with_always_strict(true), |
| 111 | + "jsx" => SourceType::default().with_jsx(true).with_always_strict(true), |
| 112 | + "typescript" | "ts" => SourceType::default().with_typescript(true).with_always_strict(true), |
| 113 | + "tsx" => { |
| 114 | + SourceType::default().with_typescript(true).with_jsx(true).with_always_strict(true) |
| 115 | + } |
| 116 | + _ => default_source(), |
| 117 | + } |
| 118 | +} |
0 commit comments