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(graphql_formatter): implement BracketSpacing option #3310

Merged
merged 7 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Line ending", markup!({DebugDisplay(formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplay(formatter_configuration.line_width.value())}))}
{KeyValuePair("Attribute position", markup!({DebugDisplay(formatter_configuration.attribute_position)}))}
{KeyValuePair("Bracket spacing", markup!({DebugDisplay(formatter_configuration.bracket_spacing)}))}
denbezrukov marked this conversation as resolved.
Show resolved Hide resolved
{KeyValuePair("Ignore", markup!({DebugDisplay(formatter_configuration.ignore.iter().collect::<Vec<_>>())}))}
{KeyValuePair("Include", markup!({DebugDisplay(formatter_configuration.include.iter().collect::<Vec<_>>())}))}
).fmt(fmt)?;
Expand Down Expand Up @@ -275,6 +276,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Indent width", markup!({DebugDisplayOption(graphql_formatter_configuration.indent_width)}))}
{KeyValuePair("Line ending", markup!({DebugDisplayOption(graphql_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(graphql_formatter_configuration.line_width)}))}
{KeyValuePair("Bracket spacing", markup!({DebugDisplay(graphql_formatter_configuration.bracket_spacing)}))}
denbezrukov marked this conversation as resolved.
Show resolved Hide resolved
{KeyValuePair("Quote style", markup!({DebugDisplay(graphql_formatter_configuration.quote_style)}))}
denbezrukov marked this conversation as resolved.
Show resolved Hide resolved
).fmt(fmt)?;
}
Expand Down
7 changes: 4 additions & 3 deletions crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use biome_deserialize::{json::deserialize_from_json_str, StringSet};
use biome_deserialize_macros::Deserializable;
use biome_diagnostics::{DiagnosticExt, PrintDiagnostic};
use biome_formatter::{
AttributePosition, IndentWidth, LineEnding, LineWidth, ParseFormatNumberError, QuoteStyle,
AttributePosition, BracketSpacing, IndentWidth, LineEnding, LineWidth, ParseFormatNumberError,
QuoteStyle,
};
use biome_fs::{FileSystem, OpenOptions};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
Expand Down Expand Up @@ -210,6 +211,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
enabled: Some(true),
// deprecated
indent_size: None,
bracket_spacing: Some(BracketSpacing::default()),
};
result.formatter = Some(formatter);

Expand Down Expand Up @@ -246,7 +248,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
trailing_comma: None,
quote_style: Some(quote_style),
quote_properties: Some(value.quote_props.into()),
bracket_spacing: Some(value.bracket_spacing),
bracket_spacing: Some(value.bracket_spacing.into()),
jsx_quote_style: Some(jsx_quote_style),
attribute_position: Some(AttributePosition::default()),
};
Expand Down Expand Up @@ -349,7 +351,6 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
.map(|trailing_comma| trailing_comma.into()),
quote_style,
quote_properties: options.quote_props.map(|quote_props| quote_props.into()),
bracket_spacing: options.bracket_spacing,
jsx_quote_style,
..Default::default()
};
Expand Down
50 changes: 50 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ let foo = {a, b};
const {a, b} = foo;
"#;

const APPLY_BRACKET_SPACING_BEFORE_GRAPHQL: &str = r#"{
field_value(
object_value: {key: "value"}
)
}"#;

const APPLY_BRACKET_SPACING_AFTER_GRAPHQL: &str = r#"{
field_value(object_value: {key: "value"})
}
"#;

const APPLY_BRACKET_SAME_LINE_BEFORE: &str = r#"<Foo
className={style}
reallyLongAttributeName1={longComplexValue}
Expand Down Expand Up @@ -3768,3 +3779,42 @@ fn should_error_if_unchanged_files_only_with_changed_flag() {
result,
));
}

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

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

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
("--bracket-spacing"),
("false"),
("--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_BRACKET_SPACING_AFTER_GRAPHQL);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"applies_custom_bracket_spacing_graphql",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ The configuration that is contained inside the file `biome.json`
--line-width=NUMBER What's the max width of a line. Defaults to 80.
--attribute-position=<multiline|auto> The attribute position style in HTMLish languages. By
default auto.
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.
--jsx-quote-style=<double|single> The type of quotes used in JSX. Defaults to double.
--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
Expand All @@ -41,8 +43,6 @@ The configuration that is contained inside the file `biome.json`
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.
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ The configuration that is contained inside the file `biome.json`
--line-width=NUMBER What's the max width of a line. Defaults to 80.
--attribute-position=<multiline|auto> The attribute position style in HTMLish languages. By
default auto.
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.
--jsx-quote-style=<double|single> The type of quotes used in JSX. Defaults to double.
--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
Expand All @@ -43,8 +45,6 @@ The configuration that is contained inside the file `biome.json`
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.
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.graphql`

```graphql
{
field_value(object_value: {key: "value"})
}

```

# Emitted Messages

```block
Formatted 1 file in <TIME>. Fixed 1 file.
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Generic options applied to all files
--line-width=NUMBER What's the max width of a line. Defaults to 80.
--attribute-position=<multiline|auto> The attribute position style in HTMLish languages. By
default auto.
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.

Formatting options specific to the JavaScript files
--jsx-quote-style=<double|single> The type of quotes used in JSX. Defaults to double.
Expand All @@ -29,8 +31,6 @@ Formatting options specific to the JavaScript files
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.
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.
Expand All @@ -49,6 +49,8 @@ Formatting options specific to the JavaScript files
--quote-style=<double|single> The type of quotes used in JavaScript code. Defaults to double.
--javascript-attribute-position=<multiline|auto> The attribute position style in jsx elements.
Defaults to auto.
--bracket-spacing=<true|false> Whether to insert spaces around brackets in object literals.
Defaults to true.

Set of properties to integrate Biome with a VCS software.
--vcs-client-kind=<git> The kind of client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
6 │ + → → "indentWidth":·2,
7 │ + → → "lineEnding":·"lf",
8 │ + → → "lineWidth":·80,
9 │ + → → "attributePosition":·"auto"
10 │ + → },
11 │ + → "linter":·{·"enabled":·true·},
12 │ + → "javascript":·{
13 │ + → → "formatter":·{
14 │ + → → → "jsxQuoteStyle":·"double",
15 │ + → → → "quoteProperties":·"asNeeded",
16 │ + → → → "trailingCommas":·"all",
17 │ + → → → "semicolons":·"always",
18 │ + → → → "arrowParentheses":·"always",
19 │ + → → → "bracketSpacing":·true,
9 │ + → → "attributePosition":·"auto",
10 │ + → → "bracketSpacing":·true
11 │ + → },
12 │ + → "linter":·{·"enabled":·true·},
13 │ + → "javascript":·{
14 │ + → → "formatter":·{
15 │ + → → → "jsxQuoteStyle":·"double",
16 │ + → → → "quoteProperties":·"asNeeded",
17 │ + → → → "trailingCommas":·"all",
18 │ + → → → "semicolons":·"always",
19 │ + → → → "arrowParentheses":·"always",
20 │ + → → → "bracketSameLine":·false,
21 │ + → → → "quoteStyle":·"single",
22 │ + → → → "attributePosition":·"auto"
23 │ + → → }
24 │ + → }
25 │ + }
26 │ +
22 │ + → → → "attributePosition":·"auto",
23 │ + → → → "bracketSpacing":·true
24 │ + → → }
25 │ + → }
26 │ + }
27 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
6 │ + → → "indentWidth":·2,
7 │ + → → "lineEnding":·"lf",
8 │ + → → "lineWidth":·80,
9 │ + → → "attributePosition":·"auto"
10 │ + → },
11 │ + → "javascript":·{
12 │ + → → "formatter":·{
13 │ + → → → "jsxQuoteStyle":·"double",
14 │ + → → → "quoteProperties":·"asNeeded",
15 │ + → → → "trailingCommas":·"all",
16 │ + → → → "semicolons":·"asNeeded",
17 │ + → → → "arrowParentheses":·"always",
18 │ + → → → "bracketSpacing":·true,
9 │ + → → "attributePosition":·"auto",
10 │ + → → "bracketSpacing":·true
11 │ + → },
12 │ + → "javascript":·{
13 │ + → → "formatter":·{
14 │ + → → → "jsxQuoteStyle":·"double",
15 │ + → → → "quoteProperties":·"asNeeded",
16 │ + → → → "trailingCommas":·"all",
17 │ + → → → "semicolons":·"asNeeded",
18 │ + → → → "arrowParentheses":·"always",
19 │ + → → → "bracketSameLine":·false,
20 │ + → → → "quoteStyle":·"single",
21 │ + → → → "attributePosition":·"auto"
22 │ + → → }
23 │ + → }
24 │ + }
25 │ +
21 │ + → → → "attributePosition":·"auto",
22 │ + → → → "bracketSpacing":·true
23 │ + → → }
24 │ + → }
25 │ + }
26 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ expression: content
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 80,
"attributePosition": "auto"
"attributePosition": "auto",
"bracketSpacing": true
},
"linter": { "enabled": true },
"javascript": {
Expand All @@ -23,10 +24,10 @@ expression: content
"trailingCommas": "all",
"semicolons": "always",
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto"
"attributePosition": "auto",
"bracketSpacing": true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ biome.jsonc migrate ━━━━━━━━━━━━━━━━━━━━
6 │ + → → "indentWidth":·2,
7 │ + → → "lineEnding":·"lf",
8 │ + → → "lineWidth":·80,
9 │ + → → "attributePosition":·"auto"
10 │ + → },
11 │ + → "linter":·{·"enabled":·true·},
12 │ + → "javascript":·{
13 │ + → → "formatter":·{
14 │ + → → → "jsxQuoteStyle":·"double",
15 │ + → → → "quoteProperties":·"asNeeded",
16 │ + → → → "trailingCommas":·"all",
17 │ + → → → "semicolons":·"always",
18 │ + → → → "arrowParentheses":·"always",
19 │ + → → → "bracketSpacing":·true,
9 │ + → → "attributePosition":·"auto",
10 │ + → → "bracketSpacing":·true
11 │ + → },
12 │ + → "linter":·{·"enabled":·true·},
13 │ + → "javascript":·{
14 │ + → → "formatter":·{
15 │ + → → → "jsxQuoteStyle":·"double",
16 │ + → → → "quoteProperties":·"asNeeded",
17 │ + → → → "trailingCommas":·"all",
18 │ + → → → "semicolons":·"always",
19 │ + → → → "arrowParentheses":·"always",
20 │ + → → → "bracketSameLine":·false,
21 │ + → → → "quoteStyle":·"single",
22 │ + → → → "attributePosition":·"auto"
23 │ + → → }
24 │ + → }
25 │ + }
26 │ +
22 │ + → → → "attributePosition":·"auto",
23 │ + → → → "bracketSpacing":·true
24 │ + → → }
25 │ + → }
26 │ + }
27 │ +


```
Expand Down
Loading