Skip to content

Commit

Permalink
feat(rulegen): improve examples in documentation (#4815)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac authored Aug 10, 2024
1 parent 15a0fd4 commit a266b45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tasks/rulegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::print_stdout, clippy::print_stderr)]
use std::{
borrow::Cow,
collections::HashMap,
fmt::{self, Display, Formatter},
};
Expand Down Expand Up @@ -319,6 +320,10 @@ pub struct Context {
fail_cases: String,
fix_cases: Option<String>,
has_filename: bool,
/// Language examples are written in.
///
/// Should be `"js"`, `"jsx"`, `"ts"`, `"tsx"`. Defaults to `"js"`.
language: Cow<'static, str>,
}

impl Context {
Expand All @@ -335,6 +340,7 @@ impl Context {
fail_cases,
fix_cases: None,
has_filename: false,
language: Cow::Borrowed("js"),
}
}

Expand All @@ -347,6 +353,11 @@ impl Context {
self.fix_cases = Some(fix_cases);
self
}

fn with_language<S: Into<Cow<'static, str>>>(mut self, language: S) -> Self {
self.language = language.into();
self
}
}

struct State<'a> {
Expand Down Expand Up @@ -648,6 +659,12 @@ fn main() {
RuleKind::Vitest => format!("{VITEST_TEST_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::Oxc => String::new(),
};
let language = match rule_kind {
RuleKind::Typescript | RuleKind::Oxc => "ts",
RuleKind::NextJS => "tsx",
RuleKind::React | RuleKind::ReactPerf | RuleKind::JSXA11y | RuleKind::TreeShaking => "jsx",
_ => "js",
};

println!("Reading test file from {rule_test_path}");

Expand Down Expand Up @@ -719,6 +736,7 @@ fn main() {
let (fail_cases, fix_cases) = gen_cases_string(fail_cases);

Context::new(plugin_name, &rule_name, pass_cases, fail_cases)
.with_language(language)
.with_filename(has_filename)
.with_fix_cases(fix_cases)
}
Expand Down
12 changes: 10 additions & 2 deletions tasks/rulegen/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ declare_oxc_lint!(
/// ### Why is this bad?
///
///
/// ### Example
/// ```javascript
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```{{language}}
/// FIXME: Tests will fail if examples are missing or syntactically incorrect.
/// ```
///
/// Examples of **correct** code for this rule:
/// ```{{language}}
/// FIXME: Tests will fail if examples are missing or syntactically incorrect.
/// ```
{{pascal_rule_name}},
nursery, // TODO: change category to `correctness`, `suspicious`, `pedantic`, `perf`, `restriction`, or `style`
Expand Down

0 comments on commit a266b45

Please sign in to comment.