Skip to content

Commit 0b18005

Browse files
committed
docs(linter): Add config docs generation for rules with Regex arguments (#15978)
~~Draft until we merge oxc-project/schemars#9 and upgrade the oxc-schemars crate in this repo afterwards. Once we have the schemars crate bumped up to 0.8.27 we can safely rebase this branch and this'll be ready to merge.~~ But I can confirm that this now works with my patch to schemars :D Part of #14743. Generated docs: ```md ## Configuration This rule accepts a configuration object with the following properties: ### ignore type: `string[]` A list of patterns to ignore when checking `catch` variable names. The pattern can be a string or regular expression. ### name type: `string` default: `"error"` The name to use for error variables in `catch` blocks. You can customize it to something other than `'error'` (e.g., `'exception'`). ``` Unfortunately it doesn't recognize the default value in this case, although I'm not 100% sure why. But at the very least, this works and allows us to finish the docs for most of the remaining lint rules. ```md ## Configuration This rule accepts a configuration object with the following properties: ### rejectPattern type: `[ string, null ]` Regex pattern used to validate the `reject` parameter name. If provided, this pattern is used instead of the default `^_?reject$` check. ### resolvePattern type: `[ string, null ]` Regex pattern used to validate the `resolve` parameter name. If provided, this pattern is used instead of the default `^_?resolve$` check. ```
1 parent 80fe39d commit 0b18005

File tree

5 files changed

+19
-43
lines changed

5 files changed

+19
-43
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_linter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ phf = { workspace = true, features = ["macros"] }
6363
rayon = { workspace = true }
6464
rust-lapper = { workspace = true }
6565
rustc-hash = { workspace = true }
66-
schemars = { workspace = true, features = ["indexmap2"] }
66+
schemars = { workspace = true, features = ["indexmap2", "regex"] }
6767
self_cell = { workspace = true }
6868
serde = { workspace = true, features = ["derive"] }
6969
serde_json = { workspace = true, features = ["preserve_order"] } # preserve_order: print config with ordered keys.

crates/oxc_linter/src/rules/promise/param_names.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use oxc_ast::{
66
use oxc_diagnostics::OxcDiagnostic;
77
use oxc_macros::declare_oxc_lint;
88
use oxc_span::Span;
9+
use schemars::JsonSchema;
910

1011
use crate::{AstNode, context::LintContext, rule::Rule};
1112

@@ -19,9 +20,14 @@ fn param_names_diagnostic(span: Span, pattern: &str) -> OxcDiagnostic {
1920
#[derive(Debug, Default, Clone)]
2021
pub struct ParamNames(Box<ParamNamesConfig>);
2122

22-
#[derive(Debug, Default, Clone)]
23+
#[derive(Debug, Default, Clone, JsonSchema)]
24+
#[serde(rename_all = "camelCase", default)]
2325
pub struct ParamNamesConfig {
26+
/// Regex pattern used to validate the `resolve` parameter name. If provided, this pattern
27+
/// is used instead of the default `^_?resolve$` check.
2428
resolve_pattern: Option<Regex>,
29+
/// Regex pattern used to validate the `reject` parameter name. If provided, this pattern
30+
/// is used instead of the default `^_?reject$` check.
2531
reject_pattern: Option<Regex>,
2632
}
2733

@@ -65,6 +71,7 @@ declare_oxc_lint!(
6571
ParamNames,
6672
promise,
6773
style,
74+
config = ParamNamesConfig,
6875
);
6976

7077
impl Rule for ParamNames {

crates/oxc_linter/src/rules/unicorn/catch_error_name.rs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use oxc_diagnostics::OxcDiagnostic;
77
use oxc_macros::declare_oxc_lint;
88
use oxc_span::{CompactStr, Span};
99
use oxc_syntax::identifier::is_identifier_name;
10+
use schemars::JsonSchema;
1011

1112
use crate::{AstNode, context::LintContext, rule::Rule};
1213

@@ -24,9 +25,14 @@ fn catch_error_name_diagnostic(
2425
#[derive(Debug, Default, Clone)]
2526
pub struct CatchErrorName(Box<CatchErrorNameConfig>);
2627

27-
#[derive(Debug, Clone)]
28+
#[derive(Debug, Clone, JsonSchema)]
29+
#[serde(rename_all = "camelCase", default)]
2830
pub struct CatchErrorNameConfig {
31+
/// A list of patterns to ignore when checking `catch` variable names. The pattern
32+
/// can be a string or regular expression.
2933
ignore: Vec<Regex>,
34+
/// The name to use for error variables in `catch` blocks. You can customize it
35+
/// to something other than `'error'` (e.g., `'exception'`).
3036
name: CompactStr,
3137
}
3238

@@ -82,46 +88,11 @@ declare_oxc_lint!(
8288
///
8389
/// promise.then(undefined, error => {});
8490
/// ```
85-
///
86-
/// ### Options
87-
///
88-
/// #### name
89-
///
90-
/// `{ type: string, default: "error" }`
91-
///
92-
/// The name to use for error variables in `catch` blocks. You can customize it
93-
/// to something other than `'error'` (e.g., `'exception'`).
94-
///
95-
/// Example:
96-
/// ```json
97-
/// "unicorn/catch-error-name": [
98-
/// "error",
99-
/// { "name": "exception" }
100-
/// ]
101-
/// ```
102-
///
103-
/// #### ignore
104-
///
105-
/// `{ type: Array<string | RegExp>, default: [] }`
106-
///
107-
/// A list of patterns to ignore when checking `catch` variable names. The pattern
108-
/// can be a string or regular expression.
109-
///
110-
/// Example:
111-
/// ```json
112-
/// "unicorn/catch-error-name": [
113-
/// "error",
114-
/// {
115-
/// "ignore": [
116-
/// "^error\\d*$"
117-
/// ]
118-
/// }
119-
/// ]
120-
/// ```
12191
CatchErrorName,
12292
unicorn,
12393
style,
124-
fix
94+
fix,
95+
config = CatchErrorNameConfig,
12596
);
12697

12798
impl Rule for CatchErrorName {

crates/oxc_linter/tests/rule_configuration_documentation_test.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ fn test_rules_with_custom_configuration_have_schema() {
4444
// jest
4545
"jest/consistent-test-it",
4646
"jest/valid-title",
47-
// promise
48-
"promise/param-names",
4947
// react
5048
"react/forbid-dom-props",
5149
"react/forbid-elements",
@@ -55,7 +53,6 @@ fn test_rules_with_custom_configuration_have_schema() {
5553
"typescript/ban-ts-comment",
5654
"typescript/consistent-type-imports",
5755
// unicorn
58-
"unicorn/catch-error-name",
5956
"unicorn/filename-case",
6057
];
6158

0 commit comments

Comments
 (0)