Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply feedback
Browse files Browse the repository at this point in the history
ematipico committed Oct 1, 2024
1 parent 640a728 commit 01de10b
Showing 3 changed files with 28 additions and 30 deletions.
20 changes: 13 additions & 7 deletions crates/biome_analyze/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -161,16 +161,16 @@ We would like to set the options in the `biome.json` configuration file:
}
```

The first step is to create the Rust data representation of the rule's options. Each option must be wrapped in a `Option`, this is required so the configuration schema won't mark them as required.
The first step is to create the Rust data representation of the rule's options.

```rust
use biome_deserialize_macros::Deserializable;

#[derive(Clone, Debug, Default, Deserializable)]
pub struct MyRuleOptions {
behavior: Option<Behavior>,
threshold: Option<u8>,
behavior_exceptions: Option<Vec<String>>
behavior: Behavior,
threshold: u8,
behavior_exceptions: Vec<String>
}

#[derive(Clone, Debug, Default, Deserializable)]
@@ -207,18 +207,24 @@ let options = ctx.options();

The compiler should warn you that `MyRuleOptions` does not implement some required types.
We currently require implementing _serde_'s traits `Deserialize`/`Serialize`.

Also, we use other `serde` macros to adjust the JSON configuration:
- `rename_all = "camelCase"`: it renames all fields in camel-case, so they are in line with the naming style of the `biome.json`.
- `deny_unknown_fields`: it raises an error if the configuration contains extraneous fields.
- `default`: it uses the `Default` value when the field is missing from `biome.json`. This macro makes the field optional.

You can simply use a derive macros:

```rust
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[serde(rename_all = "camelCase", deny_unknown_fields, default)]
pub struct MyRuleOptions {
#[serde(default, skip_serializing_if = "is_default")]
main_behavior: Option<Behavior>,
main_behavior: Behavior,

#[serde(default, skip_serializing_if = "is_default")]
extra_behaviors: Option<Vec<Behavior>>,
extra_behaviors: Vec<Behavior>,
}

#[derive(Debug, Default, Clone)]
26 changes: 9 additions & 17 deletions crates/biome_js_analyze/src/lint/a11y/no_label_without_control.rs
Original file line number Diff line number Diff line change
@@ -155,11 +155,11 @@ impl Rule for NoLabelWithoutControl {
#[serde(rename_all = "camelCase", deny_unknown_fields, default)]
pub struct NoLabelWithoutControlOptions {
/// Array of component names that should be considered the same as an `input` element.
pub input_components: Option<Vec<String>>,
pub input_components: Vec<String>,
/// Array of attributes that should be treated as the `label` accessible text content.
pub label_attributes: Option<Vec<String>>,
pub label_attributes: Vec<String>,
/// Array of component names that should be considered the same as a `label` element.
pub label_components: Option<Vec<String>>,
pub label_components: Vec<String>,
}

impl NoLabelWithoutControlOptions {
@@ -174,10 +174,8 @@ impl NoLabelWithoutControlOptions {
if !DEFAULT_LABEL_ATTRIBUTES.contains(&attribute_name)
&& !self
.label_attributes
.as_ref()
.is_some_and(|label_attributes| {
label_attributes.iter().any(|name| name == attribute_name)
})
.iter()
.any(|name| name == attribute_name)
{
return false;
}
@@ -246,10 +244,8 @@ impl NoLabelWithoutControlOptions {
if DEFAULT_INPUT_COMPONENTS.contains(&element_name)
|| self
.input_components
.as_ref()
.is_some_and(|input_components| {
input_components.iter().any(|name| name == element_name)
})
.iter()
.any(|name| name == element_name)
{
return true;
}
@@ -263,12 +259,8 @@ impl NoLabelWithoutControlOptions {

fn has_element_name(&self, element_name: &str) -> bool {
self.label_components
.as_ref()
.is_some_and(|label_components| {
label_components
.iter()
.any(|label_component_name| label_component_name == element_name)
})
.iter()
.any(|label_component_name| label_component_name == element_name)
}
}

12 changes: 6 additions & 6 deletions packages/@biomejs/biome/configuration_schema.json

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

0 comments on commit 01de10b

Please sign in to comment.