Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(rome_service): treat nursery rules differently
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 19, 2023
1 parent 61abdc3 commit a3d1193
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
53 changes: 53 additions & 0 deletions crates/rome_cli/tests/commands/check.rs
Expand Up @@ -2220,3 +2220,56 @@ array.map((sentence) => sentence.split(" ")).flat();
result,
));
}

#[test]
fn should_not_enable_nursery_rules() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let configuration = r#" {
"$schema": "https://docs.rome.tools/schemas/12.1.0/schema.json",
"organizeImports": {
"enabled": false
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"nursery": {
"noAccumulatingSpread": "error"
}
}
}
}"#;

let configuration_path = Path::new("rome.json");
fs.insert(configuration_path.into(), configuration.as_bytes());

let file_path = Path::new("fix.ts");
fs.insert(
file_path.into(),
r#"const bannedType: Boolean = true;
if (true) {
const obj = {};
obj["useLiteralKey"];
}
"#,
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[("check"), file_path.as_os_str().to_str().unwrap()]),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"should_not_enable_nursery_rules",
fs,
console,
result,
));
}
@@ -0,0 +1,43 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"$schema": "https://docs.rome.tools/schemas/12.1.0/schema.json",
"organizeImports": {
"enabled": false
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"nursery": {
"noAccumulatingSpread": "error"
}
}
}
}
```

## `fix.ts`

```ts
const bannedType: Boolean = true;

if (true) {
const obj = {};
obj["useLiteralKey"];
}

```

# Emitted Messages

```block
Checked 1 file(s) in <TIME>
```


4 changes: 2 additions & 2 deletions crates/rome_service/src/configuration/linter/rules.rs

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

16 changes: 13 additions & 3 deletions xtask/codegen/src/generate_configuration.rs
Expand Up @@ -447,7 +447,17 @@ fn generate_struct(group: &str, rules: &BTreeMap<&'static str, RuleMetadata>) ->
let group_struct_name = Ident::new(&group.to_capitalized(), Span::call_site());

let number_of_recommended_rules = Literal::u8_unsuffixed(number_of_recommended_rules);

let (group_recommended, parent_parameter) = if group == "nursery" {
(
quote! { self.is_recommended() },
quote! { _parent_is_recommended: bool, },
)
} else {
(
quote! { parent_is_recommended || self.is_recommended() },
quote! { parent_is_recommended: bool, },
)
};
quote! {
#[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
Expand Down Expand Up @@ -537,13 +547,13 @@ fn generate_struct(group: &str, rules: &BTreeMap<&'static str, RuleMetadata>) ->
/// Select preset rules
pub(crate) fn collect_preset_rules(
&self,
parent_is_recommended: bool,
#parent_parameter
enabled_rules: &mut IndexSet<RuleFilter>,
disabled_rules: &mut IndexSet<RuleFilter>,
) {
if self.is_all() {
enabled_rules.extend(Self::all_rules_as_filters());
} else if parent_is_recommended || self.is_recommended() {
} else if #group_recommended {
enabled_rules.extend(Self::recommended_rules_as_filters());
}
if self.is_not_all() {
Expand Down

0 comments on commit a3d1193

Please sign in to comment.