Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): default value for indent_width #3096

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,36 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

## Unreleased

### Analyzer

### CLI

#### Bug fixes

- Fix [#3069](https://github.com/biomejs/biome/issues/3069), prevent overwriting paths when using `--staged` or `--changed` options. Contributed by @unvalley


### Configuration

#### Bug fixes

- Fix [#3067](https://github.com/biomejs/biome/issues/3067), by assigning the correct default value to `indentWidth`. Contributed by @ematipico

### Editors

### Formatter

### JavaScript APIs

### Linter

#### Bug fixes

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


### Parser

## 1.8.0 (2024-06-04)

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

### JavaScript APIs

### Linter

#### Promoted rules
Expand Down
6 changes: 3 additions & 3 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Indent style", markup!({DebugDisplay(formatter_configuration.indent_style)}))}
{KeyValuePair("Indent width", markup!({DebugDisplay(formatter_configuration.indent_width)}))}
{KeyValuePair("Line ending", markup!({DebugDisplay(formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplay(formatter_configuration.line_width.get())}))}
{KeyValuePair("Line width", markup!({DebugDisplay(formatter_configuration.line_width.value())}))}
{KeyValuePair("Attribute position", markup!({DebugDisplay(formatter_configuration.attribute_position)}))}
{KeyValuePair("Ignore", markup!({DebugDisplay(formatter_configuration.ignore.iter().collect::<Vec<_>>())}))}
{KeyValuePair("Include", markup!({DebugDisplay(formatter_configuration.include.iter().collect::<Vec<_>>())}))}
Expand All @@ -237,7 +237,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Indent style", markup!({DebugDisplayOption(javascript_formatter_configuration.indent_style)}))}
{KeyValuePair("Indent width", markup!({DebugDisplayOption(javascript_formatter_configuration.indent_width)}))}
{KeyValuePair("Line ending", markup!({DebugDisplayOption(javascript_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(javascript_formatter_configuration.line_width.map(|lw| lw.get()))}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(javascript_formatter_configuration.line_width.map(|lw| lw.value()))}))}
{KeyValuePair("Attribute position", markup!({DebugDisplay(javascript_formatter_configuration.attribute_position)}))}
)
.fmt(fmt)?;
Expand All @@ -250,7 +250,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Indent style", markup!({DebugDisplayOption(json_formatter_configuration.indent_style)}))}
{KeyValuePair("Indent width", markup!({DebugDisplayOption(json_formatter_configuration.indent_width)}))}
{KeyValuePair("Line ending", markup!({DebugDisplayOption(json_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(json_formatter_configuration.line_width.map(|lw| lw.get()))}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(json_formatter_configuration.line_width.map(|lw| lw.value()))}))}
{KeyValuePair("Trailing Commas", markup!({DebugDisplayOption(json_formatter_configuration.trailing_commas)}))}
).fmt(fmt)?;

Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/src/execute/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use biome_deserialize::json::deserialize_from_json_ast;
use biome_deserialize::Merge;
use biome_diagnostics::Diagnostic;
use biome_diagnostics::{category, PrintDiagnostic};
use biome_formatter::LineWidthFromIntError;
use biome_formatter::ParseFormatNumberError;
use biome_fs::{BiomePath, ConfigName, FileSystemExt, OpenOptions};
use biome_json_parser::{parse_json_with_cache, JsonParserOptions};
use biome_json_syntax::{JsonFileSource, JsonRoot};
Expand Down Expand Up @@ -93,7 +93,7 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
let prettier_biome_config =
prettier_config
.try_into()
.map_err(|err: LineWidthFromIntError| {
.map_err(|err: ParseFormatNumberError| {
CliDiagnostic::MigrateError(MigrationDiagnostic {
reason: err.to_string(),
})
Expand Down
17 changes: 12 additions & 5 deletions crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use biome_deserialize::{json::deserialize_from_json_str, StringSet};
use biome_deserialize_macros::Deserializable;
use biome_diagnostics::{DiagnosticExt, PrintDiagnostic};
use biome_formatter::{
AttributePosition, LineEnding, LineWidth, LineWidthFromIntError, QuoteStyle,
AttributePosition, IndentWidth, LineEnding, LineWidth, ParseFormatNumberError, QuoteStyle,
};
use biome_fs::{FileSystem, OpenOptions};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
Expand Down Expand Up @@ -187,18 +187,19 @@ impl From<QuoteProps> for QuoteProperties {
}

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

let line_width = LineWidth::try_from(value.print_width)?;
let indent_width = IndentWidth::try_from(value.tab_width)?;
let indent_style = if value.use_tabs {
biome_configuration::PlainIndentStyle::Tab
} else {
biome_configuration::PlainIndentStyle::Space
};
let formatter = biome_configuration::PartialFormatterConfiguration {
indent_width: Some(value.tab_width),
indent_width: Some(indent_width),
line_width: Some(line_width),
indent_style: Some(indent_style),
line_ending: Some(value.end_of_line.into()),
Expand Down Expand Up @@ -266,7 +267,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
}

impl TryFrom<Override> for biome_configuration::OverridePattern {
type Error = LineWidthFromIntError;
type Error = ParseFormatNumberError;
fn try_from(Override { files, options }: Override) -> Result<Self, Self::Error> {
let mut result = biome_configuration::OverridePattern {
include: Some(StringSet::new(files.into_iter().collect())),
Expand All @@ -283,6 +284,12 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
} else {
None
};
// are global options are set
let indent_width = if let Some(indent_width) = options.tab_width {
Some(IndentWidth::try_from(indent_width)?)
} else {
None
};
let indent_style = options.use_tabs.map(|use_tabs| {
if use_tabs {
biome_configuration::PlainIndentStyle::Tab
Expand All @@ -291,7 +298,7 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
}
});
let formatter = biome_configuration::OverrideFormatterConfiguration {
indent_width: options.tab_width,
indent_width,
line_width,
indent_style,
line_ending: options.end_of_line.map(|end_of_line| end_of_line.into()),
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/tests/cases/config_extends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ fn allows_reverting_fields_in_extended_config_to_default() {
rome_json.into(),
format!(
r#"{{ "extends": ["format.json"], "formatter": {{ "lineWidth": {} }} }}"#,
LineWidth::default().get()
LineWidth::default().value()
),
);

Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/tests/snap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use biome_console::fmt::{Formatter, Termcolor};
use biome_console::{markup, BufferConsole, Markup};
use biome_diagnostics::termcolor::NoColor;
use biome_diagnostics::{print_diagnostic_to_string, Error};
use biome_formatter::IndentStyle;
use biome_formatter::{IndentStyle, IndentWidth};
use biome_fs::{ConfigName, FileSystemExt, MemoryFileSystem};
use biome_json_formatter::context::JsonFormatOptions;
use biome_json_formatter::format_node;
Expand Down Expand Up @@ -69,7 +69,7 @@ impl CliSnapshot {
let formatted = format_node(
JsonFormatOptions::default()
.with_indent_style(IndentStyle::Space)
.with_indent_width(2.into()),
.with_indent_width(IndentWidth::default()),
&parsed.syntax(),
)
.expect("formatted JSON")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ JSON Formatter:
CSS Formatter:
Enabled: false
Indent style: Tab
Indent width: 0
Indent width: 2
Line ending: Lf
Line width: LineWidth(80)
Line width: 80
Quote style: Double

Server:
Expand Down
12 changes: 10 additions & 2 deletions crates/biome_configuration/src/css.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::PlainIndentStyle;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{LineEnding, LineWidth, QuoteStyle};
use biome_formatter::{IndentWidth, LineEnding, LineWidth, QuoteStyle};
use bpaf::Bpaf;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct CssFormatter {

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

/// The type of line ending applied to CSS (and its super languages) files.
#[partial(bpaf(long("css-formatter-line-ending"), argument("lf|crlf|cr"), optional))]
Expand Down Expand Up @@ -100,3 +100,11 @@ impl PartialCssLinter {
}
}
}

#[test]
fn default_css() {
let css_configuration = CssFormatter::default();

assert_eq!(css_configuration.indent_width, IndentWidth::default());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value should be None; otherwise, the CSS formatter will not inherit indent_width from the global formatter settings when css.formatter.indent_width is unset.

Related: #3103

assert_eq!(css_configuration.indent_width.value(), 2);
}
33 changes: 16 additions & 17 deletions crates/biome_configuration/src/editorconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{collections::HashMap, str::FromStr};

use biome_deserialize::StringSet;
use biome_diagnostics::{adapters::IniError, Error};
use biome_formatter::{LineEnding, LineWidth};
use biome_formatter::{IndentWidth, LineEnding, LineWidth};
use indexmap::IndexSet;
use serde::{Deserialize, Deserializer};

Expand Down Expand Up @@ -83,8 +83,8 @@ impl EditorConfig {
#[serde(default)]
pub struct EditorConfigOptions {
indent_style: Option<PlainIndentStyle>,
#[serde(deserialize_with = "deserialize_optional_u8_from_string")]
indent_size: Option<u8>,
#[serde(deserialize_with = "deserialize_optional_indent_width_from_string")]
indent_size: Option<IndentWidth>,
end_of_line: Option<LineEnding>,
#[serde(deserialize_with = "deserialize_optional_line_width_from_string")]
max_line_length: Option<LineWidth>,
Expand Down Expand Up @@ -146,17 +146,16 @@ where
deserialize_bool_from_string(deserializer).map(Some)
}

fn deserialize_optional_u8_from_string<'de, D>(deserializer: D) -> Result<Option<u8>, D::Error>
fn deserialize_optional_indent_width_from_string<'de, D>(
deserializer: D,
) -> Result<Option<IndentWidth>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.parse() {
Ok(n) => Ok(Some(n)),
Err(_) => Err(serde::de::Error::custom(
"expected a number between 0 and 255",
)),
}
IndentWidth::from_str(s.as_str())
.map_err(serde::de::Error::custom)
.map(Some)
}

fn deserialize_optional_line_width_from_string<'de, D>(
Expand Down Expand Up @@ -225,20 +224,20 @@ root = true

[*]
insert_final_newline = true
end_of_line = lf
indent_style = tab
end_of_line = crlf
indent_style = space
indent_size = 4
max_line_length = 120
max_line_length = 80
"#;

let conf = parse_str(input).expect("Failed to parse editorconfig");
let (conf, _) = conf.to_biome();
let conf = conf.expect("Failed to convert editorconfig to biome");
let formatter = conf.formatter.expect("Formatter not set");
assert_eq!(formatter.indent_style, Some(PlainIndentStyle::Tab));
assert_eq!(formatter.indent_width, Some(4));
assert_eq!(formatter.line_ending, Some(LineEnding::Lf));
assert_eq!(formatter.line_width.map(|v| v.get()), Some(120));
assert_eq!(formatter.indent_style, Some(PlainIndentStyle::Space));
assert_eq!(formatter.indent_width.unwrap().value(), 4);
assert_eq!(formatter.line_ending, Some(LineEnding::Crlf));
assert_eq!(formatter.line_width.map(|v| v.value()), Some(80));
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions crates/biome_configuration/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use biome_deserialize::StringSet;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{AttributePosition, IndentStyle, LineEnding, LineWidth};
use biome_formatter::{AttributePosition, IndentStyle, IndentWidth, LineEnding, LineWidth};
use bpaf::Bpaf;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
Expand All @@ -27,11 +27,11 @@ pub struct FormatterConfiguration {
/// The size of the indentation, 2 by default (deprecated, use `indent-width`)
#[partial(bpaf(long("indent-size"), argument("NUMBER"), optional))]
#[partial(deserializable(deprecated(use_instead = "formatter.indentWidth")))]
pub indent_size: u8,
pub indent_size: IndentWidth,

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

/// The type of line ending.
#[partial(bpaf(long("line-ending"), argument("lf|crlf|cr"), optional))]
Expand Down Expand Up @@ -82,8 +82,8 @@ impl Default for FormatterConfiguration {
Self {
enabled: true,
format_with_errors: false,
indent_size: 2,
indent_width: 2,
indent_size: IndentWidth::default(),
indent_width: IndentWidth::default(),
indent_style: PlainIndentStyle::default(),
line_ending: LineEnding::default(),
line_width: LineWidth::default(),
Expand Down
6 changes: 3 additions & 3 deletions crates/biome_configuration/src/javascript/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::PlainIndentStyle;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{AttributePosition, LineEnding, LineWidth, QuoteStyle};
use biome_formatter::{AttributePosition, IndentWidth, LineEnding, LineWidth, QuoteStyle};
use biome_js_formatter::context::{
trailing_commas::TrailingCommas, ArrowParentheses, QuoteProperties, Semicolons,
};
Expand Down Expand Up @@ -63,15 +63,15 @@ pub struct JavascriptFormatter {
/// The size of the indentation applied to JavaScript (and its super languages) files. Default to 2.
#[partial(deserializable(deprecated(use_instead = "javascript.formatter.indentWidth")))]
#[partial(bpaf(long("javascript-formatter-indent-size"), argument("NUMBER"), optional))]
pub indent_size: Option<u8>,
pub indent_size: Option<IndentWidth>,

/// The size of the indentation applied to JavaScript (and its super languages) files. Default to 2.
#[partial(bpaf(
long("javascript-formatter-indent-width"),
argument("NUMBER"),
optional
))]
pub indent_width: Option<u8>,
pub indent_width: Option<IndentWidth>,

/// The type of line ending applied to JavaScript (and its super languages) files.
#[partial(bpaf(
Expand Down
6 changes: 3 additions & 3 deletions crates/biome_configuration/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::PlainIndentStyle;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{LineEnding, LineWidth};
use biome_formatter::{IndentWidth, LineEnding, LineWidth};
use biome_json_formatter::context::TrailingCommas;
use bpaf::Bpaf;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -54,12 +54,12 @@ pub struct JsonFormatter {

/// The size of the indentation applied to JSON (and its super languages) files. Default to 2.
#[partial(bpaf(long("json-formatter-indent-width"), argument("NUMBER"), optional))]
pub indent_width: Option<u8>,
pub indent_width: Option<IndentWidth>,

/// The size of the indentation applied to JSON (and its super languages) files. Default to 2.
#[partial(bpaf(long("json-formatter-indent-size"), argument("NUMBER"), optional))]
#[partial(deserializable(deprecated(use_instead = "json.formatter.indentWidth")))]
pub indent_size: Option<u8>,
pub indent_size: Option<IndentWidth>,

/// The type of line ending applied to JSON (and its super languages) files.
#[partial(bpaf(long("json-formatter-line-ending"), argument("lf|crlf|cr"), optional))]
Expand Down
1 change: 1 addition & 0 deletions crates/biome_configuration/src/linter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ impl<'de> serde::Deserialize<'de> for RuleSelector {
}
}

#[cfg(feature = "schema")]
impl schemars::JsonSchema for RuleSelector {
fn schema_name() -> String {
"RuleCode".to_string()
Expand Down
Loading
Loading