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): bootstrap GraphQL formatter #3255

Merged
merged 11 commits into from
Jun 29, 2024
Merged
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pnpm-lock.yaml linguist-generated=true text=auto eol=lf
/crates/biome_js_analyze/tests/specs/**/*.snap linguist-language=Markdown
/crates/biome_css_formatter/tests/**/*.css.prettier-snap linguist-language=CSS
/crates/biome_css_formatter/tests/**/*.css.snap linguist-language=Markdown
/crates/biome_graphql_formatter/tests/**/*.css.prettier-snap linguist-language=GraphQL
/crates/biome_graphql_formatter/tests/**/*.css.snap linguist-language=Markdown
/crates/biome_js_formatter/tests/**/*.ts.prettier-snap linguist-language=TypeScript
/crates/biome_js_formatter/tests/**/*.js.prettier-snap linguist-language=JavaScript
/crates/biome_js_formatter/tests/**/*.ts.snap linguist-language=Markdown
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Profile-*.json
dhat-heap.json

/crates/biome_css_formatter/report.*
/crates/biome_graphql_formatter/report.*
/crates/biome_js_formatter/report.*
/crates/biome_json_formatter/report.*
/crates/biome_json_formatter/report_incompatible.*
Expand All @@ -27,3 +28,4 @@ dhat-heap.json
!/crates/biome_js_formatter/tests/specs/prettier/**/*
!/crates/biome_json_formatter/tests/specs/prettier/**/*
!/crates/biome_css_formatter/tests/specs/prettier/**/*
!/crates/biome_graphql_formatter/tests/specs/prettier/**/*
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ biome_formatter = { version = "0.5.7", path = "./crates/biome_forma
biome_fs = { version = "0.5.7", path = "./crates/biome_fs" }
biome_graphql_analyze = { version = "0.0.1", path = "./crates/biome_graphql_analyze" }
biome_graphql_factory = { version = "0.1.0", path = "./crates/biome_graphql_factory" }
biome_graphql_formatter = { version = "0.1.0", path = "./crates/biome_graphql_formatter" }
biome_graphql_parser = { version = "0.1.0", path = "./crates/biome_graphql_parser" }
biome_graphql_syntax = { version = "0.1.0", path = "./crates/biome_graphql_syntax" }
biome_grit_factory = { version = "0.5.7", path = "./crates/biome_grit_factory" }
Expand Down
12 changes: 12 additions & 0 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Line width", markup!({DebugDisplayOption(css_formatter_configuration.line_width)}))}
{KeyValuePair("Quote style", markup!({DebugDisplay(css_formatter_configuration.quote_style)}))}
).fmt(fmt)?;

let graphql_formatter_configuration =
configuration.get_graphql_formatter_configuration();
markup! (
{Section("GraphQL Formatter")}
{KeyValuePair("Enabled", markup!({DebugDisplay(graphql_formatter_configuration.enabled)}))}
{KeyValuePair("Indent style", markup!({DebugDisplayOption(graphql_formatter_configuration.indent_style)}))}
{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)}))}
).fmt(fmt)?;
}

// Print linter configuration if --linter option is true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ The configuration that is contained inside the file `biome.json`
--css-formatter-quote-style=<double|single> The type of quotes used in CSS code. Defaults to
double.
--css-linter-enabled=<true|false> Control the linter for CSS (and its super languages) files.
--graphql-formatter-enabled=<true|false> Control the formatter for GraphQL files.
--graphql-formatter-indent-style=<tab|space> The indent style applied to GraphQL files.
--graphql-formatter-indent-width=NUMBER The size of the indentation applied to GraphQL files.
Default to 2.
--graphql-formatter-line-ending=<lf|crlf|cr> The type of line ending applied to GraphQL files.
--graphql-formatter-line-width=NUMBER What's the max width of a line applied to GraphQL files.
Defaults to 80.
--graphql-formatter-quote-style=<double|single> The type of quotes used in GraphQL code. Defaults
to double.

Global options applied to all commands
--colors=<off|force> Set the formatting mode for markup: "off" prints everything as plain text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ The configuration that is contained inside the file `biome.json`
--css-formatter-quote-style=<double|single> The type of quotes used in CSS code. Defaults to
double.
--css-linter-enabled=<true|false> Control the linter for CSS (and its super languages) files.
--graphql-formatter-enabled=<true|false> Control the formatter for GraphQL files.
--graphql-formatter-indent-style=<tab|space> The indent style applied to GraphQL files.
--graphql-formatter-indent-width=NUMBER The size of the indentation applied to GraphQL files.
Default to 2.
--graphql-formatter-line-ending=<lf|crlf|cr> The type of line ending applied to GraphQL files.
--graphql-formatter-line-width=NUMBER What's the max width of a line applied to GraphQL files.
Defaults to 80.
--graphql-formatter-quote-style=<double|single> The type of quotes used in GraphQL code. Defaults
to double.

Global options applied to all commands
--colors=<off|force> Set the formatting mode for markup: "off" prints everything as plain text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ CSS Formatter:
Line width: unset
Quote style: Double

GraphQL Formatter:
Enabled: false
Indent style: unset
Indent width: unset
Line ending: unset
Line width: unset
Quote style: Double

Server:
Version: 0.0.0
Name: biome_lsp
Expand Down
84 changes: 84 additions & 0 deletions crates/biome_configuration/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use crate::PlainIndentStyle;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{IndentWidth, LineEnding, LineWidth, QuoteStyle};
use bpaf::Bpaf;
use serde::{Deserialize, Serialize};

/// Options applied to GraphQL files
#[derive(Clone, Default, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct GraphqlConfiguration {
/// GraphQL formatter options
#[partial(type, bpaf(external(partial_graphql_formatter), optional))]
pub formatter: GraphqlFormatter,
}

/// Options that changes how the GraphQL formatter behaves
#[derive(Clone, Debug, Default, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct GraphqlFormatter {
/// Control the formatter for GraphQL files.
#[partial(bpaf(long("graphql-formatter-enabled"), argument("true|false"), optional))]
pub enabled: bool,

/// The indent style applied to GraphQL files.
#[partial(bpaf(
long("graphql-formatter-indent-style"),
argument("tab|space"),
optional
))]
pub indent_style: Option<PlainIndentStyle>,

/// The size of the indentation applied to GraphQL files. Default to 2.
#[partial(bpaf(long("graphql-formatter-indent-width"), argument("NUMBER"), optional))]
pub indent_width: Option<IndentWidth>,

/// The type of line ending applied to GraphQL files.
#[partial(bpaf(
long("graphql-formatter-line-ending"),
argument("lf|crlf|cr"),
optional
))]
pub line_ending: Option<LineEnding>,

/// What's the max width of a line applied to GraphQL files. Defaults to 80.
#[partial(bpaf(long("graphql-formatter-line-width"), argument("NUMBER"), optional))]
pub line_width: Option<LineWidth>,

/// The type of quotes used in GraphQL code. Defaults to double.
#[partial(bpaf(
long("graphql-formatter-quote-style"),
argument("double|single"),
optional
))]
pub quote_style: QuoteStyle,
}

impl PartialGraphqlFormatter {
pub fn get_formatter_configuration(&self) -> GraphqlFormatter {
GraphqlFormatter {
enabled: self.enabled.unwrap_or_default(),
indent_style: self.indent_style,
indent_width: self.indent_width,
line_ending: self.line_ending,
line_width: self.line_width,
quote_style: self.quote_style.unwrap_or_default(),
}
}
}

#[test]
fn default_graphql() {
let graphql_configuration = GraphqlFormatter::default();

assert!(!graphql_configuration.enabled);
assert_eq!(graphql_configuration.indent_style, None);
assert_eq!(graphql_configuration.indent_width, None);
assert_eq!(graphql_configuration.line_ending, None);
assert_eq!(graphql_configuration.line_width, None);
assert_eq!(graphql_configuration.quote_style, QuoteStyle::Double);
}
21 changes: 21 additions & 0 deletions crates/biome_configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod diagnostics;
pub mod editorconfig;
pub mod formatter;
pub mod generated;
pub mod graphql;
pub mod javascript;
pub mod json;
pub mod linter;
Expand All @@ -33,6 +34,10 @@ pub use formatter::{
partial_formatter_configuration, FormatterConfiguration, PartialFormatterConfiguration,
PlainIndentStyle,
};
pub use graphql::{
partial_graphql_configuration, GraphqlConfiguration, GraphqlFormatter,
PartialGraphqlConfiguration, PartialGraphqlFormatter,
};
pub use javascript::{
partial_javascript_configuration, JavascriptConfiguration, JavascriptFormatter,
PartialJavascriptConfiguration, PartialJavascriptFormatter,
Expand Down Expand Up @@ -106,6 +111,10 @@ pub struct Configuration {
#[partial(type, bpaf(external(partial_css_configuration), optional))]
pub css: CssConfiguration,

/// Specific configuration for the GraphQL language
#[partial(type, bpaf(external(partial_graphql_configuration), optional))]
pub graphql: GraphqlConfiguration,

/// A list of paths to other JSON files, used to extends the current configuration.
#[partial(bpaf(hide))]
pub extends: StringSet,
Expand Down Expand Up @@ -218,6 +227,18 @@ impl PartialConfiguration {
.unwrap_or_default()
}

pub fn get_graphql_formatter_configuration(&self) -> GraphqlFormatter {
self.graphql
.as_ref()
.map(|f| {
f.formatter
.as_ref()
.map(|f| f.get_formatter_configuration())
.unwrap_or_default()
})
.unwrap_or_default()
}

pub fn is_linter_disabled(&self) -> bool {
self.linter.as_ref().map_or(false, |f| f.is_disabled())
}
Expand Down
9 changes: 7 additions & 2 deletions crates/biome_formatter_test/src/check_reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ where
)
}

let formatted = self
let formatted = match self
.language
.format_node(self.options.clone(), &re_parse.syntax())
.unwrap();
{
Ok(formatted) => formatted,
Err(err) => {
panic!("failed to format: {}", err);
}
};

let printed = formatted.print().unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/biome_formatter_test/src/prettier/prepare_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function traverseDir(dir, input_config) {
const file = entry.name;

// Ignore spec files
if (file.startsWith('jsfmt.spec')) {
if (file.startsWith('format.test')) {
continue;
}

Expand All @@ -55,7 +55,7 @@ async function traverseDir(dir, input_config) {
const snapshotPath = path.resolve(
dir,
'__snapshots__',
'jsfmt.spec.js.snap'
'format.test.js.snap'
);
const snapFile = path.basename(file) + '.prettier-snap';
const snapOriginalFile = path.basename(file) + '.prettier-snap-original';
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_graphql_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
analyze_and_snap(
&mut snapshot,
&script,
GraphqlFileSource {},
GraphqlFileSource::default(),
filter,
file_name,
input_file,
Expand Down Expand Up @@ -207,7 +207,7 @@ pub(crate) fn _run_suppression_test(input: &'static str, _: &str, _: &str, _: &s
analyze_and_snap(
&mut snapshot,
&input_code,
GraphqlFileSource {},
GraphqlFileSource::default(),
filter,
file_name,
input_file,
Expand Down
38 changes: 38 additions & 0 deletions crates/biome_graphql_formatter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
authors.workspace = true
categories.workspace = true
description = "Biome's GraphQL formatter"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "biome_graphql_formatter"
repository.workspace = true
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_diagnostics = { workspace = true }
biome_formatter = { workspace = true }
biome_graphql_syntax = { workspace = true }
biome_rowan = { workspace = true }
biome_suppression = { workspace = true }

[dev-dependencies]
biome_configuration = { path = "../biome_configuration" }
biome_formatter_test = { path = "../biome_formatter_test" }
biome_graphql_parser = { path = "../biome_graphql_parser" }
biome_parser = { path = "../biome_parser" }
biome_service = { path = "../biome_service" }
countme = { workspace = true, features = ["enable"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tests_macros = { path = "../tests_macros" }

# cargo-workspaces metadata
[package.metadata.workspaces]
independent = true

[lints]
workspace = true
19 changes: 19 additions & 0 deletions crates/biome_graphql_formatter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p align="center">
<img alt="Biome - Toolchain of the web" width="400" src="https://raw.githubusercontent.com/biomejs/resources/main/svg/slogan-light-transparent.svg"/>
</p>

<div align="center">

[![Discord chat][discord-badge]][discord-url]
[![cargo version][cargo-badge]][cargo-url]

[discord-badge]: https://badgen.net/discord/online-members/BypW39g6Yc?icon=discord&label=discord&color=green
[discord-url]: https://discord.gg/BypW39g6Yc
[cargo-badge]: https://badgen.net/crates/v/biome_graphql_formatter?&color=green
[cargo-url]: https://crates.io/crates/biome_graphql_formatter/

</div>

# `biome_graphql_formatter`

Biome's GraphQL formatter implementation. Follow the [documentation](https://docs.rs/biome_graphql_formatter/).
Loading
Loading