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(configuration): deprecate trailingComma in favor of trailingCommas #2492

Merged
merged 1 commit into from
May 14, 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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Configuration

#### Enhancements

- The `javascript.formatter.trailingComma` option is deprecated and renamed to `javascript.formatter.trailingCommas`. The corresponding CLI option `--trailing-comma` is also deprecated and renamed to `--trailing-commas`. Details can be checked in [#2492](https://github.com/biomejs/biome/pull/2492). Contributed by @Sec-ant

### Editors

#### New features
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

29 changes: 25 additions & 4 deletions crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ pub(crate) fn format(
{PrintDiagnostic::simple(&diagnostic)}
});

config.indent_width = Some(indent_size);
if config.indent_width.is_none() {
config.indent_width = Some(indent_size);
}
}
}
// TODO: remove in biome 2.0
Expand All @@ -95,7 +97,22 @@ pub(crate) fn format(
{PrintDiagnostic::simple(&diagnostic)}
});

js_formatter.indent_width = Some(indent_size);
if js_formatter.indent_width.is_none() {
js_formatter.indent_width = Some(indent_size);
}
}

if let Some(trailing_comma) = js_formatter.trailing_comma {
let diagnostic = DeprecatedArgument::new(markup! {
"The argument "<Emphasis>"--trailing-comma"</Emphasis>" is deprecated, it will be removed in the next major release. Use "<Emphasis>"--trailing-commas"</Emphasis>" instead."
});
console.error(markup! {
{PrintDiagnostic::simple(&diagnostic)}
});

if js_formatter.trailing_commas.is_none() {
js_formatter.trailing_commas = Some(trailing_comma);
}
}
}
// TODO: remove in biome 2.0
Expand All @@ -108,7 +125,9 @@ pub(crate) fn format(
{PrintDiagnostic::simple(&diagnostic)}
});

json_formatter.indent_width = Some(indent_size);
if json_formatter.indent_width.is_none() {
json_formatter.indent_width = Some(indent_size);
}
}
}
// TODO: remove in biome 2.0
Expand All @@ -121,7 +140,9 @@ pub(crate) fn format(
{PrintDiagnostic::simple(&diagnostic)}
});

css_formatter.indent_width = Some(indent_size);
if css_formatter.indent_width.is_none() {
css_formatter.indent_width = Some(indent_size);
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ impl Display for RageConfiguration<'_, '_> {
{Section("Formatter")}
{KeyValuePair("Format with errors", markup!({DebugDisplay(configuration.get_formatter_configuration().format_with_errors)}))}
{KeyValuePair("Indent style", markup!({DebugDisplay(formatter_configuration.indent_style)}))}
{KeyValuePair("Indent size", markup!({DebugDisplay(formatter_configuration.indent_size)}))}
{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())}))}
Expand All @@ -232,14 +231,13 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Enabled", markup!({DebugDisplay(javascript_formatter_configuration.enabled)}))}
{KeyValuePair("JSX quote style", markup!({DebugDisplay(javascript_formatter_configuration.jsx_quote_style)}))}
{KeyValuePair("Quote properties", markup!({DebugDisplay(javascript_formatter_configuration.quote_properties)}))}
{KeyValuePair("Trailing comma", markup!({DebugDisplay(javascript_formatter_configuration.trailing_comma)}))}
{KeyValuePair("Trailing commas", markup!({DebugDisplay(javascript_formatter_configuration.trailing_commas)}))}
{KeyValuePair("Semicolons", markup!({DebugDisplay(javascript_formatter_configuration.semicolons)}))}
{KeyValuePair("Arrow parentheses", markup!({DebugDisplay(javascript_formatter_configuration.arrow_parentheses)}))}
{KeyValuePair("Bracket spacing", markup!({DebugDisplay(javascript_formatter_configuration.bracket_spacing)}))}
{KeyValuePair("Bracket same line", markup!({DebugDisplay(javascript_formatter_configuration.bracket_same_line)}))}
{KeyValuePair("Quote style", markup!({DebugDisplay(javascript_formatter_configuration.quote_style)}))}
{KeyValuePair("Indent style", markup!({DebugDisplayOption(javascript_formatter_configuration.indent_style)}))}
{KeyValuePair("Indent size", markup!({DebugDisplayOption(javascript_formatter_configuration.indent_size)}))}
{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()))}))}
Expand All @@ -254,7 +252,6 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Enabled", markup!({DebugDisplay(json_formatter_configuration.enabled)}))}
{KeyValuePair("Indent style", markup!({DebugDisplayOption(json_formatter_configuration.indent_style)}))}
{KeyValuePair("Indent width", markup!({DebugDisplayOption(json_formatter_configuration.indent_width)}))}
{KeyValuePair("Indent size", markup!({DebugDisplayOption(json_formatter_configuration.indent_size)}))}
{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("Trailing Commas", markup!({DebugDisplayOption(json_formatter_configuration.trailing_commas)}))}
Expand Down
14 changes: 8 additions & 6 deletions crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use biome_formatter::{
AttributePosition, LineEnding, LineWidth, LineWidthFromIntError, QuoteStyle,
};
use biome_fs::{FileSystem, OpenOptions};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingComma};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
use biome_json_parser::JsonParserOptions;
use biome_service::DynRef;
use std::path::Path;
Expand Down Expand Up @@ -36,7 +36,7 @@ pub(crate) struct PrettierConfiguration {
print_width: u16,
/// https://prettier.io/docs/en/options#use-tabs
use_tabs: bool,
/// https://prettier.io/docs/en/options#trailing-comma
/// https://prettier.io/docs/en/options#trailing-commas
trailing_comma: PrettierTrailingComma,
/// https://prettier.io/docs/en/options#tab-width
tab_width: u8,
Expand Down Expand Up @@ -93,7 +93,7 @@ pub(crate) struct OverrideOptions {
print_width: Option<u16>,
/// https://prettier.io/docs/en/options#use-tabs
use_tabs: Option<bool>,
/// https://prettier.io/docs/en/options#trailing-comma
/// https://prettier.io/docs/en/options#trailing-commas
trailing_comma: Option<PrettierTrailingComma>,
/// https://prettier.io/docs/en/options#tab-width
tab_width: Option<u8>,
Expand Down Expand Up @@ -147,7 +147,7 @@ enum QuoteProps {
Preserve,
}

impl From<PrettierTrailingComma> for TrailingComma {
impl From<PrettierTrailingComma> for TrailingCommas {
fn from(value: PrettierTrailingComma) -> Self {
match value {
PrettierTrailingComma::All => Self::All,
Expand Down Expand Up @@ -240,7 +240,9 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
bracket_same_line: Some(value.bracket_line),
arrow_parentheses: Some(value.arrow_parens.into()),
semicolons: Some(semicolons),
trailing_comma: Some(value.trailing_comma.into()),
trailing_commas: Some(value.trailing_comma.into()),
// deprecated
trailing_comma: None,
quote_style: Some(quote_style),
quote_properties: Some(value.quote_props.into()),
bracket_spacing: Some(value.bracket_spacing),
Expand Down Expand Up @@ -335,7 +337,7 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
bracket_same_line: options.bracket_line,
arrow_parentheses: options.arrow_parens.map(|arrow_parens| arrow_parens.into()),
semicolons,
trailing_comma: options
trailing_commas: options
.trailing_comma
.map(|trailing_comma| trailing_comma.into()),
quote_style,
Expand Down
92 changes: 83 additions & 9 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ const hello: string = "world";
</script>
<div></div>"#;

const APPLY_TRAILING_COMMA_BEFORE: &str = r#"
const APPLY_TRAILING_COMMAS_BEFORE: &str = r#"
const a = [
longlonglonglongItem1longlonglonglongItem1,
longlonglonglongItem1longlonglonglongItem2,
longlonglonglongItem1longlonglonglongItem3,
];
"#;

const APPLY_TRAILING_COMMA_AFTER: &str = r#"const a = [
const APPLY_TRAILING_COMMAS_AFTER: &str = r#"const a = [
longlonglonglongItem1longlonglonglongItem1,
longlonglonglongItem1longlonglonglongItem2,
longlonglonglongItem1longlonglonglongItem3
Expand Down Expand Up @@ -743,12 +743,48 @@ fn applies_custom_css_quote_style() {
}

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

let file_path = Path::new("file.js");
fs.insert(file_path.into(), APPLY_TRAILING_COMMA_BEFORE.as_bytes());
fs.insert(file_path.into(), APPLY_TRAILING_COMMAS_BEFORE.as_bytes());

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

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

assert_file_contents(&fs, file_path, APPLY_TRAILING_COMMAS_AFTER);

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

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

let file_path = Path::new("file.js");
fs.insert(file_path.into(), APPLY_TRAILING_COMMAS_BEFORE.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
Expand All @@ -767,11 +803,49 @@ fn applies_custom_trailing_comma() {

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

assert_file_contents(&fs, file_path, APPLY_TRAILING_COMMA_AFTER);
assert_file_contents(&fs, file_path, APPLY_TRAILING_COMMAS_AFTER);

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

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

let file_path = Path::new("file.js");
fs.insert(file_path.into(), APPLY_TRAILING_COMMAS_BEFORE.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
("--trailing-commas"),
("none"),
("--trailing-comma"),
("all"),
("--write"),
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

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

assert_file_contents(&fs, file_path, APPLY_TRAILING_COMMAS_AFTER);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"applies_custom_trailing_comma",
"applies_custom_trailing_commas_overriding_the_deprecated_option",
fs,
console,
result,
Expand Down Expand Up @@ -923,21 +997,21 @@ fn applies_custom_bracket_same_line() {
}

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

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("format"), ("--trailing-comma"), ("NONE"), ("file.js")].as_slice()),
Args::from([("format"), ("--trailing-commas"), ("NONE"), ("file.js")].as_slice()),
);

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

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"trailing_comma_parse_errors",
"trailing_commas_parse_errors",
fs,
console,
result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The configuration that is contained inside the file `biome.json`
--quote-properties=<preserve|as-needed> When properties in objects are quoted. Defaults to asNeeded.
--trailing-comma=<all|es5|none> Print trailing commas wherever possible in multi-line comma-separated
syntactic structures. Defaults to "all".
--trailing-commas=<all|es5|none> Print trailing commas wherever possible in multi-line comma-separated
syntactic structures. Defaults to "all".
--semicolons=<always|as-needed> Whether the formatter prints semicolons for all statements or
only in for statements where it is necessary because of ASI.
--arrow-parentheses=<always|as-needed> Whether to add non-necessary parentheses to arrow functions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The configuration that is contained inside the file `biome.json`
--quote-properties=<preserve|as-needed> When properties in objects are quoted. Defaults to asNeeded.
--trailing-comma=<all|es5|none> Print trailing commas wherever possible in multi-line comma-separated
syntactic structures. Defaults to "all".
--trailing-commas=<all|es5|none> Print trailing commas wherever possible in multi-line comma-separated
syntactic structures. Defaults to "all".
--semicolons=<always|as-needed> Whether the formatter prints semicolons for all statements or
only in for statements where it is necessary because of ASI.
--arrow-parentheses=<always|as-needed> Whether to add non-necessary parentheses to arrow functions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.js`

```js
const a = [
longlonglonglongItem1longlonglonglongItem1,
longlonglonglongItem1longlonglonglongItem2,
longlonglonglongItem1longlonglonglongItem3
];

```

# Emitted Messages

```block
internalError/fs DEPRECATED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The argument --trailing-comma is deprecated, it will be removed in the next major release. Use --trailing-commas instead.


```

```block
Formatted 1 file in <TIME>. Fixed 1 file.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.js`

```js
const a = [
longlonglonglongItem1longlonglonglongItem1,
longlonglonglongItem1longlonglonglongItem2,
longlonglonglongItem1longlonglonglongItem3
];

```

# Emitted Messages

```block
internalError/fs DEPRECATED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The argument --trailing-comma is deprecated, it will be removed in the next major release. Use --trailing-commas instead.


```

```block
Formatted 1 file in <TIME>. Fixed 1 file.
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Formatting options specific to the JavaScript files
--quote-properties=<preserve|as-needed> When properties in objects are quoted. Defaults to asNeeded.
--trailing-comma=<all|es5|none> Print trailing commas wherever possible in multi-line comma-separated
syntactic structures. Defaults to "all".
--trailing-commas=<all|es5|none> Print trailing commas wherever possible in multi-line comma-separated
syntactic structures. Defaults to "all".
--semicolons=<always|as-needed> Whether the formatter prints semicolons for all statements or
only in for statements where it is necessary because of ASI.
--arrow-parentheses=<always|as-needed> Whether to add non-necessary parentheses to arrow functions.
Expand Down