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 graphql formatter #3296

Merged
merged 2 commits into from
Jun 27, 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
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 @@ -210,7 +210,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
25 changes: 8 additions & 17 deletions crates/biome_graphql_formatter/src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::prelude::*;
use biome_diagnostics::category;
use biome_formatter::comments::{
is_doc_comment, CommentKind, CommentStyle, Comments, SourceComment,
};
use biome_formatter::formatter::Formatter;
use biome_formatter::{write, FormatResult, FormatRule};
use biome_graphql_syntax::{GraphqlLanguage, TextLen};
use biome_rowan::SyntaxTriviaPieceComments;
use biome_suppression::parse_suppression_comment;

pub type GraphqlComments = Comments<GraphqlLanguage>;

Expand Down Expand Up @@ -64,22 +62,15 @@ pub struct GraphqlCommentStyle;
impl CommentStyle for GraphqlCommentStyle {
type Language = GraphqlLanguage;

fn is_suppression(text: &str) -> bool {
parse_suppression_comment(text)
.filter_map(Result::ok)
.flat_map(|suppression| suppression.categories)
.any(|(key, _)| key == category!("format"))
fn is_suppression(_text: &str) -> bool {
// parse_suppression_comment(text)
// .filter_map(Result::ok)
// .flat_map(|suppression| suppression.categories)
// .any(|(key, _)| key == category!("format"))
false
arendjr marked this conversation as resolved.
Show resolved Hide resolved
}

fn get_comment_kind(comment: &SyntaxTriviaPieceComments<Self::Language>) -> CommentKind {
if comment.text().starts_with("/*") {
if comment.has_newline() {
CommentKind::Block
} else {
CommentKind::InlineBlock
}
} else {
CommentKind::Line
}
fn get_comment_kind(_comment: &SyntaxTriviaPieceComments<Self::Language>) -> CommentKind {
CommentKind::Line
}
}
8 changes: 5 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/alias.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlAlias;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlAlias, GraphqlAliasFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlAlias;
impl FormatNodeRule<GraphqlAlias> for FormatGraphqlAlias {
fn fmt_fields(&self, node: &GraphqlAlias, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlAliasFields { value, colon_token } = node.as_fields();

write!(f, [value.format(), colon_token.format(), space()])
}
}
16 changes: 13 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/argument.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlArgument;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlArgument, GraphqlArgumentFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlArgument;
impl FormatNodeRule<GraphqlArgument> for FormatGraphqlArgument {
fn fmt_fields(&self, node: &GraphqlArgument, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlArgumentFields {
name,
colon_token,
value,
} = node.as_fields();

write!(
f,
[name.format(), colon_token.format(), space(), value.format(),]
)
}
}
20 changes: 17 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlArguments;
use biome_rowan::AstNode;
use biome_formatter::{format_args, write};
use biome_graphql_syntax::{GraphqlArguments, GraphqlArgumentsFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlArguments;
impl FormatNodeRule<GraphqlArguments> for FormatGraphqlArguments {
fn fmt_fields(&self, node: &GraphqlArguments, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlArgumentsFields {
l_paren_token,
arguments,
r_paren_token,
} = node.as_fields();

write!(
f,
[group(&format_args!(
l_paren_token.format(),
soft_block_indent(&arguments.format()),
r_paren_token.format(),
))]
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlDescription;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlDescription, GraphqlDescriptionFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlDescription;
impl FormatNodeRule<GraphqlDescription> for FormatGraphqlDescription {
fn fmt_fields(&self, node: &GraphqlDescription, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlDescriptionFields {
graphql_string_value,
} = node.as_fields();

write!(f, [graphql_string_value.format()])
}
}
13 changes: 10 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/directive.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlDirective;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlDirective, GraphqlDirectiveFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlDirective;
impl FormatNodeRule<GraphqlDirective> for FormatGraphqlDirective {
fn fmt_fields(&self, node: &GraphqlDirective, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlDirectiveFields {
at_token,
name,
arguments,
} = node.as_fields();

write![f, [at_token.format(), name.format(), arguments.format()]]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlDirectiveLocation;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlDirectiveLocation, GraphqlDirectiveLocationFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlDirectiveLocation;
impl FormatNodeRule<GraphqlDirectiveLocation> for FormatGraphqlDirectiveLocation {
Expand All @@ -9,6 +10,8 @@ impl FormatNodeRule<GraphqlDirectiveLocation> for FormatGraphqlDirectiveLocation
node: &GraphqlDirectiveLocation,
f: &mut GraphqlFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlDirectiveLocationFields { value_token } = node.as_fields();

write!(f, [value_token.format()])
}
}
29 changes: 26 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/field.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlField;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlField, GraphqlFieldFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlField;
impl FormatNodeRule<GraphqlField> for FormatGraphqlField {
fn fmt_fields(&self, node: &GraphqlField, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlFieldFields {
alias,
name,
arguments,
directives,
selection_set,
} = node.as_fields();

write!(
f,
[
alias.format(),
name.format(),
arguments.format(),
directives.format(),
]
)?;

if let Some(selection_set) = selection_set {
write!(f, [space(), selection_set.format()])?;
}

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlFragmentSpread;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlFragmentSpread, GraphqlFragmentSpreadFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlFragmentSpread;
impl FormatNodeRule<GraphqlFragmentSpread> for FormatGraphqlFragmentSpread {
Expand All @@ -9,6 +10,15 @@ impl FormatNodeRule<GraphqlFragmentSpread> for FormatGraphqlFragmentSpread {
node: &GraphqlFragmentSpread,
f: &mut GraphqlFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlFragmentSpreadFields {
dotdotdot_token,
name,
directives,
} = node.as_fields();

write![
f,
[dotdotdot_token.format(), name.format(), directives.format()]
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlImplementsInterfaces;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlImplementsInterfaces, GraphqlImplementsInterfacesFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlImplementsInterfaces;
impl FormatNodeRule<GraphqlImplementsInterfaces> for FormatGraphqlImplementsInterfaces {
Expand All @@ -9,6 +10,19 @@ impl FormatNodeRule<GraphqlImplementsInterfaces> for FormatGraphqlImplementsInte
node: &GraphqlImplementsInterfaces,
f: &mut GraphqlFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlImplementsInterfacesFields {
implements_token,
amp_token,
interfaces,
} = node.as_fields();

if let Some(amp_token) = amp_token {
write!(f, [format_removed(&amp_token)])?;
}

write!(
f,
[implements_token.format(), space(), interfaces.format(),]
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlInlineFragment;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlInlineFragment, GraphqlInlineFragmentFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlInlineFragment;
impl FormatNodeRule<GraphqlInlineFragment> for FormatGraphqlInlineFragment {
Expand All @@ -9,6 +10,19 @@ impl FormatNodeRule<GraphqlInlineFragment> for FormatGraphqlInlineFragment {
node: &GraphqlInlineFragment,
f: &mut GraphqlFormatter,
) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlInlineFragmentFields {
dotdotdot_token,
type_condition,
directives,
selection_set,
} = node.as_fields();

write!(f, [dotdotdot_token.format()])?;

if let Some(type_condition) = type_condition {
write!(f, [space(), type_condition.format()])?;
}

write!(f, [directives.format(), space(), selection_set.format(),])
}
}
20 changes: 17 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/list_type.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlListType;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlListType, GraphqlListTypeFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlListType;
impl FormatNodeRule<GraphqlListType> for FormatGraphqlListType {
fn fmt_fields(&self, node: &GraphqlListType, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlListTypeFields {
l_brack_token,
element,
r_brack_token,
} = node.as_fields();

write!(
f,
[
l_brack_token.format(),
element.format(),
r_brack_token.format()
]
)
}
}
9 changes: 6 additions & 3 deletions crates/biome_graphql_formatter/src/graphql/auxiliary/name.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::prelude::*;
use biome_graphql_syntax::GraphqlName;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_graphql_syntax::{GraphqlName, GraphqlNameFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGraphqlName;
impl FormatNodeRule<GraphqlName> for FormatGraphqlName {
fn fmt_fields(&self, node: &GraphqlName, f: &mut GraphqlFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GraphqlNameFields { value_token } = node.as_fields();

write![f, [value_token.format()]]
}
}
Loading