Skip to content

Commit

Permalink
feat: support graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 14, 2024
1 parent bc99818 commit 3793035
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ biome_css_parser = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3
biome_css_syntax = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_diagnostics = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_formatter = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_graphql_formatter = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_graphql_parser = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_graphql_syntax = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_js_formatter = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_js_parser = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
biome_js_syntax = { git = "https://github.com/biomejs/biome", tag = "cli/v1.9.3" }
Expand Down
27 changes: 27 additions & 0 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@
"css.indentStyle": {
"$ref": "#/definitions/indentStyle"
},
"graphql.enabled": {
"description": "Enable graphql formatting.",
"default": false,
"type": "boolean"
},
"graphql.indentWidth": {
"$ref": "#/definitions/indentWidth"
},
"graphql.lineWidth": {
"$ref": "#/definitions/lineWidth"
},
"graphql.quoteStyle": {
"$ref": "#/definitions/quoteStyle"
},
"graphql.indentStyle": {
"$ref": "#/definitions/indentStyle"
},
"graphql.bracketSpacing": {
"description": "Surround the inner contents of some braces with spaces.",
"default": true,
"type": "boolean"
},
"javascript.indentStyle": {
"$ref": "#/definitions/indentStyle"
},
Expand All @@ -104,6 +126,11 @@
"javascript.quoteStyle": {
"$ref": "#/definitions/quoteStyle"
},
"javascript.bracketSpacing": {
"description": "Surround the inner contents of some braces with spaces.",
"default": true,
"type": "boolean"
},
"json.indentStyle": {
"$ref": "#/definitions/indentStyle"
},
Expand Down
8 changes: 7 additions & 1 deletion src/configuration/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ pub struct Configuration {
pub css_indent_width: Option<u8>,
pub css_line_width: Option<u16>,
pub css_quote_style: Option<QuoteStyle>,
pub graphql_enabled: Option<bool>,
pub graphql_indent_style: Option<IndentStyle>,
pub graphql_indent_width: Option<u8>,
pub graphql_line_width: Option<u16>,
pub graphql_quote_style: Option<QuoteStyle>,
pub graphql_bracket_spacing: Option<bool>,
pub javascript_indent_style: Option<IndentStyle>,
pub javascript_indent_width: Option<u8>,
pub javascript_line_width: Option<u16>,
Expand All @@ -89,5 +95,5 @@ pub struct Configuration {
pub arrow_parentheses: Option<ArrowParentheses>,
pub trailing_commas: Option<TrailingComma>,
pub bracket_same_line: Option<bool>,
pub bracket_spacing: Option<bool>,
pub javascript_bracket_spacing: Option<bool>,
}
11 changes: 10 additions & 1 deletion src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn resolve_config(
);
let quote_style = get_nullable_value(&mut config, "quoteStyle", &mut diagnostics);
let jsx_quote_style = get_nullable_value(&mut config, "jsxQuoteStyle", &mut diagnostics);
let bracket_spacing = get_nullable_value(&mut config, "bracketSpacing", &mut diagnostics);

let resolved_config = Configuration {
line_ending: get_nullable_value(&mut config, "lineEnding", &mut diagnostics).or(
Expand All @@ -60,6 +61,13 @@ pub fn resolve_config(
css_line_width: get_nullable_value(&mut config, "css.lineWidth", &mut diagnostics).or(line_width),
css_quote_style: get_nullable_value(&mut config, "css.quoteStyle", &mut diagnostics).or(quote_style),
css_indent_style: get_nullable_value(&mut config, "css.indentStyle", &mut diagnostics).or(indent_style),
graphql_enabled: get_nullable_value(&mut config, "graphql.enabled", &mut diagnostics),
graphql_indent_width: get_nullable_value(&mut config, "graphql.indentWidth", &mut diagnostics).or(indent_width),
graphql_line_width: get_nullable_value(&mut config, "graphql.lineWidth", &mut diagnostics).or(line_width),
graphql_quote_style: get_nullable_value(&mut config, "graphql.quoteStyle", &mut diagnostics).or(quote_style),
graphql_indent_style: get_nullable_value(&mut config, "graphql.indentStyle", &mut diagnostics).or(indent_style),
graphql_bracket_spacing: get_nullable_value(&mut config, "graphql.bracketSpacing", &mut diagnostics)
.or(bracket_spacing),
javascript_indent_style: get_nullable_value(&mut config, "javascript.indentStyle", &mut diagnostics)
.or(indent_style),
javascript_indent_width: get_nullable_value(&mut config, "javascript.indentWidth", &mut diagnostics)
Expand All @@ -79,7 +87,8 @@ pub fn resolve_config(
trailing_commas: get_nullable_value(&mut config, "trailingCommas", &mut diagnostics)
.or_else(|| get_nullable_value(&mut config, "trailingComma", &mut diagnostics)),
bracket_same_line: get_nullable_value(&mut config, "bracketSameLine", &mut diagnostics),
bracket_spacing: get_nullable_value(&mut config, "bracketSpacing", &mut diagnostics),
javascript_bracket_spacing: get_nullable_value(&mut config, "javascript.bracketSpacing", &mut diagnostics)
.or(bracket_spacing),
};

diagnostics.extend(get_unknown_property_diagnostics(config));
Expand Down
51 changes: 50 additions & 1 deletion src/format_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use biome_formatter::IndentStyle;
use biome_formatter::LineEnding;
use biome_formatter::LineWidth;
use biome_formatter::QuoteStyle;
use biome_graphql_formatter::context::GraphqlFormatOptions;
use biome_graphql_syntax::GraphqlFileSource;
use biome_js_formatter::context::ArrowParentheses;
use biome_js_formatter::context::JsFormatOptions;
use biome_js_formatter::context::QuoteProperties;
Expand Down Expand Up @@ -98,6 +100,23 @@ pub fn format_text(file_path: &Path, input_text: &str, config: &Configuration) -
let formatted = biome_css_formatter::format_node(options, &tree.syntax())?;
formatted.print()?.into_code()
}
Some("graphql") => {
if config.graphql_enabled != Some(true) {
return Ok(None);
}

let Ok(syntax) = GraphqlFileSource::try_from(file_path) else {
return Ok(None);
};

let options = build_graphql_options(config, syntax)?;
let tree = biome_graphql_parser::parse_graphql(input_text);
if tree.has_errors() {
bail!("{}", get_diagnostics_message(tree.into_diagnostics()));
}
let formatted = biome_graphql_formatter::format_node(options, &tree.syntax())?;
formatted.print()?.into_code()
}
_ => return Ok(None),
};
if output == input_text {
Expand Down Expand Up @@ -175,6 +194,36 @@ fn build_css_options(config: &Configuration, syntax: CssFileSource) -> Result<Cs
Ok(options)
}

fn build_graphql_options(config: &Configuration, syntax: GraphqlFileSource) -> Result<GraphqlFormatOptions> {
let mut options = GraphqlFormatOptions::new(syntax);
if let Some(indent_style) = config.graphql_indent_style {
options = options.with_indent_style(match indent_style {
crate::configuration::IndentStyle::Tab => IndentStyle::Tab,
crate::configuration::IndentStyle::Space => IndentStyle::Space,
});
}
if let Some(value) = config.graphql_indent_width {
if let Ok(value) = value.try_into() {
options = options.with_indent_width(value);
}
}
if let Some(line_width) = config.graphql_line_width {
options = options.with_line_width(
LineWidth::from_str(&line_width.to_string()).map_err(|err| anyhow::anyhow!("{} (Value: {})", err, line_width))?,
);
}
if let Some(quote_style) = &config.graphql_quote_style {
options = options.with_quote_style(match quote_style {
crate::configuration::QuoteStyle::Single => QuoteStyle::Single,
crate::configuration::QuoteStyle::Double => QuoteStyle::Double,
})
}
if let Some(bracket_spacing) = config.graphql_bracket_spacing {
options = options.with_bracket_spacing(bracket_spacing.into());
}
Ok(options)
}

fn build_js_options(config: &Configuration, syntax: JsFileSource) -> Result<JsFormatOptions> {
let mut options = JsFormatOptions::new(syntax);
if let Some(line_ending) = config.line_ending {
Expand Down Expand Up @@ -244,7 +293,7 @@ fn build_js_options(config: &Configuration, syntax: JsFileSource) -> Result<JsFo
})
}

if let Some(bracket_spacing) = &config.bracket_spacing {
if let Some(bracket_spacing) = &config.javascript_bracket_spacing {
options = options.with_bracket_spacing((*bracket_spacing).into());
}

Expand Down
3 changes: 3 additions & 0 deletions src/wasm_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ impl SyncPluginHandler<Configuration> for BiomePluginHandler {
if result.config.css_enabled == Some(true) {
file_extensions.push("css".to_string());
}
if result.config.graphql_enabled == Some(true) {
file_extensions.push("graphql".to_string());
}
PluginResolveConfigurationResult {
config: result.config,
diagnostics: result.diagnostics,
Expand Down
41 changes: 41 additions & 0 deletions tests/specs/Graphql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- file.graphql --
~~ graphql.enabled: true, graphql.indentStyle: space, graphql.indentWidth: 8, graphql.lineWidth: 10 ~~
== should format ==
{
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
rightComparison: hero(episode: JEDI) {
...comparisonFields
}
}

fragment comparisonFields on Character {
name
appearsIn
friends {
name
}
}

[expect]
{
leftComparison: hero(
episode: EMPIRE
) {
...comparisonFields
}
rightComparison: hero(
episode: JEDI
) {
...comparisonFields
}
}

fragment comparisonFields on Character {
name
appearsIn
friends {
name
}
}
36 changes: 36 additions & 0 deletions tests/specs/Graphql_NotEnabled.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- file.graphql --
== should not format ==
{
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
rightComparison: hero(episode: JEDI) {
...comparisonFields
}
}

fragment comparisonFields on Character {
name
appearsIn
friends {
name
}
}

[expect]
{
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
rightComparison: hero(episode: JEDI) {
...comparisonFields
}
}

fragment comparisonFields on Character {
name
appearsIn
friends {
name
}
}

0 comments on commit 3793035

Please sign in to comment.