Skip to content

Commit

Permalink
fix(formatter): don't let overrides erroneously enable formatter if u…
Browse files Browse the repository at this point in the history
…nspecified (#2957)
  • Loading branch information
dyc3 authored May 24, 2024
1 parent c3c5308 commit a49bd8d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
68 changes: 68 additions & 0 deletions crates/biome_cli/tests/cases/overrides_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,74 @@ fn does_include_file_with_different_overrides() {
));
}

/// Issue: https://github.com/biomejs/biome/issues/2924
#[test]
fn complex_enable_disable_overrides() {
let mut console = BufferConsole::default();
let mut fs = MemoryFileSystem::default();
let file_path = Path::new("biome.json");
fs.insert(
file_path.into(),
r#"{
"formatter": {
"lineWidth": 20
},
"javascript": {
"formatter": {
"enabled": false
}
},
"overrides": [
{ "include": ["formatted.js"], "formatter": { "enabled": true } },
{
"include": ["dirty.js"],
"linter": {
"rules": {
"performance": {
"noBarrelFile": "off"
}
}
}
}
]
}
"#
.as_bytes(),
);

let formatted = Path::new("formatted.js");
fs.insert(formatted.into(), UNFORMATTED_LINE_WIDTH.as_bytes());

let unformatted = Path::new("dirty.js");
fs.insert(unformatted.into(), UNFORMATTED_LINE_WIDTH.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
("--write"),
formatted.as_os_str().to_str().unwrap(),
unformatted.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert_file_contents(&fs, formatted, FORMATTED_LINE_WIDTH_OVERRIDDEN);
assert_file_contents(&fs, unformatted, UNFORMATTED_LINE_WIDTH);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"complex_enable_disable_overrides",
fs,
console,
result,
));
}

#[test]
#[ignore = "Enable when we are ready to handle CSS files"]
fn does_include_file_with_different_languages() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"formatter": {
"lineWidth": 20
},
"javascript": {
"formatter": {
"enabled": false
}
},
"overrides": [
{ "include": ["formatted.js"], "formatter": { "enabled": true } },
{
"include": ["dirty.js"],
"linter": {
"rules": {
"performance": {
"noBarrelFile": "off"
}
}
}
}
]
}
```

## `dirty.js`

```js
const a = ["loreum", "ipsum"]
```

## `formatted.js`

```js
const a = [
"loreum",
"ipsum",
];

```

# Emitted Messages

```block
Formatted 1 file in <TIME>. Fixed 1 file.
```
8 changes: 7 additions & 1 deletion crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,13 @@ pub(crate) fn to_override_format_settings(
.unwrap_or(format_settings.format_with_errors);

OverrideFormatSettings {
enabled: conf.enabled.or(Some(format_settings.enabled)),
enabled: conf.enabled.or(
if format_settings.enabled != FormatSettings::default().enabled {
Some(format_settings.enabled)
} else {
None
},
),
indent_style,
indent_width,
line_ending,
Expand Down

0 comments on commit a49bd8d

Please sign in to comment.