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

feat(Formatter): add space inside option #4605

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
bracket_spacing: Some(value.bracket_spacing.into()),
jsx_quote_style: Some(jsx_quote_style),
attribute_position: Some(AttributePosition::default()),
space_inside_stuff: None,
};
let js_config = biome_configuration::PartialJavascriptConfiguration {
formatter: Some(js_formatter),
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_configuration/src/javascript/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{
AttributePosition, BracketSpacing, IndentStyle, IndentWidth, LineEnding, LineWidth, QuoteStyle,
SpaceInsideStuff,
};
use biome_js_formatter::context::{
trailing_commas::TrailingCommas, ArrowParentheses, QuoteProperties, Semicolons,
Expand Down Expand Up @@ -88,6 +89,10 @@ pub struct JavascriptFormatter {
#[partial(bpaf(long("quote-style"), argument("double|single"), optional))]
pub quote_style: QuoteStyle,

/// @todo
#[partial(bpaf(long("space-inside-stuff"), argument("true|false"), optional))]
pub space_inside_stuff: SpaceInsideStuff,

// it's also a top-level configurable property.
/// The attribute position style in jsx elements. Defaults to auto.
#[partial(bpaf(
Expand Down Expand Up @@ -122,6 +127,7 @@ impl PartialJavascriptFormatter {
line_width: self.line_width,
quote_style: self.quote_style.unwrap_or_default(),
attribute_position: self.attribute_position,
space_inside_stuff: self.space_inside_stuff.unwrap_or_default(),
}
}
}
Expand All @@ -145,6 +151,7 @@ impl Default for JavascriptFormatter {
line_width: Default::default(),
quote_style: Default::default(),
attribute_position: Default::default(),
space_inside_stuff: Default::default(),
}
}
}
42 changes: 42 additions & 0 deletions crates/biome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,48 @@ impl From<QuoteStyle> for Quote {
}
}

#[derive(Clone, Copy, Debug, Default, Deserializable, Eq, Hash, Merge, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema),
serde(rename_all = "camelCase")
)]
pub struct SpaceInsideStuff(bool);

impl SpaceInsideStuff {
/// Return the boolean value for this [SpaceInsideStuff]
pub fn value(&self) -> bool {
self.0
}
}

impl From<bool> for SpaceInsideStuff {
fn from(value: bool) -> Self {
Self(value)
}
}

impl std::fmt::Display for SpaceInsideStuff {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}", self.value())
}
}

impl FromStr for SpaceInsideStuff {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = bool::from_str(s);

match value {
Ok(value) => Ok(Self(value)),
Err(_) => Err(
"Value not supported for SpaceInsideStuff. Supported values are 'true' and 'false'.",
),
}
}
}

#[derive(Clone, Copy, Debug, Deserializable, Eq, Hash, Merge, PartialEq)]
#[cfg_attr(
feature = "serde",
Expand Down
23 changes: 21 additions & 2 deletions crates/biome_js_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use biome_deserialize_macros::{Deserializable, Merge};
use biome_formatter::printer::PrinterOptions;
use biome_formatter::{
AttributePosition, BracketSpacing, CstFormatContext, FormatContext, FormatElement,
FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth, QuoteStyle, TransformSourceMap,
FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth, QuoteStyle, SpaceInsideStuff,
TransformSourceMap,
};
use biome_js_syntax::{AnyJsFunctionBody, JsFileSource, JsLanguage};
use std::fmt;
Expand Down Expand Up @@ -172,6 +173,9 @@ pub struct JsFormatOptions {

/// Attribute position style. By default auto.
attribute_position: AttributePosition,

// @todo
space_inside_stuff: SpaceInsideStuff,
}

impl JsFormatOptions {
Expand All @@ -191,6 +195,7 @@ impl JsFormatOptions {
bracket_spacing: BracketSpacing::default(),
bracket_same_line: BracketSameLine::default(),
attribute_position: AttributePosition::default(),
space_inside_stuff: SpaceInsideStuff::default(),
}
}

Expand All @@ -204,6 +209,11 @@ impl JsFormatOptions {
self
}

pub fn with_space_inside_stuff(mut self, space_inside_stuff: SpaceInsideStuff) -> Self {
self.space_inside_stuff = space_inside_stuff;
self
}

pub fn with_bracket_same_line(mut self, bracket_same_line: BracketSameLine) -> Self {
self.bracket_same_line = bracket_same_line;
self
Expand Down Expand Up @@ -271,6 +281,10 @@ impl JsFormatOptions {
self.bracket_same_line = bracket_same_line;
}

pub fn set_space_inside_stuff(&mut self, space_inside_stuff: SpaceInsideStuff) {
self.space_inside_stuff = space_inside_stuff;
}

pub fn set_indent_style(&mut self, indent_style: IndentStyle) {
self.indent_style = indent_style;
}
Expand Down Expand Up @@ -318,6 +332,10 @@ impl JsFormatOptions {
self.bracket_spacing
}

pub fn space_inside_stuff(&self) -> SpaceInsideStuff {
self.space_inside_stuff
}

pub fn bracket_same_line(&self) -> BracketSameLine {
self.bracket_same_line
}
Expand Down Expand Up @@ -399,7 +417,8 @@ impl fmt::Display for JsFormatOptions {
writeln!(f, "Arrow parentheses: {}", self.arrow_parentheses)?;
writeln!(f, "Bracket spacing: {}", self.bracket_spacing.value())?;
writeln!(f, "Bracket same line: {}", self.bracket_same_line.value())?;
writeln!(f, "Attribute Position: {}", self.attribute_position)
writeln!(f, "Attribute Position: {}", self.attribute_position)?;
writeln!(f, "Space inside stuff: {}", self.space_inside_stuff)
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/biome_js_formatter/src/js/statements/if_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ impl FormatNodeRule<JsIfStatement> for FormatJsIfStatement {
let l_paren_token = l_paren_token?;
let r_paren_token = r_paren_token?;
let consequent = consequent?;
let should_insert_space_inside_parens = f.options().space_inside_stuff().value();

write!(
f,
[group(&format_args![
if_token.format(),
space(),
l_paren_token.format(),
group(&soft_block_indent(&test.format())),
group(&soft_block_indent_with_maybe_space(
&test.format(),
should_insert_space_inside_parens
)),
r_paren_token.format(),
FormatStatementBody::new(&consequent),
]),]
Expand Down
6 changes: 4 additions & 2 deletions crates/biome_js_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_formatter::{AttributePosition, IndentStyle, LineWidth, QuoteStyle};
use biome_formatter::{AttributePosition, IndentStyle, LineWidth, QuoteStyle, SpaceInsideStuff};
use biome_formatter_test::check_reformat::CheckReformat;
use biome_js_formatter::context::{ArrowParentheses, JsFormatOptions, Semicolons};
use biome_js_formatter::{format_node, JsFormatLanguage};
Expand All @@ -17,6 +17,7 @@ fn quick_test() {
export let shim: typeof import("./foo2") = {
Bar: Bar2
};
if (true) {}
"#;
let source_type = JsFileSource::tsx();
let tree = parse(
Expand All @@ -31,7 +32,8 @@ export let shim: typeof import("./foo2") = {
.with_quote_style(QuoteStyle::Double)
.with_jsx_quote_style(QuoteStyle::Single)
.with_arrow_parentheses(ArrowParentheses::AsNeeded)
.with_attribute_position(AttributePosition::Multiline);
.with_attribute_position(AttributePosition::Multiline)
.with_space_inside_stuff(SpaceInsideStuff::from(false));

let doc = format_node(options.clone(), &tree.syntax()).unwrap();
let result = doc.print().unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/array_nested.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -70,6 +71,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/binding_pattern.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -32,6 +33,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/empty_lines.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -41,6 +42,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/spaces.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -34,6 +35,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/spread.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -34,6 +35,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/trailing-comma/es5/array_trailing_comma.js
info: js/module/array/trailing-commas/es5/array_trailing_commas.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -39,6 +40,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down Expand Up @@ -72,6 +74,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/array/trailing-comma/none/array_trailing_comma.js
info: js/module/array/trailing-commas/none/array_trailing_commas.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -39,6 +40,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down Expand Up @@ -72,6 +74,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/arrow/arrow-comments.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -50,6 +51,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down Expand Up @@ -91,6 +93,7 @@ Arrow parentheses: As needed
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/arrow/arrow_chain_comments.js
snapshot_kind: text
---
# Input

Expand Down Expand Up @@ -38,6 +39,7 @@ Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down Expand Up @@ -75,6 +77,7 @@ Arrow parentheses: As needed
Bracket spacing: true
Bracket same line: false
Attribute Position: Auto
Space inside stuff: false
-----

```js
Expand Down
Loading