Skip to content

Commit

Permalink
working reading option in single location
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Nov 20, 2024
1 parent ad54e9c commit 19de599
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
20 changes: 19 additions & 1 deletion 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
5 changes: 4 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 @@ -31,7 +31,10 @@ impl FormatNodeRule<JsIfStatement> for FormatJsIfStatement {
if_token.format(),
space(),
l_paren_token.format(),
group(&soft_block_indent(&test.format())),
group(&soft_block_indent_with_maybe_space(
&test.format(),
f.options().space_inside_stuff().value()
)),
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

0 comments on commit 19de599

Please sign in to comment.