Skip to content

Commit 7c83e79

Browse files
authored
fix(config): default value for indent_width (#3096)
1 parent e6a5b04 commit 7c83e79

File tree

32 files changed

+291
-148
lines changed

32 files changed

+291
-148
lines changed

CHANGELOG.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
1111

1212
## Unreleased
1313

14+
### Analyzer
15+
1416
### CLI
1517

1618
#### Bug fixes
1719

1820
- Fix [#3069](https://github.com/biomejs/biome/issues/3069), prevent overwriting paths when using `--staged` or `--changed` options. Contributed by @unvalley
1921
- Fix the bug where whitespace after the & character in CSS nesting was incorrectly trimmed, ensuring proper targeting of child classes [#3061](https://github.com/biomejs/biome/issues/3061). Contributed by @denbezrukov
2022

23+
24+
### Configuration
25+
26+
#### Bug fixes
27+
28+
- Fix [#3067](https://github.com/biomejs/biome/issues/3067), by assigning the correct default value to `indentWidth`. Contributed by @ematipico
29+
30+
### Editors
31+
32+
### Formatter
33+
34+
### JavaScript APIs
35+
2136
### Linter
2237

2338
#### Bug fixes
2439

2540
- The `no-empty-block` css lint rule now treats empty blocks containing comments as valid ones. Contributed by @Sec-ant
2641

42+
43+
### Parser
44+
2745
## 1.8.0 (2024-06-04)
2846

2947
### Analyzer
@@ -306,8 +324,6 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
306324
- Fix [#2782](https://github.com/biomejs/biome/issues/2782) by computing the enabled rules by taking the override settings into consideration. Contributed by @ematipico
307325
- Fix [https://github.com/biomejs/biome/issues/2877] by correctly handling line terminators in JSX string. Contributed by @ah-yu
308326

309-
### JavaScript APIs
310-
311327
### Linter
312328

313329
#### Promoted rules

crates/biome_cli/src/commands/rage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl Display for RageConfiguration<'_, '_> {
215215
{KeyValuePair("Indent style", markup!({DebugDisplay(formatter_configuration.indent_style)}))}
216216
{KeyValuePair("Indent width", markup!({DebugDisplay(formatter_configuration.indent_width)}))}
217217
{KeyValuePair("Line ending", markup!({DebugDisplay(formatter_configuration.line_ending)}))}
218-
{KeyValuePair("Line width", markup!({DebugDisplay(formatter_configuration.line_width.get())}))}
218+
{KeyValuePair("Line width", markup!({DebugDisplay(formatter_configuration.line_width.value())}))}
219219
{KeyValuePair("Attribute position", markup!({DebugDisplay(formatter_configuration.attribute_position)}))}
220220
{KeyValuePair("Ignore", markup!({DebugDisplay(formatter_configuration.ignore.iter().collect::<Vec<_>>())}))}
221221
{KeyValuePair("Include", markup!({DebugDisplay(formatter_configuration.include.iter().collect::<Vec<_>>())}))}
@@ -237,7 +237,7 @@ impl Display for RageConfiguration<'_, '_> {
237237
{KeyValuePair("Indent style", markup!({DebugDisplayOption(javascript_formatter_configuration.indent_style)}))}
238238
{KeyValuePair("Indent width", markup!({DebugDisplayOption(javascript_formatter_configuration.indent_width)}))}
239239
{KeyValuePair("Line ending", markup!({DebugDisplayOption(javascript_formatter_configuration.line_ending)}))}
240-
{KeyValuePair("Line width", markup!({DebugDisplayOption(javascript_formatter_configuration.line_width.map(|lw| lw.get()))}))}
240+
{KeyValuePair("Line width", markup!({DebugDisplayOption(javascript_formatter_configuration.line_width.map(|lw| lw.value()))}))}
241241
{KeyValuePair("Attribute position", markup!({DebugDisplay(javascript_formatter_configuration.attribute_position)}))}
242242
)
243243
.fmt(fmt)?;
@@ -250,7 +250,7 @@ impl Display for RageConfiguration<'_, '_> {
250250
{KeyValuePair("Indent style", markup!({DebugDisplayOption(json_formatter_configuration.indent_style)}))}
251251
{KeyValuePair("Indent width", markup!({DebugDisplayOption(json_formatter_configuration.indent_width)}))}
252252
{KeyValuePair("Line ending", markup!({DebugDisplayOption(json_formatter_configuration.line_ending)}))}
253-
{KeyValuePair("Line width", markup!({DebugDisplayOption(json_formatter_configuration.line_width.map(|lw| lw.get()))}))}
253+
{KeyValuePair("Line width", markup!({DebugDisplayOption(json_formatter_configuration.line_width.map(|lw| lw.value()))}))}
254254
{KeyValuePair("Trailing Commas", markup!({DebugDisplayOption(json_formatter_configuration.trailing_commas)}))}
255255
).fmt(fmt)?;
256256

crates/biome_cli/src/execute/migrate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use biome_deserialize::json::deserialize_from_json_ast;
88
use biome_deserialize::Merge;
99
use biome_diagnostics::Diagnostic;
1010
use biome_diagnostics::{category, PrintDiagnostic};
11-
use biome_formatter::LineWidthFromIntError;
11+
use biome_formatter::ParseFormatNumberError;
1212
use biome_fs::{BiomePath, ConfigName, FileSystemExt, OpenOptions};
1313
use biome_json_parser::{parse_json_with_cache, JsonParserOptions};
1414
use biome_json_syntax::{JsonFileSource, JsonRoot};
@@ -93,7 +93,7 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
9393
let prettier_biome_config =
9494
prettier_config
9595
.try_into()
96-
.map_err(|err: LineWidthFromIntError| {
96+
.map_err(|err: ParseFormatNumberError| {
9797
CliDiagnostic::MigrateError(MigrationDiagnostic {
9898
reason: err.to_string(),
9999
})

crates/biome_cli/src/execute/migrate/prettier.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use biome_deserialize::{json::deserialize_from_json_str, StringSet};
55
use biome_deserialize_macros::Deserializable;
66
use biome_diagnostics::{DiagnosticExt, PrintDiagnostic};
77
use biome_formatter::{
8-
AttributePosition, LineEnding, LineWidth, LineWidthFromIntError, QuoteStyle,
8+
AttributePosition, IndentWidth, LineEnding, LineWidth, ParseFormatNumberError, QuoteStyle,
99
};
1010
use biome_fs::{FileSystem, OpenOptions};
1111
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
@@ -187,18 +187,19 @@ impl From<QuoteProps> for QuoteProperties {
187187
}
188188

189189
impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguration {
190-
type Error = LineWidthFromIntError;
190+
type Error = ParseFormatNumberError;
191191
fn try_from(value: PrettierConfiguration) -> Result<Self, Self::Error> {
192192
let mut result = biome_configuration::PartialConfiguration::default();
193193

194194
let line_width = LineWidth::try_from(value.print_width)?;
195+
let indent_width = IndentWidth::try_from(value.tab_width)?;
195196
let indent_style = if value.use_tabs {
196197
biome_configuration::PlainIndentStyle::Tab
197198
} else {
198199
biome_configuration::PlainIndentStyle::Space
199200
};
200201
let formatter = biome_configuration::PartialFormatterConfiguration {
201-
indent_width: Some(value.tab_width),
202+
indent_width: Some(indent_width),
202203
line_width: Some(line_width),
203204
indent_style: Some(indent_style),
204205
line_ending: Some(value.end_of_line.into()),
@@ -266,7 +267,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
266267
}
267268

268269
impl TryFrom<Override> for biome_configuration::OverridePattern {
269-
type Error = LineWidthFromIntError;
270+
type Error = ParseFormatNumberError;
270271
fn try_from(Override { files, options }: Override) -> Result<Self, Self::Error> {
271272
let mut result = biome_configuration::OverridePattern {
272273
include: Some(StringSet::new(files.into_iter().collect())),
@@ -283,6 +284,12 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
283284
} else {
284285
None
285286
};
287+
// are global options are set
288+
let indent_width = if let Some(indent_width) = options.tab_width {
289+
Some(IndentWidth::try_from(indent_width)?)
290+
} else {
291+
None
292+
};
286293
let indent_style = options.use_tabs.map(|use_tabs| {
287294
if use_tabs {
288295
biome_configuration::PlainIndentStyle::Tab
@@ -291,7 +298,7 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
291298
}
292299
});
293300
let formatter = biome_configuration::OverrideFormatterConfiguration {
294-
indent_width: options.tab_width,
301+
indent_width,
295302
line_width,
296303
indent_style,
297304
line_ending: options.end_of_line.map(|end_of_line| end_of_line.into()),

crates/biome_cli/tests/cases/config_extends.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn allows_reverting_fields_in_extended_config_to_default() {
325325
rome_json.into(),
326326
format!(
327327
r#"{{ "extends": ["format.json"], "formatter": {{ "lineWidth": {} }} }}"#,
328-
LineWidth::default().get()
328+
LineWidth::default().value()
329329
),
330330
);
331331

crates/biome_cli/tests/snap_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use biome_console::fmt::{Formatter, Termcolor};
33
use biome_console::{markup, BufferConsole, Markup};
44
use biome_diagnostics::termcolor::NoColor;
55
use biome_diagnostics::{print_diagnostic_to_string, Error};
6-
use biome_formatter::IndentStyle;
6+
use biome_formatter::{IndentStyle, IndentWidth};
77
use biome_fs::{ConfigName, FileSystemExt, MemoryFileSystem};
88
use biome_json_formatter::context::JsonFormatOptions;
99
use biome_json_formatter::format_node;
@@ -69,7 +69,7 @@ impl CliSnapshot {
6969
let formatted = format_node(
7070
JsonFormatOptions::default()
7171
.with_indent_style(IndentStyle::Space)
72-
.with_indent_width(2.into()),
72+
.with_indent_width(IndentWidth::default()),
7373
&parsed.syntax(),
7474
)
7575
.expect("formatted JSON")

crates/biome_cli/tests/snapshots/main_commands_rage/with_formatter_configuration.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ JSON Formatter:
112112
CSS Formatter:
113113
Enabled: false
114114
Indent style: Tab
115-
Indent width: 0
115+
Indent width: 2
116116
Line ending: Lf
117-
Line width: LineWidth(80)
117+
Line width: 80
118118
Quote style: Double
119119
120120
Server:

crates/biome_configuration/src/css.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::PlainIndentStyle;
22
use biome_deserialize_macros::{Deserializable, Merge, Partial};
3-
use biome_formatter::{LineEnding, LineWidth, QuoteStyle};
3+
use biome_formatter::{IndentWidth, LineEnding, LineWidth, QuoteStyle};
44
use bpaf::Bpaf;
55
use serde::{Deserialize, Serialize};
66

@@ -54,7 +54,7 @@ pub struct CssFormatter {
5454

5555
/// The size of the indentation applied to CSS (and its super languages) files. Default to 2.
5656
#[partial(bpaf(long("css-formatter-indent-width"), argument("NUMBER"), optional))]
57-
pub indent_width: u8,
57+
pub indent_width: IndentWidth,
5858

5959
/// The type of line ending applied to CSS (and its super languages) files.
6060
#[partial(bpaf(long("css-formatter-line-ending"), argument("lf|crlf|cr"), optional))]
@@ -100,3 +100,11 @@ impl PartialCssLinter {
100100
}
101101
}
102102
}
103+
104+
#[test]
105+
fn default_css() {
106+
let css_configuration = CssFormatter::default();
107+
108+
assert_eq!(css_configuration.indent_width, IndentWidth::default());
109+
assert_eq!(css_configuration.indent_width.value(), 2);
110+
}

crates/biome_configuration/src/editorconfig.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{collections::HashMap, str::FromStr};
1313

1414
use biome_deserialize::StringSet;
1515
use biome_diagnostics::{adapters::IniError, Error};
16-
use biome_formatter::{LineEnding, LineWidth};
16+
use biome_formatter::{IndentWidth, LineEnding, LineWidth};
1717
use indexmap::IndexSet;
1818
use serde::{Deserialize, Deserializer};
1919

@@ -83,8 +83,8 @@ impl EditorConfig {
8383
#[serde(default)]
8484
pub struct EditorConfigOptions {
8585
indent_style: Option<PlainIndentStyle>,
86-
#[serde(deserialize_with = "deserialize_optional_u8_from_string")]
87-
indent_size: Option<u8>,
86+
#[serde(deserialize_with = "deserialize_optional_indent_width_from_string")]
87+
indent_size: Option<IndentWidth>,
8888
end_of_line: Option<LineEnding>,
8989
#[serde(deserialize_with = "deserialize_optional_line_width_from_string")]
9090
max_line_length: Option<LineWidth>,
@@ -146,17 +146,16 @@ where
146146
deserialize_bool_from_string(deserializer).map(Some)
147147
}
148148

149-
fn deserialize_optional_u8_from_string<'de, D>(deserializer: D) -> Result<Option<u8>, D::Error>
149+
fn deserialize_optional_indent_width_from_string<'de, D>(
150+
deserializer: D,
151+
) -> Result<Option<IndentWidth>, D::Error>
150152
where
151153
D: Deserializer<'de>,
152154
{
153155
let s = String::deserialize(deserializer)?;
154-
match s.parse() {
155-
Ok(n) => Ok(Some(n)),
156-
Err(_) => Err(serde::de::Error::custom(
157-
"expected a number between 0 and 255",
158-
)),
159-
}
156+
IndentWidth::from_str(s.as_str())
157+
.map_err(serde::de::Error::custom)
158+
.map(Some)
160159
}
161160

162161
fn deserialize_optional_line_width_from_string<'de, D>(
@@ -225,20 +224,20 @@ root = true
225224
226225
[*]
227226
insert_final_newline = true
228-
end_of_line = lf
229-
indent_style = tab
227+
end_of_line = crlf
228+
indent_style = space
230229
indent_size = 4
231-
max_line_length = 120
230+
max_line_length = 80
232231
"#;
233232

234233
let conf = parse_str(input).expect("Failed to parse editorconfig");
235234
let (conf, _) = conf.to_biome();
236235
let conf = conf.expect("Failed to convert editorconfig to biome");
237236
let formatter = conf.formatter.expect("Formatter not set");
238-
assert_eq!(formatter.indent_style, Some(PlainIndentStyle::Tab));
239-
assert_eq!(formatter.indent_width, Some(4));
240-
assert_eq!(formatter.line_ending, Some(LineEnding::Lf));
241-
assert_eq!(formatter.line_width.map(|v| v.get()), Some(120));
237+
assert_eq!(formatter.indent_style, Some(PlainIndentStyle::Space));
238+
assert_eq!(formatter.indent_width.unwrap().value(), 4);
239+
assert_eq!(formatter.line_ending, Some(LineEnding::Crlf));
240+
assert_eq!(formatter.line_width.map(|v| v.value()), Some(80));
242241
}
243242

244243
#[test]

crates/biome_configuration/src/formatter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use biome_deserialize::StringSet;
22
use biome_deserialize_macros::{Deserializable, Merge, Partial};
3-
use biome_formatter::{AttributePosition, IndentStyle, LineEnding, LineWidth};
3+
use biome_formatter::{AttributePosition, IndentStyle, IndentWidth, LineEnding, LineWidth};
44
use bpaf::Bpaf;
55
use serde::{Deserialize, Serialize};
66
use std::str::FromStr;
@@ -27,11 +27,11 @@ pub struct FormatterConfiguration {
2727
/// The size of the indentation, 2 by default (deprecated, use `indent-width`)
2828
#[partial(bpaf(long("indent-size"), argument("NUMBER"), optional))]
2929
#[partial(deserializable(deprecated(use_instead = "formatter.indentWidth")))]
30-
pub indent_size: u8,
30+
pub indent_size: IndentWidth,
3131

3232
/// The size of the indentation, 2 by default
3333
#[partial(bpaf(long("indent-width"), argument("NUMBER"), optional))]
34-
pub indent_width: u8,
34+
pub indent_width: IndentWidth,
3535

3636
/// The type of line ending.
3737
#[partial(bpaf(long("line-ending"), argument("lf|crlf|cr"), optional))]
@@ -82,8 +82,8 @@ impl Default for FormatterConfiguration {
8282
Self {
8383
enabled: true,
8484
format_with_errors: false,
85-
indent_size: 2,
86-
indent_width: 2,
85+
indent_size: IndentWidth::default(),
86+
indent_width: IndentWidth::default(),
8787
indent_style: PlainIndentStyle::default(),
8888
line_ending: LineEnding::default(),
8989
line_width: LineWidth::default(),

0 commit comments

Comments
 (0)