From f741a13def7b1e5ee38c925f4b03e19ce9e48df2 Mon Sep 17 00:00:00 2001 From: Denis Bezrukov <6227442+denbezrukov@users.noreply.github.com> Date: Sun, 30 Jun 2024 10:00:06 +0300 Subject: [PATCH] feat(graphql_formatter): implement BracketSpacing option (#3310) Co-authored-by: Ze-Zheng Wu --- crates/biome_cli/src/commands/rage.rs | 8 +- .../biome_cli/src/execute/migrate/prettier.rs | 7 +- crates/biome_cli/tests/commands/format.rs | 50 ++++++++++++ .../main_commands_check/check_help.snap | 4 +- .../snapshots/main_commands_ci/ci_help.snap | 4 +- ...pplies_custom_bracket_spacing_graphql.snap | 18 +++++ .../main_commands_format/format_help.snap | 6 +- .../prettier_migrate.snap | 33 ++++---- .../prettier_migrate_end_of_line.snap | 31 ++++---- .../prettier_migrate_fix.snap | 7 +- .../prettier_migrate_jsonc.snap | 33 ++++---- .../prettier_migrate_overrides.snap | 63 +++++++-------- .../prettier_migrate_with_ignore.snap | 33 ++++---- .../prettier_migrate_write.snap | 7 +- .../prettier_migrate_write_biome_jsonc.snap | 7 +- .../prettier_migrate_write_packagejson.snap | 7 +- ...ettier_migrate_write_with_ignore_file.snap | 5 +- .../prettierjson_migrate_write.snap | 7 +- .../with_formatter_configuration.snap | 8 +- crates/biome_configuration/src/formatter.rs | 10 ++- crates/biome_configuration/src/graphql.rs | 14 +++- .../src/javascript/formatter.rs | 21 ++--- crates/biome_configuration/src/overrides.rs | 18 ++++- crates/biome_css_formatter/src/context.rs | 20 ++--- .../src/format_element/document.rs | 7 +- crates/biome_formatter/src/lib.rs | 57 ++++++++++++++ .../src/printer/printer_options/mod.rs | 22 +++++- crates/biome_graphql_formatter/src/context.rs | 30 +++++-- .../src/graphql/value/object_value.rs | 7 +- .../directive_definition.graphql.snap | 1 + .../graphql/definitions/enum.graphql.snap | 1 + .../definitions/enum_extension.graphql.snap | 1 + .../graphql/definitions/fragment.graphql.snap | 1 + .../definitions/input_object.graphql.snap | 1 + .../input_object_extension.graphql.snap | 1 + .../definitions/interface.graphql.snap | 1 + .../interface_extension.graphql.snap | 1 + .../graphql/definitions/object.graphql.snap | 1 + .../definitions/object_extension.graphql.snap | 1 + .../graphql/definitions/scalar.graphql.snap | 1 + .../definitions/scalar_extension.graphql.snap | 1 + .../graphql/definitions/schema.graphql.snap | 1 + .../definitions/schema_extension.graphql.snap | 1 + .../graphql/definitions/union.graphql.snap | 1 + .../definitions/union_extension.graphql.snap | 1 + .../specs/graphql/directive.graphql.snap | 1 + .../spacing/global/object_spacing.graphql | 5 ++ .../global/object_spacing.graphql.snap | 53 +++++++++++++ .../object/spacing/global/options.json | 11 +++ .../object/spacing/object_spacing.graphql | 5 ++ .../spacing/object_spacing.graphql.snap | 53 +++++++++++++ .../specs/graphql/object/spacing/options.json | 6 ++ .../specs/graphql/operation.graphql.snap | 1 + .../specs/graphql/selection_set.graphql.snap | 1 + .../tests/specs/graphql/simple.graphql.snap | 1 + .../tests/specs/graphql/type.graphql.snap | 1 + .../tests/specs/graphql/value.graphql.snap | 1 + crates/biome_js_formatter/src/context.rs | 35 ++------- crates/biome_json_formatter/src/context.rs | 18 +++-- .../src/file_handlers/graphql.rs | 11 ++- .../src/file_handlers/javascript.rs | 13 +++- crates/biome_service/src/settings.rs | 78 +++++++++++++++++-- .../formatter_extraneous_field.json.snap | 4 +- .../invalid/formatter_quote_style.json.snap | 4 +- .../invalid/overrides/incorrect_key.json.snap | 4 +- .../@biomejs/backend-jsonrpc/src/workspace.ts | 19 ++++- .../@biomejs/biome/configuration_schema.json | 34 +++++++- 67 files changed, 696 insertions(+), 223 deletions(-) create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing_graphql.snap create mode 100644 crates/biome_graphql_formatter/tests/specs/graphql/object/spacing/global/object_spacing.graphql create mode 100644 crates/biome_graphql_formatter/tests/specs/graphql/object/spacing/global/object_spacing.graphql.snap create mode 100644 crates/biome_graphql_formatter/tests/specs/graphql/object/spacing/global/options.json create mode 100644 crates/biome_graphql_formatter/tests/specs/graphql/object/spacing/object_spacing.graphql create mode 100644 crates/biome_graphql_formatter/tests/specs/graphql/object/spacing/object_spacing.graphql.snap create mode 100644 crates/biome_graphql_formatter/tests/specs/graphql/object/spacing/options.json diff --git a/crates/biome_cli/src/commands/rage.rs b/crates/biome_cli/src/commands/rage.rs index a7d7fdfbf016..057c6ec1fec3 100644 --- a/crates/biome_cli/src/commands/rage.rs +++ b/crates/biome_cli/src/commands/rage.rs @@ -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)}))} {KeyValuePair("Ignore", markup!({DebugDisplay(formatter_configuration.ignore.iter().collect::>())}))} {KeyValuePair("Include", markup!({DebugDisplay(formatter_configuration.include.iter().collect::>())}))} ).fmt(fmt)?; @@ -231,14 +232,14 @@ impl Display for RageConfiguration<'_, '_> { {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 spacing", markup!({DebugDisplayOption(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 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.value()))}))} - {KeyValuePair("Attribute position", markup!({DebugDisplay(javascript_formatter_configuration.attribute_position)}))} + {KeyValuePair("Attribute position", markup!({DebugDisplayOption(javascript_formatter_configuration.attribute_position)}))} ) .fmt(fmt)?; @@ -275,7 +276,8 @@ 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("Quote style", markup!({DebugDisplay(graphql_formatter_configuration.quote_style)}))} + {KeyValuePair("Bracket spacing", markup!({DebugDisplayOption(graphql_formatter_configuration.bracket_spacing)}))} + {KeyValuePair("Quote style", markup!({DebugDisplayOption(graphql_formatter_configuration.quote_style)}))} ).fmt(fmt)?; } diff --git a/crates/biome_cli/src/execute/migrate/prettier.rs b/crates/biome_cli/src/execute/migrate/prettier.rs index 4a4037c2c36b..ee23347c6014 100644 --- a/crates/biome_cli/src/execute/migrate/prettier.rs +++ b/crates/biome_cli/src/execute/migrate/prettier.rs @@ -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}; @@ -210,6 +211,7 @@ impl TryFrom for biome_configuration::PartialConfiguratio enabled: Some(true), // deprecated indent_size: None, + bracket_spacing: Some(BracketSpacing::default()), }; result.formatter = Some(formatter); @@ -246,7 +248,7 @@ impl TryFrom 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()), }; @@ -349,7 +351,6 @@ impl TryFrom 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() }; diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index 2575f8cf9cd0..27485b120dcf 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -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#" The attribute position style in HTMLish languages. By default auto. + --bracket-spacing= Whether to insert spaces around brackets in object literals. + Defaults to true. --jsx-quote-style= The type of quotes used in JSX. Defaults to double. --quote-properties= When properties in objects are quoted. Defaults to asNeeded. --trailing-comma= Print trailing commas wherever possible in multi-line comma-separated @@ -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= Whether to add non-necessary parentheses to arrow functions. Defaults to "always". - --bracket-spacing= Whether to insert spaces around brackets in object literals. - Defaults to true. --bracket-same-line= 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. diff --git a/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap b/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap index 9751fcc0f32c..67c599192fec 100644 --- a/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap +++ b/crates/biome_cli/tests/snapshots/main_commands_ci/ci_help.snap @@ -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= The attribute position style in HTMLish languages. By default auto. + --bracket-spacing= Whether to insert spaces around brackets in object literals. + Defaults to true. --jsx-quote-style= The type of quotes used in JSX. Defaults to double. --quote-properties= When properties in objects are quoted. Defaults to asNeeded. --trailing-comma= Print trailing commas wherever possible in multi-line comma-separated @@ -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= Whether to add non-necessary parentheses to arrow functions. Defaults to "always". - --bracket-spacing= Whether to insert spaces around brackets in object literals. - Defaults to true. --bracket-same-line= 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. diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing_graphql.snap b/crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing_graphql.snap new file mode 100644 index 000000000000..fe17d3a0ddbe --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_commands_format/applies_custom_bracket_spacing_graphql.snap @@ -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