Skip to content

Commit

Permalink
challenge(formatter): Implement bracketSameLine option to match Pre…
Browse files Browse the repository at this point in the history
…ttier (#799)
  • Loading branch information
faultyserver authored Nov 21, 2023
1 parent b08a836 commit d1e6290
Show file tree
Hide file tree
Showing 228 changed files with 637 additions and 7 deletions.
74 changes: 74 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ let foo = {a, b};
const {a, b} = foo;
"#;

const APPLY_BRACKET_SAME_LINE_BEFORE: &str = r#"<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
/>;
<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
>
Hi
</Foo>;
"#;

const APPLY_BRACKET_SAME_LINE_AFTER: &str = r#"<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
/>;
<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}>
Hi
</Foo>;
"#;

// Without this, Test (windows-latest) fails with: `warning: constant `DEFAULT_CONFIGURATION_BEFORE` is never used`
#[allow(dead_code)]
const DEFAULT_CONFIGURATION_BEFORE: &str = r#"function f() {
Expand Down Expand Up @@ -704,6 +733,51 @@ fn applies_custom_bracket_spacing() {
));
}

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

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

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

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

let mut file = fs
.open(file_path)
.expect("formatting target file was removed by the CLI");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

assert_eq!(content, APPLY_BRACKET_SAME_LINE_AFTER);

drop(file);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"applies_custom_bracket_same_line",
fs,
console,
result,
));
}

#[test]
fn trailing_comma_parse_errors() {
let mut console = BufferConsole::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ The configuration that is contained inside the file `biome.json`
Defaults to "always".
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.
--bracket-same-line=<true|false> Whether to hug the closing bracket of multiline HTML/JSX tags
to the end of the last line, rather than being alone on the following line.
Defaults to false.
--javascript-formatter-enabled=<true|false> Control the formatter for JavaScript (and its super
languages) files.
--javascript-formatter-indent-style=<tab|space> The indent style applied to JavaScript (and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ The configuration that is contained inside the file `biome.json`
Defaults to "always".
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.
--bracket-same-line=<true|false> Whether to hug the closing bracket of multiline HTML/JSX tags
to the end of the last line, rather than being alone on the following line.
Defaults to false.
--javascript-formatter-enabled=<true|false> Control the formatter for JavaScript (and its super
languages) files.
--javascript-formatter-indent-style=<tab|space> The indent style applied to JavaScript (and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.js`

```js
<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}
/>;

<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
reallyLongAttributeName2={anotherLongValue}>
Hi
</Foo>;

```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Formatting options specific to the JavaScript files
Defaults to "always".
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.
--bracket-same-line=<true|false> Whether to hug the closing bracket of multiline HTML/JSX tags
to the end of the last line, rather than being alone on the following line.
Defaults to false.
--javascript-formatter-enabled=<true|false> Control the formatter for JavaScript (and its super
languages) files.
--javascript-formatter-indent-style=<tab|space> The indent style applied to JavaScript (and
Expand Down
41 changes: 40 additions & 1 deletion crates/biome_js_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ pub struct JsFormatOptions {
/// Whether to insert spaces around brackets in object literals. Defaults to true.
bracket_spacing: BracketSpacing,

/// Whether to hug the closing bracket of multiline HTML/JSX tags to the end of the last line, rather than being alone on the following line. Defaults to false.
bracket_same_line: BracketSameLine,

/// Information related to the current file
source_type: JsFileSource,
}
Expand All @@ -184,6 +187,7 @@ impl JsFormatOptions {
semicolons: Semicolons::default(),
arrow_parentheses: ArrowParentheses::default(),
bracket_spacing: BracketSpacing::default(),
bracket_same_line: BracketSameLine::default(),
}
}

Expand All @@ -197,6 +201,11 @@ impl JsFormatOptions {
self
}

pub fn with_bracket_same_line(mut self, bracket_same_line: BracketSameLine) -> Self {
self.bracket_same_line = bracket_same_line;
self
}

pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self {
self.indent_style = indent_style;
self
Expand Down Expand Up @@ -250,6 +259,10 @@ impl JsFormatOptions {
self.bracket_spacing = bracket_spacing;
}

pub fn set_bracket_same_line(&mut self, bracket_same_line: BracketSameLine) {
self.bracket_same_line = bracket_same_line;
}

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

pub fn bracket_same_line(&self) -> BracketSameLine {
self.bracket_same_line
}

pub fn quote_style(&self) -> QuoteStyle {
self.quote_style
}
Expand Down Expand Up @@ -357,7 +374,8 @@ impl fmt::Display for JsFormatOptions {
writeln!(f, "Trailing comma: {}", self.trailing_comma)?;
writeln!(f, "Semicolons: {}", self.semicolons)?;
writeln!(f, "Arrow parentheses: {}", self.arrow_parentheses)?;
writeln!(f, "Bracket spacing: {}", self.bracket_spacing.value())
writeln!(f, "Bracket spacing: {}", self.bracket_spacing.value())?;
writeln!(f, "Bracket same line: {}", self.bracket_same_line.value())
}
}

Expand Down Expand Up @@ -684,3 +702,24 @@ impl From<bool> for BracketSpacing {
Self(value)
}
}

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

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

impl From<bool> for BracketSameLine {
fn from(value: bool) -> Self {
Self(value)
}
}
5 changes: 3 additions & 2 deletions crates/biome_js_formatter/src/jsx/tag/opening_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ impl Format<JsFormatContext> for AnyJsxOpeningElement {
]
)?;

let bracket_same_line = attributes.is_empty() && !name_has_comments;
let force_bracket_same_line = f.options().bracket_same_line().value();
let wants_bracket_same_line = attributes.is_empty() && !name_has_comments;

if self.is_self_closing() {
write!(f, [soft_line_break_or_space(), format_close])
} else if bracket_same_line {
} else if force_bracket_same_line || wants_bracket_same_line {
write!(f, [format_close])
} else {
write!(f, [soft_line_break(), format_close])
Expand Down
11 changes: 9 additions & 2 deletions crates/biome_js_formatter/tests/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use biome_formatter::{
use biome_formatter_test::TestFormatLanguage;
use biome_js_formatter::context::trailing_comma::TrailingComma;
use biome_js_formatter::context::{
ArrowParentheses, BracketSpacing, JsFormatContext, JsFormatOptions, QuoteProperties,
QuoteStyle, Semicolons,
ArrowParentheses, BracketSameLine, BracketSpacing, JsFormatContext, JsFormatOptions,
QuoteProperties, QuoteStyle, Semicolons,
};
use biome_js_formatter::{format_node, format_range, JsFormatLanguage};
use biome_js_parser::{parse, JsParserOptions};
Expand Down Expand Up @@ -228,6 +228,9 @@ pub struct JsSerializableFormatOptions {

/// Whether to insert spaces around brackets in object literals. Defaults to true.
pub bracket_spacing: Option<bool>,

/// Whether to hug the closing bracket of multiline HTML/JSX tags to the end of the last line, rather than being alone on the following line. Defaults to false.
pub bracket_same_line: Option<bool>,
}

impl JsSerializableFormatOptions {
Expand Down Expand Up @@ -273,6 +276,10 @@ impl JsSerializableFormatOptions {
self.bracket_spacing
.map_or_else(BracketSpacing::default, |value| value.into()),
)
.with_bracket_same_line(
self.bracket_same_line
.map_or_else(BracketSameLine::default, |value| value.into()),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down Expand Up @@ -69,6 +70,7 @@ Trailing comma: ES5
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down Expand Up @@ -100,6 +102,7 @@ Trailing comma: None
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down Expand Up @@ -88,6 +89,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: As needed
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down Expand Up @@ -72,6 +73,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: As needed
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down Expand Up @@ -63,6 +64,7 @@ Trailing comma: All
Semicolons: Always
Arrow parentheses: As needed
Bracket spacing: true
Bracket same line: false
-----

```js
Expand Down
Loading

0 comments on commit d1e6290

Please sign in to comment.